In SX Microcontrollers, SX/B Compiler and SX-Key Tool, Mike_W wrote: [3]Every now and again I take out my SX Tech board and try to rewrite one of the BS2 programs in SX/B. well I have been working with the Ping Sensor and SX/B the last couple of days. With the use of the Ping manual, SX/B help files, and various snippets of code from the forums I have come up with this program.[/3] [3] [/3] [3]I am using the SX Tech Board with the 4 MHZ resonator installed, a Parallax 2 X 16 LCD display, and a Ping sensor. I am displaying the value of RawDist to the LCD[/3] [3] [/3] [3]The program seems to work, but not OK! There are dead spots in front of the ping where the value of RawDist drops and then starts to gain again. I suspect I may have timing issues.[/3] [3] [/3] [3]I would appreciate any help or suggestions[/3] [3] [/3] [3]Thank You [/3] [3]Mike W[/3] [3] [/3] [3] [/3] [3]' =========================================================================[/3] [3]'[/3] [3]' File...... Ping2.SXB[/3] [3]'[/3] [3]' =========================================================================[/3] [3]' -------------------------------------------------------------------------[/3] [3]' Device Settings[/3] [3]' -------------------------------------------------------------------------[/3] [3]DEVICE SX28, OSCXT2, TURBO, STACKX, OPTIONX[/3] [3]FREQ 4_000_000[/3] [3]' -------------------------------------------------------------------------[/3] [3]' IO Pins[/3] [3]' -------------------------------------------------------------------------[/3] [3]Lcd VAR RA.0 ' LCD serial pin[/3] [3]Ping VAR RA.2 ' Ping I/O pin[/3] [3]' -------------------------------------------------------------------------[/3] [3]' Constants[/3] [3]' -------------------------------------------------------------------------[/3] [3]Trigger CON 1[/3] [3]IsHigh CON 1[/3] [3]IsLow CON 0[/3] [3] [/3] [3]LcdBaud CON "T19200" ' or T2400, or T9600[/3] [3] [/3] [3]LcdCls CON $0C ' clear LCD (need 5 ms delay)[/3] [3]LcdCR CON $0D ' move pos 0 of next line[/3] [3]LcdBLon CON $11 ' backlight on[/3] [3]LcdBLoff CON $12 ' backlight off[/3] [3]LcdOn1 CON $16 ' LCD on; no crsr, no blink[/3] [3]LcdLine1 CON $80 ' move to line 1, column 0[/3] [3]LcdLine2 CON $94 ' move to line 2, column 0[/3] [3]' -------------------------------------------------------------------------[/3] [3]' Variables[/3] [3]' -------------------------------------------------------------------------[/3] [3]rawDist VAR Word ' hold raw distance[/3] [3] [/3] [3]digit VAR Byte ' digitacter to send[/3] [3] [/3] [3]tempW VAR Word ' subroutine work vars[/3] [3]temp1 VAR Byte[/3] [3]temp2 VAR Byte[/3] [3]temp3 VAR Byte[/3] [3]' -------------------------------------------------------------------------[/3] [3]' =========================================================================[/3] [3] PROGRAM Start[/3] [3]' =========================================================================[/3] [3]' -------------------------------------------------------------------------[/3] [3]' Subroutine Declarations[/3] [3]' -------------------------------------------------------------------------[/3] [3]LCD_OUT SUB 1 ' byte to LCD[/3] [3]LCD_STR SUB 2 ' string to LCD[/3] [3]GET_DIST SUB ' get Ping reading[/3] [3]GET_DEC SUB ' print using DEC format [/3] [3]' -------------------------------------------------------------------------[/3] [3]' Program Code[/3] [3]' -------------------------------------------------------------------------[/3] [3]Start:[/3] [3] PLP_A = %0101 ' pull up unused pins[/3] [3] PLP_B = %00000000[/3] [3] PLP_C = %00000000[/3] [3] [/3] [3] HIGH Lcd ' make output pin high[/3] [3] PAUSE 100 ' let LCD initialize[/3] [3] [/3] [3]Main:[/3] [3] LCD_OUT LcdBLoff ' backlight on / off[/3] [3] LCD_OUT LcdOn1 ' no cursor or blink[/3] [3] LCD_OUT LcdCls ' clear the LCD[/3] [3] PAUSE 250[/3] [3] [/3] [3] DO[/3] [3] GET_DIST ' ping subroutine[/3] [3] [/3] [3] LCD_OUT LcdLine1 ' Line one[/3] [3] LCD_STR "RawDist " ' line heading[/3] [3] GET_DEC rawDist ' data to display[/3] [3] [/3] [3] LOOP[/3] [3]' -------------------------------------------------------------------------[/3] [3]' Subroutine Code[/3] [3]' -------------------------------------------------------------------------[/3] [3]GET_DIST:[/3] [3] Ping = IsLow ' make RA.2 0-1-0[/3] [3] PULSOUT Ping, Trigger, 2 ' activate sensor[/3] [3] PULSIN Ping, IsHigh, rawDist, 1 ' measure echo pulse[/3] [3] [/3] [3] RETURN[/3] [3]'--------------------------------------------------------------------------[/3] [3]LCD_OUT:[/3] [3] temp1 = __PARAM1 ' get output[/3] [3] SEROUT Lcd, LcdBaud, temp1 ' send output to LCD[/3] [3] [/3] [3] RETURN [/3] [3]' -------------------------------------------------------------------------[/3] [3]LCD_STR:[/3] [3] temp2 = __PARAM1 ' save offset[/3] [3] temp3 = __PARAM2 ' save base[/3] [3] DO[/3] [3] READ temp3 + temp2, temp1 ' read a character[/3] [3] IF temp1 = 0 THEN EXIT ' if 0, string complete[/3] [3] LCD_OUT temp1 ' send the byte[/3] [3] INC temp2 ' point to next character[/3] [3] temp3 = temp3 + Z ' update base on overflow[/3] [3] LOOP[/3] [3] [/3] [3] RETURN[/3] [3]' -------------------------------------------------------------------------[/3] [3]GET_DEC:[/3] [3] tempW = __WPARAM12[/3] [3] digit = "0" [/3] [3] DO WHILE tempW >= 1000 ' Thousands[/3] [3] INC digit[/3] [3] tempW = tempW - 1000[/3] [3] LOOP[/3] [3] LCD_OUT digit ' Senddigit digit[/3] [3] digit = "0"[/3] [3] DO WHILE tempW >= 100 ' Hundreds[/3] [3] INC digit[/3] [3] tempW = tempW - 100[/3] [3] LOOP[/3] [3] LCD_OUT digit ' Senddigit digit[/3] [3] digit = "0"[/3] [3] DO WHILE tempW >= 10 ' Tens[/3] [3] INC digit[/3] [3] tempW = tempW - 10[/3] [3] LOOP[/3] [3] LCD_OUT digit ' Senddigit digit[/3] [3] digit = "0" + tempW_LSB ' ones[/3] [3] LCD_OUT digit ' Senddigit digit[/3] [3] [/3] [3] RETURN[/3] [3]' -------------------------------------------------------------------------[/3] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=178081 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)