In SX Microcontrollers, SX/B Compiler and SX-Key Tool, jbrierley wrote: Sorry it's such a mess (which is why I didn't post the whole thing before - I've been trying different things), but here's the whole code: [code] ' ========================================================================= ' ' File...... TEMPLATE.SXB ' Purpose... SX/B Programming Template ' Author.... ' E-mail.... ' Started... ' Updated... 05 JUL 2006 ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- 'DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX 'FREQ 4_000_000 DEVICE SX28, OSCXT1, TURBO, STACKX, OPTIONX FREQ 4_000_000 'DEVICE SX28, OSCHS2, TURBO, STACKX, OPTIONX 'FREQ 4_000_000 ID "SXB 1.50" ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- LcdTx PIN RA.1 ' LCD serial connection Dio PIN RB.1 ' data to/from module Clk PIN RB.0 ' clock output CS PIN RB.2 ' active-low chip select ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- LcdBaud CON "T19200" ' or T2400, or T9600 LcdLF CON $0A ' move cursor down 1 line LcdCls CON $0C ' clear LCD (need 5 ms delay) LcdCR CON $0D ' move pos 0 of next line LcdBLon CON $11 ' backlight on LcdOn1 CON $16 ' LCD on; no crsr, no blink LcdLine1 CON $80 ' move to line 1, column 0 LcdLine2 CON $94 ' move to line 2, column 0 XAxis CON 0 ' adc channels YAxis CON 1 ZAxis CON 2 VRef CON 3 gfcnv CON $3852 ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- tmpB3 VAR Byte tmpW1 VAR Word axis VAR BYTE rvCount VAR WORD rvCount1 VAR BYTE rvCount2 VAR BYTE axCount VAR WorD axCount1 VAR BYTE axCount2 VAR BYTE value VAR WORD char VAR BYTE lcd1 VAR WORD ndex VAR byte sgn var BYTE ' ------------------------------------------------------------------------- INTERRUPT ' ------------------------------------------------------------------------- ISR_Start: ' ISR code here ISR_Exit: RETURNINT ' {cycles} ' ========================================================================= PROGRAM Start ' ========================================================================= ' ------------------------------------------------------------------------- ' Subroutine Declarations ' ------------------------------------------------------------------------- DELAY SUB 1, 2 ' delay in milliseconds LCD_OUT SUB 1, 2 ' byte or string to LCD CLEAR_LCD SUB 0 ' clear LCD, BL is on Get_H48C SUB 0 ' accelerometer reading WRITELCDCHAR SUB 1 ' delay in milliseconds WriteByte SUB 1 ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: ' initialization code here PAUSE 100 CLEAR_LCD LCD_OUT LcdBLon Again: LCD_OUT LcdLine1 ndex = 0 LCD_OUT "R=" FOR axis = XAxis TO Zaxis ndex = ndex + 1 GOSUB Get_H48C if rvCount > 4096 then if ndex = 1 then value = rvCount WRITELCDCHAR 0 LCD_OUT LcdLine2 LCD_OUT "--ERROR--" endif else if ndex = 1 then value = rvCount WRITELCDCHAR 0 LCD_OUT ", X=" endif if ndex = 2 then LCD_OUT LcdLine2 LCD_OUT "Y=" endif if ndex = 3 then LCD_OUT ", Z=" endif value = axCount WRITELCDCHAR 1 endif NEXT delay 250 ' main code here GOTO Again ' ------------------------------------------------------------------------- ' Subroutine Code ' ------------------------------------------------------------------------- Get_H48C: LOW CS SHIFTOUT Dio, Clk, MSBFIRST, %11\2 SHIFTOUT Dio, Clk, MSBFIRST, Vef\3 ' select vref register SHIFTIN DIO, CLK, MSBPOST, rvCount1\5 SHIFTIN DIO, CLK, MSBPOST, rvCount2\8 HIGH CS PAUSE 1 LOW CS SHIFTOUT Dio, Clk, MSBFIRST, %11\2 SHIFTOUT Dio, Clk, MSBFIRST, axis\3 ' select axis SHIFTIN DIO, CLK, MSBPOST, axCount1\5 SHIFTIN DIO, CLK, MSBPOST, axCount2\8 HIGH CS PUT @rvCount, rvCount2, RvCount1 ' Put the 12-bit reference value into a WORD variable PUT @axCount, axCount2, axCount1 ' Put the 12-bit axis value into a WORD variable ' Convert the axis reading to a g-force ' sgn is 0 for positive, 1 for negative if axCount >= rvCount then axCount = axCount - rvCount axCount = axCount ** gfcnv sgn = 0 else axCount = rvCount - axCount axCount = axCount ** gfcnv sgn = 1 endif RETURN WRITELCDCHAR: rvCount1 = __PARAM1 if sgn = 1 then LCD_OUT "-" else LCD_OUT "+" endif tmpW1 = value / 1000 GET @tmpW1, axCount2, axCount1 if tmpW1 > 0 then WriteByte axCount2 endif tmpW1 = value // 1000 tmpW1 = tmpW1 / 100 GET @tmpW1, axCount2, axCount1 WriteByte axCount2 if rvCount1 > 0 then LCD_OUT "." endif tmpW1 = value // 1000 tmpW1 = tmpW1 // 100 tmpW1 = tmpW1 / 10 GET @tmpW1, axCount2, axCount1 WriteByte axCount2 tmpW1 = value // 1000 tmpW1 = tmpW1 // 100 tmpW1 = tmpW1 // 10 GET @tmpW1, axCount2, axCount1 WriteByte axCount2 Return WriteByte: axCount1 = __PARAM1 'break if axCount1 > 9 then char = "-" else Read TheData + axCount1, char endif LCD_OUT char RETURN ' Use: DELAY ms ' -- 'ms' is delay in milliseconds, 1 - 65535 DELAY: IF __PARAMCNT = 1 THEN tmpW1 = __PARAM1 ' save byte value ELSE tmpW1 = __WPARAM12 ' save word value ENDIF PAUSE tmpW1 RETURN LCD_OUT: axCount1 = __PARAM1 ' byte or string offset IF __PARAMCNT = 2 THEN ' string specified? axCount2 = __PARAM2 ' yes, save base DO READ axCount2 + axCount1, tmpB3 ' read a character IF tmpB3 = 0 THEN EXIT ' if 0, string complete SEROUT LcdTx, LcdBaud, tmpB3 ' send the byte INC axCount1 ' point to next character axCount2 = axCount2 + Z ' update base on overflow LOOP ELSE SEROUT LcdTx, LcdBaud, axCount1 ' send the byte ENDIF RETURN ' ------------------------------------------------------------------------- ' Use: CLEAR_LCD ' -- clears the LCD and activates the backlight ' -- removes cursor and blinking block CLEAR_LCD: LCD_OUT LcdBLon ' backlight on LCD_OUT LcdOn1 ' no cursor or blink LCD_OUT LcdCls ' clear the LCD DELAY 5 RETURN ' ========================================================================= ' User Data ' ========================================================================= TheData: DATA "0123456789" [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=231466#m231481 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2007 (http://www.dotNetBB.com)