Ultrasonic [HC-SR04] + PIC16F877A


Closed Thread
Results 1 to 8 of 8

Hybrid View

  1. #1
    Join Date
    Oct 2011
    Posts
    54


    Did you find this post helpful? Yes | No

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    The PortA pins are analogue and you need to set them to digital with the ADCON1 register
    ADCON1 = %00000110 - see datasheet

    Or you could use PortB for trig and echo

    Phil

  2. #2
    Join Date
    Jun 2012
    Posts
    6


    Did you find this post helpful? Yes | No

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    Quote Originally Posted by Sherbrook View Post
    The PortA pins are analogue and you need to set them to digital with the ADCON1 register
    ADCON1 = %00000110 - see datasheet

    Or you could use PortB for trig and echo

    Phil
    thanks

    but don't work


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


    Did you find this post helpful? Yes | No

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    You will also need to change your oscillator to a higher frequency for two reasons:
    1. PAUSEUS min time is 24us at 4MHz
    2. PULSIN resolution is 10us at 4MHz

    I would suggest changing your crystal or resonator to 20MHz so PAUSEUS 10 will work and PULSIN resolution will be 2us.
    Then try your modified code to see if it works:

    Code:
    PORTA = %00000000
    TRISA = %00000010       ' Bit 1 set as input
    PORTB = %00000000
    TRISB = %00000000
    
    ADCON0.0 = 0            ' A/D module disabled
    
    define osc 20           
    INCLUDE "modedefs.bas"
    
    w var word
    r var word
    a var word
    tr var PORTA.1 'Trig pin
    ec var PORTA.2 ' Echo pin
    
    w = 0
    low tr
    
    Main :
        high tr
        pauseus 10          ' Minimum 3us at 20MHz
        low tr
        pulsin ec,1,w
        r = w/29            ' Divide by 29, PULSIN resolution is 2us. 1 count every 2us        
        if r < 10 then
        PORTB = %11111111   ' Turn ON all PORTB outputs
        pause 200
        PORTB = %00000000   ' Turn OFF all PORTB outputs
        endif
    goto main
    end
    Last edited by LinkMTech; - 17th December 2012 at 17:08. Reason: typo/clarity
    Louie

Similar Threads

  1. pic16f690 and ultrasonic tranducers
    By Nyaberi in forum Code Examples
    Replies: 1
    Last Post: - 3rd September 2012, 16:23
  2. Ultrasonic range meter
    By Crista in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th June 2008, 18:01
  3. Replies: 9
    Last Post: - 4th February 2008, 19:12
  4. ultrasonic sensor
    By PoTeToJB in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2006, 20:26
  5. SRF08 Ultrasonic Rangefinder
    By rad in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 25th July 2003, 21: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