Remove Text Formatting
Loading...

+ Reply to Thread
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2012
    Posts
    6

    Unhappy Ultrasonic [HC-SR04] + PIC16F877A

    help me Please

    it code don't work

    it's wrong

    -----------------------------------------------------------
    INCLUDE "modedefs.bas"
    define osc 4
    w var word
    r var word
    a var word
    tr var PORTA.0 'Trig pin
    ec var PORTA.1 ' Echo pin
    TRISA = %00000010
    TRISB = %00000000
    PORTB = %00000000
    w = 0
    Main :
    low tr
    high tr
    pauseus 10
    low tr
    pulsin ec,1,w
    r = w/58
    if r < 10 then
    PORTB = %11111111
    pause 200
    endif
    goto main
    end
    ------------------------------------------

    Sorry I bad eng

    data sheet HC-SR04
    https://www.google.co.th/url?sa=t&rc...55325884,d.bmk

  2. #2
    Join Date
    Oct 2011
    Posts
    45

    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

  3. #3
    Join Date
    Jun 2012
    Posts
    6

    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


  4. #4
    Join Date
    Nov 2007
    Location
    West Covina, CA
    Posts
    212

    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

  5. #5
    Join Date
    Oct 2011
    Posts
    45

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    Have you set the configuration of the oscillator correct
    See datasheet - Special features of the CPU

  6. #6
    Join Date
    Oct 2011
    Posts
    45

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    This works on PIC16F873A so should work on PIC16F877A
    Don't forget to pull MCLR high

    @ DEVICE PIC16F873A, HS_OSC 'Xtal OSC
    @ DEVICE PIC16F873A, WDT_OFF 'WDT off
    @ DEVICE PIC16F873A, PWRT_ON 'Power-up timer on
    @ DEVICE PIC16F873A, BOD_OFF 'Brown-out detect off

    ADCON1 = %00000110 'All PORTA digital

    define osc 4

    TRISA = %00000010
    TRISB = %00000000
    PORTB = %00000000

    w var word
    r var word
    a var word

    tr var PORTA.0 'Trig pin
    ec var PORTA.1 'Echo pin

    PORTB = %11111111 'Flash LED on PORTB
    pause 500 'to check PIC running
    PORTB = %00000000

    w = 0
    low tr

    Main :
    high tr
    pauseus 10
    low tr
    pulsin ec,1,w
    r = w/58
    if r < 10 then
    PORTB = %11111111
    else
    PORTB = %00000000
    endif
    pause 200
    goto main
    end

  7. #7
    Join Date
    Jun 2012
    Posts
    6

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    Quote Originally Posted by Sherbrook View Post
    This works on PIC16F873A so should work on PIC16F877A
    Don't forget to pull MCLR high

    @ DEVICE PIC16F873A, HS_OSC 'Xtal OSC
    @ DEVICE PIC16F873A, WDT_OFF 'WDT off
    @ DEVICE PIC16F873A, PWRT_ON 'Power-up timer on
    @ DEVICE PIC16F873A, BOD_OFF 'Brown-out detect off

    ADCON1 = %00000110 'All PORTA digital

    define osc 4

    TRISA = %00000010
    TRISB = %00000000
    PORTB = %00000000

    w var word
    r var word
    a var word

    tr var PORTA.0 'Trig pin
    ec var PORTA.1 'Echo pin

    PORTB = %11111111 'Flash LED on PORTB
    pause 500 'to check PIC running
    PORTB = %00000000

    w = 0
    low tr

    Main :
    high tr
    pauseus 10
    low tr
    pulsin ec,1,w
    r = w/58
    if r < 10 then
    PORTB = %11111111
    else
    PORTB = %00000000
    endif
    pause 200
    goto main
    end
    Oh!! Thanks IT work

    But edit code r = w/58 >> r = ((w*10)/58)

    Thank you very much Sherbrook & LinkMTech

  8. #8
    Join Date
    Aug 2014
    Posts
    1

    Default Re: Ultrasonic [HC-SR04] + PIC16F877A

    Try the following link, it might help you
    Interfacing HC-SR04 Ultrasonic Sensor with PIC Microcontroller

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 : 3

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