SX/B and Ping
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.
·
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
·
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.
·
I would appreciate any help or suggestions
·
Thank You
Mike W
·
·
' =========================================================================
'
'·· File...... Ping2.SXB
'
' =========================================================================
'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
Lcd····· ··········· VAR··········· RA.0··· ··········· ··········· ' LCD serial pin
Ping····· ··········· VAR··········· RA.2··· ··········· ··········· ' Ping I/O pin
'
' Constants
'
Trigger ··········· CON··········· 1
IsHigh· ··········· CON··········· 1
IsLow· ··········· CON··········· 0
·
LcdBaud········· ··········· CON··········· "T19200"········ ··········· ' or T2400, or T9600
·
LcdCls········· CON···· $0C············ ··········· ' clear LCD (need 5 ms delay)
LcdCR·········· CON···· $0D············ ··········· ' move pos 0 of next line
LcdBLon········ CON···· $11············ ··········· ' backlight on
LcdBLoff······· CON···· $12············ ··········· ' backlight off
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
'
' Variables
'
rawDist··········· ··········· VAR··········· Word·· ··········· ··········· ' hold raw distance
·
digit·········· VAR···· Byte········· ··········· ··········· ' digitacter to send
·
tempW ··········· VAR··········· Word·· ··········· ··········· ' subroutine work vars
temp1·· ··········· VAR··········· Byte
temp2·· ··········· VAR··········· Byte
temp3·· ··········· VAR··········· Byte
'
' =========================================================================
· PROGRAM Start
' =========================================================================
'
' Subroutine Declarations
'
LCD_OUT····· ··········· SUB··········· 1········· ··········· ··········· ' byte to LCD
LCD_STR······ ··········· SUB··········· 2········· ··········· ··········· ' string to LCD
GET_DIST··········· SUB···· ······················· ··········· ' get Ping reading
GET_DEC····· ··········· SUB···· ······················· ··········· ' print using DEC format
'
' Program Code
'
Start:
· PLP_A = %0101············ ······················· ··········· ' pull up unused pins
· PLP_B = %00000000
· PLP_C = %00000000
·
· HIGH Lcd····· ······················· ··········· ··········· ' make output pin high
· PAUSE 100·· ······················· ··········· ··········· ' let LCD initialize
·
Main:
· LCD_OUT LcdBLoff········ ······················· ··········· ' backlight on / off
· LCD_OUT LcdOn1·········· ······················· ··········· ' no cursor or blink
· LCD_OUT LcdCls············ ··········· ··········· ' clear the LCD
· PAUSE 250
·
· DO
· GET_DIST··· ······················· ··········· ··········· ' ping subroutine
···
· LCD_OUT LcdLine1········· ······················· ··········· ' Line one
· LCD_STR "RawDist "······· ······················· ··········· ' line heading
· GET_DEC rawDist··········· ······················· ··········· ' data to display
·
· LOOP
'
' Subroutine Code
'
GET_DIST:
· Ping = IsLow································· ' make RA.2 0-1-0
· PULSOUT Ping, Trigger, 2····················· ' activate sensor
· PULSIN· Ping, IsHigh, rawDist, 1············· ' measure echo pulse
··········· ·
· RETURN
'
LCD_OUT:
· temp1 = __PARAM1··· ······················· ··········· ' get output
· SEROUT Lcd, LcdBaud, temp1·············· ··········· ' send output to LCD
·
· RETURN
'
LCD_STR:
· temp2 = __PARAM1··· ······················· ··········· ' save offset
· temp3 = __PARAM2··· ······················· ··········· ' save base
· DO
··· READ temp3 + temp2, temp1············ ··········· ' read a character
··· IF temp1 = 0 THEN EXIT················ ··········· ' if 0, string complete
··· LCD_OUT temp1········· ···· ··········· ··········· ' send the byte
··· INC temp2···························· ··········· ' point to next character
··· temp3 = temp3 + Z···················· ··········· ' update base on overflow
· LOOP
·
· RETURN
'
GET_DEC:
· tempW = __WPARAM12
··· digit = "0"
·· DO WHILE tempW >= 1000··············· ··········· ' Thousands
··· INC digit
··· tempW = tempW - 1000
· LOOP
·· LCD_OUT digit················· ··········· ··········· ' Senddigit digit
···· digit = "0"
· DO WHILE tempW >= 100······················· ··········· ··········· ' Hundreds
··· INC digit
··· tempW = tempW - 100
· LOOP
·· LCD_OUT digit················· ··········· ··········· ' Senddigit digit
···· digit = "0"
· DO WHILE tempW >= 10· ······················· ··········· ' Tens
··· INC digit
··· tempW = tempW - 10
· LOOP
·· LCD_OUT digit················· ··········· ··········· ' Senddigit digit
···· digit = "0" + tempW_LSB··· ··········· ··········· ' ones
· LCD_OUT digit ················ ··········· ··········· ' Senddigit digit
·
· RETURN
'
·
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
·
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.
·
I would appreciate any help or suggestions
·
Thank You
Mike W
·
·
' =========================================================================
'
'·· File...... Ping2.SXB
'
' =========================================================================
'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
Lcd····· ··········· VAR··········· RA.0··· ··········· ··········· ' LCD serial pin
Ping····· ··········· VAR··········· RA.2··· ··········· ··········· ' Ping I/O pin
'
' Constants
'
Trigger ··········· CON··········· 1
IsHigh· ··········· CON··········· 1
IsLow· ··········· CON··········· 0
·
LcdBaud········· ··········· CON··········· "T19200"········ ··········· ' or T2400, or T9600
·
LcdCls········· CON···· $0C············ ··········· ' clear LCD (need 5 ms delay)
LcdCR·········· CON···· $0D············ ··········· ' move pos 0 of next line
LcdBLon········ CON···· $11············ ··········· ' backlight on
LcdBLoff······· CON···· $12············ ··········· ' backlight off
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
'
' Variables
'
rawDist··········· ··········· VAR··········· Word·· ··········· ··········· ' hold raw distance
·
digit·········· VAR···· Byte········· ··········· ··········· ' digitacter to send
·
tempW ··········· VAR··········· Word·· ··········· ··········· ' subroutine work vars
temp1·· ··········· VAR··········· Byte
temp2·· ··········· VAR··········· Byte
temp3·· ··········· VAR··········· Byte
'
' =========================================================================
· PROGRAM Start
' =========================================================================
'
' Subroutine Declarations
'
LCD_OUT····· ··········· SUB··········· 1········· ··········· ··········· ' byte to LCD
LCD_STR······ ··········· SUB··········· 2········· ··········· ··········· ' string to LCD
GET_DIST··········· SUB···· ······················· ··········· ' get Ping reading
GET_DEC····· ··········· SUB···· ······················· ··········· ' print using DEC format
'
' Program Code
'
Start:
· PLP_A = %0101············ ······················· ··········· ' pull up unused pins
· PLP_B = %00000000
· PLP_C = %00000000
·
· HIGH Lcd····· ······················· ··········· ··········· ' make output pin high
· PAUSE 100·· ······················· ··········· ··········· ' let LCD initialize
·
Main:
· LCD_OUT LcdBLoff········ ······················· ··········· ' backlight on / off
· LCD_OUT LcdOn1·········· ······················· ··········· ' no cursor or blink
· LCD_OUT LcdCls············ ··········· ··········· ' clear the LCD
· PAUSE 250
·
· DO
· GET_DIST··· ······················· ··········· ··········· ' ping subroutine
···
· LCD_OUT LcdLine1········· ······················· ··········· ' Line one
· LCD_STR "RawDist "······· ······················· ··········· ' line heading
· GET_DEC rawDist··········· ······················· ··········· ' data to display
·
· LOOP
'
' Subroutine Code
'
GET_DIST:
· Ping = IsLow································· ' make RA.2 0-1-0
· PULSOUT Ping, Trigger, 2····················· ' activate sensor
· PULSIN· Ping, IsHigh, rawDist, 1············· ' measure echo pulse
··········· ·
· RETURN
'
LCD_OUT:
· temp1 = __PARAM1··· ······················· ··········· ' get output
· SEROUT Lcd, LcdBaud, temp1·············· ··········· ' send output to LCD
·
· RETURN
'
LCD_STR:
· temp2 = __PARAM1··· ······················· ··········· ' save offset
· temp3 = __PARAM2··· ······················· ··········· ' save base
· DO
··· READ temp3 + temp2, temp1············ ··········· ' read a character
··· IF temp1 = 0 THEN EXIT················ ··········· ' if 0, string complete
··· LCD_OUT temp1········· ···· ··········· ··········· ' send the byte
··· INC temp2···························· ··········· ' point to next character
··· temp3 = temp3 + Z···················· ··········· ' update base on overflow
· LOOP
·
· RETURN
'
GET_DEC:
· tempW = __WPARAM12
··· digit = "0"
·· DO WHILE tempW >= 1000··············· ··········· ' Thousands
··· INC digit
··· tempW = tempW - 1000
· LOOP
·· LCD_OUT digit················· ··········· ··········· ' Senddigit digit
···· digit = "0"
· DO WHILE tempW >= 100······················· ··········· ··········· ' Hundreds
··· INC digit
··· tempW = tempW - 100
· LOOP
·· LCD_OUT digit················· ··········· ··········· ' Senddigit digit
···· digit = "0"
· DO WHILE tempW >= 10· ······················· ··········· ' Tens
··· INC digit
··· tempW = tempW - 10
· LOOP
·· LCD_OUT digit················· ··········· ··········· ' Senddigit digit
···· digit = "0" + tempW_LSB··· ··········· ··········· ' ones
· LCD_OUT digit ················ ··········· ··········· ' Senddigit digit
·
· RETURN
'
Comments
GET_DIST:
Ping = IsLow
PULSOUT Ping, Trigger, 2············ ·'but 5 normall·
Pauseus 750
PULSIN· Ping, IsHigh, rawDist, 1·············
··················································· 'requires 115 us to 18ms to measure return wave··
· RETURN
··················································· 'requires 200 us to refresh
·A lot of serial out comands in between durring the refresh time
Also I don't know how critical the speed of sound is for your application, but here is the formulae I use:
·····Sqr root·[noparse][[/noparse](((F-32)*.5555)+273)*1.4*287] = speed of sound in meters/sec
I appreciate your time and suggestions.
Later today I will get a chance to play around with it.
Thanks
Mike W
Can you elaborate on this or provide some example data? Do you mean to imply that you have used the Ping sensor in an identical arrangement running on a BS2 and did not experience these dropouts?
I looked over the code but nothing jumped out at me as being a glaring error.
- Sparks
You have raised some thought about my project.
I am going to get my BS2 out and see·if it responds·the same way·with the·same conditions.
Baiscly the RawDist value continues to increase till I get about 4 or 5 feet from the ping, then nothing happens·until I get 6 feet·or so away then the RawDist value starts to increase again but Sporadically.
Let me see what happens with the BS2.
I'll get back
Mike·W
I changed the trigger constant from 1 to 5
and added a pause 250 at the end of the do loop
the results were positive, so I think I need to work on my timing issues.
when I use the BS2 i get a rawDist reading of 2000 + or so.
the SX/b gives me a rawDist readings around 800 and seemes to be limited to about 6 feet
these are readings taken around my office (a lot of furniture and stuff)
im going to the basement (large flat floor and no obstructions) I'll see what results I get there
Mike W
http://forums.parallax.com/showthread.php?p=623170