pulsout pulsin on same pin - timing?


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2007
    Posts
    70

    Default pulsout pulsin on same pin - timing?

    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
    Attached Images Attached Images  
    Last edited by vacpress; - 14th January 2009 at 07:17.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    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.

  3. #3
    Join Date
    Jan 2007
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    Henrik,

    Code:
    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
    Last edited by vacpress; - 15th January 2009 at 04:58.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default

    Oh, the ADC....that's a first...not ;-)

    I'm glad you figured it out!

    /Henrik.

Similar Threads

  1. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  2. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 17:07
  3. Pulsin to pulsout with up than 4MHZ crystal ?
    By RCtech in forum mel PIC BASIC
    Replies: 5
    Last Post: - 18th May 2006, 19:23
  4. Output PIC module
    By freelancebee in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th September 2005, 20:10
  5. Help Quick Need to make code smaller
    By Programmednew in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 25th January 2005, 03:46

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts