ultrasonic distance sensor
I used the following code to test out a SRF-04 Ultrasonic sensor from Acroname with a 16f876 (I think, maybe an f84). I believe it worked fine. Results sent to a serial LCD or to Hyperterminal. I think it's on the lines of what Melanie is suggesting.
<code>
DEFINE LOADER_USED 1 'Only required if bootloader used to program PIC
DEFINE HSER_RCSTA 90h 'setup receive register
DEFINE HSER_TXSTA 20h 'setup transmit register
DEFINE HSER_BAUD 2400 'setup baud rate
'DEFINE HSER_SPBRG 25 'setup baud rate
' -----[ Variables ]-------------------------------------------------------
Dist VAR WORD 'setup variable to hold result of sonar conversion distance
fire VAR WORD 'setup variable to hold result of flame sensor
speed VAR WORD 'setup variable to hold result of next sensor
fire = 18 'dummy number for now
speed = 27 'dummy number for now
' -----[ Initialization ]--------------------------------------------------
'
portsetup:
PORTC = %00000000 ' all outputs off to start
TRISC = %00010000 ' All of Port c is outputs except RC4
' -----[ Main Code ]-------------------------------------------------------
main:
'Pause 10 'settle in time(may not be needed)
GoSub sr_sonar 'goto sonar code
GoSub hyperview
GoSub lcdview 'print on LCD and Hyperterminal
GoTo main 'Repeat forever
sr_sonar:
PORTC.4 = 0 'Be sure RC4 is low
PulsOut PORTC.4,2 ' 10us init pulse
PulsIn PORTC.5,1,Dist ' measure echo time
Dist = Dist / 15 ' convert to inches
Pause 100
Return
hyperview:
HSerout ["Distance to Object = ", DEC dist,13,10]
Pause 250
HSerout ["Magnitude of flame = ", DEC fire,13,10] 'output data to hyperterminal
Pause 250
Pause 250
HSerout ["Speed of Travel = ", DEC speed,13,10]
Return
lcdview
SerOut PORTB.6,0,[254,1] 'clear LCD screen
Pause 40
SerOut PORTB.6,8,[254,134,"FIRE"] 'List outputs to LCD
SerOut PORTB.6,8,[254,199,#fire]
SerOut PORTB.6,8,[254,128,"DATA"]
SerOut PORTB.6,8,[254,193,#Dist]
SerOut PORTB.6,8,[254,140,"SPED"]
SerOut PORTB.6,8,[254,204,#speed]
Pause 500
Return
End
</code>
It's been a few years since I've tried it. Hope it helps.