reading ultrasonic receve


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    11


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    here is the code i may not be understanding the pulse command the sensor is an hc-sr04 thanks for the help

    define osc 4
    ANSEL = 0 ' set I/O pin to digital
    CMCON0 = 7 ' set comparator off
    osccon = $60
    define osc 4


    input GPIO.4 ' set GPIO.4 to input pin 3
    OUtput GPIO.2 ' set GPIO.0 to output pin 5
    output GPIO.1 ' set GPIO.1 to output pin 6
    sens var GPIO.4
    led var GPIO.2
    trig var GPIO.1
    dist var word


    main:
    gosub distance
    low led
    while dist < 8
    high led
    'pause 500
    'low led
    'pause 500
    wend
    goto main

    distance:
    high trig
    pauseus 10
    low trig
    'pulsout trig,2 ' produces 10us pulse
    pause 50
    pulsin sens,1,dist 'reads echo pulse from sonar
    dist= dist/15 'convert to inches
    pause 100
    return

  2. #2
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    219


    Did you find this post helpful? Yes | No

    Default Re: reading ultrasonic receve

    The PULSIN and PULSOUT commands at 4MHz have a 10us resolution per count.
    PULSIN will time out after 65535 counts or 655ms and return a value of 0 (zero) if it doesn't see the expected pulse.

    Make sure MCLR setting is OFF or pin 4 pulled HIGH with a 10K or something to keep it out of reset.

    Try this code but realize that if the PULSIN times out, the 0 (zero) will turn the LED ON too because it is <8.
    Code:
    ANSEL = 0 ' set I/O pin to digital
    CMCON0 = 7 ' set comparator off
    osccon = $60
    define osc 4
    
    input GPIO.4 ' set GPIO.4 to input pin 3
    OUtput GPIO.2 ' set GPIO.0 to output pin 5
    output GPIO.1 ' set GPIO.1 to output pin 6
    sens var GPIO.4
    led var GPIO.2
    trig var GPIO.1
    dist var word
    
    main:
    GOSUB distance    
        IF dist < 8 THEN
            HIGH led
        ELSE
            LOW led
        ENDIF
    GOTO main    
    
    distance:
        trig = 0            ' start output in LOW state
        PULSOUT trig,1      ' each count = 10us
        PULSIN sens,1,dist  ' will time out in 655ms if nothing received
        dist= dist/15 'convert to inches
        PAUSE 100
    RETURN
    
    END
    If you don't want the LED ON when PULSOUT times out then I would use the SELECT CASE:
    Code:
    ADCON0 = 0      ' A/D module OFF
    ANSEL = 0 ' set I/O pin to digital
    CMCON0 = 7 ' set comparator off
    osccon = $60
    define osc 4
    
    input GPIO.4 ' set GPIO.4 to input pin 3
    OUtput GPIO.2 ' set GPIO.0 to output pin 5
    output GPIO.1 ' set GPIO.1 to output pin 6
    sens var GPIO.4
    led var GPIO.2
    trig var GPIO.1
    dist var word
    
    main:
    GOSUB distance    
        SELECT CASE dist
            CASE 0          ' PULSIN timed out and returns zero, turns LED OFF
                LOW led 
            CASE IS < 8     ' dist <8 inches but not zero, turns ON LED
                HIGH led 
            CASE ELSE       ' dist >8 inches turns LED OFF
                LOW led
        END SELECT
    GOTO main    
    
    distance:
        trig = 0            ' start output in LOW state
        PULSOUT trig,1      ' each count = 10us
        PULSIN sens,1,dist  ' will time out in 655ms if nothing received
        dist= dist/15 'convert to inches
        PAUSE 100
    RETURN
    
    END
    Louie

Similar Threads

  1. Ultrasonic range meter
    By Crista in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2008, 19:01
  2. the ultrasonic SRF04 with pic16f88
    By tur_bot in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 20th March 2008, 18:24
  3. sensor ultrasonic 8051&PIC
    By hd_uni_pro in forum Schematics
    Replies: 1
    Last Post: - 13th September 2006, 13:58
  4. ultrasonic sensor
    By PoTeToJB in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2006, 21:26
  5. SRF08 Ultrasonic Rangefinder
    By rad in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th July 2003, 22:01

Members who have read this thread : 0

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