PDA

View Full Version : pulsout pulsin on same pin - timing?



vacpress
- 14th January 2009, 06:53
Hello. I am trying to use Parallax ultrasonic Sensor PN 28015 (http://www.parallax.com/Store/Microcontrollers/BASICStampModules/tabid/134/txtSearch/28015/List/1/ProductID/92/Default.aspx?SortField=ProductName%2cProductName)

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.

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

HenrikOlsson
- 14th January 2009, 13:28
You are not waiting 750uS between sending the pulse and measuring the return pulse - if that's what you're trying to do. It may be a case of read-modify-write where the output doesn't actually have time to return to LOW between the PulsOut and PulsIn command and therefor the PulsIn command misses the return pulse - just an idea.

If you know that it will be at least 750uS between the end of the outgoing pulse and the beginning of the return pulse then try inserting a PauseUs 700 or something between the PulsOut and PulsIn commands.

/Henrik.

vacpress
- 15th January 2009, 04:55
Henrik,


TRISB.0 = 0 ' setup port b.0 as output initially
ANSEL = 0 ' disable all AD convertors
ANSELH = 0 ' Disable PortB AD


Thanks for the reply. I fixed the problem. So simple - configuration fuses. On the Pic16f887 there are 12 A/D ports, spanning port A and port B. Apparently PortB.0 defaulted to analog in. It magically worked when I properly configured it! So much troubleshooting for one little fuse.

Incidently, I tried the pauseUS method you recommended, it didnt help - probably because the pin was setup as an analog convertor.

The code worked without a pause once i configured the chip correctly.

Thanks!

R

HenrikOlsson
- 15th January 2009, 07:21
Oh, the ADC....that's a first...not ;-)

I'm glad you figured it out!

/Henrik.