PDA

View Full Version : Ultrasonic [HC-SR04] + PIC16F877A



intarapun
- 17th December 2012, 11:02
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&rct=j&q=&esrc=s&source=web&cd=8&cad=rja&ved=0CGAQFjAH&url=http%3A%2F%2Fjaktek.com%2Fwp-content%2Fuploads%2F2011%2F12%2FHC-SR04.pdf&ei=f-3OUIjGDorRrQflzoCIDg&usg=AFQjCNGvwkkPlRVU3B2v7KfGMKRTPYZ4hw&sig2=EzzM-FQYofTuAUG5bxJT5Q&bvm=bv.1355325884,d.bmk

Sherbrook
- 17th December 2012, 13:40
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

intarapun
- 17th December 2012, 16:26
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

:frown:

LinkMTech
- 17th December 2012, 18:06
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:



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

Sherbrook
- 17th December 2012, 18:29
Have you set the configuration of the oscillator correct
See datasheet - Special features of the CPU

Sherbrook
- 17th December 2012, 21:38
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

intarapun
- 18th December 2012, 12:55
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 :biggrin::wink:

ligogeorge
- 20th August 2014, 13:09
Try the following link, it might help you
Interfacing HC-SR04 Ultrasonic Sensor with PIC Microcontroller (http://electrosome.com/hc-sr04-ultrasonic-sensor-pic/)