Hello. I am trying to use Parallax ultrasonic Sensor PN 28015
For some reason, I cannot get the code to work. I have searched for quite a bit and found examples of people using similar code with the same and similar(devantech sf04) devices.
I am using a PIC16f887 at 8mhz external oscillator. This should give a pulsout cycle of 5us.
In the attached image you can see the interface info for the sonar sensor. It appears that I send a pulse of 5us, wait ~750us then read back a result upto 18.5ms. Any idea if my current code fulfills these goals?
I have hooked the devices upto an o-scope and verified their operation. I can see the return pulse begin 750us after the end of a trigger pulse. I read in the pic basic docs that the pulsin waits 65555 cycles for a pulse to time. Is this all working with my current code?
When I trigger the device, the sensors LED lights, but I always get back a 0.
Here is my code.
Code:DEFINE OSC 8 ' Set oscillator to 8mhz DEFINE DEBUG_REG PORTC ' Set PORTC as debug serial out port DEFINE DEBUG_BIT 6 ' PORTC.6 Set Debug-Out pin bit DEFINE DEBUG_BAUD 9600 ' Set Debug baud rate DEFINE DEBUG_MODE 0 ' Set Debug mode: 0 = true, 1 = inverted TRISB.0 = 0 ' setup port b.0 as output initially ANSEL = 0 ' disable all AD convertors ' -----[ I/O Definitions ]------------------------------------------------- Ping var PORTB.0 ' -----[ Variables & Constants]-------------------------------------------- rawDist VAR Word ' raw measurement Trigger CON 2 ' trigger pulse = 10 uS (at 8mhz this is 5us per pulsout) IsHigh CON 1 ' for PULSOUT IsLow CON 0 ' -----[ Initialization ]-------------------------------------------------- reset: PAUSE 100 DEBUG "Parallax Ping Sonar Test ", 10 ' -----[ Program Code ]---------------------------------------------------- Main: if PORTA.0 = 1 then ' wait for keypress GOSUB Get_Sonar ' get sensor value debug DEC rawDist ' output raw distance data PAUSE 100 endif goto main ' loop forever END ' -----[ Subroutines ]----------------------------------------------------- Get_Sonar: 'high 4 low ping ' make trigger 0-1-0 PULSOUT Ping, Trigger ' activate sensor by sending 10us pulse PULSIN Ping, 1, rawDist ' measure echo pulse RETURN




Bookmarks