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
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
Re: Ultrasonic [HC-SR04] + PIC16F877A
Quote:
Originally Posted by
Sherbrook
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:
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
Re: Ultrasonic [HC-SR04] + PIC16F877A
Have you set the configuration of the oscillator correct
See datasheet - Special features of the CPU
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
Re: Ultrasonic [HC-SR04] + PIC16F877A
Quote:
Originally Posted by
Sherbrook
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:
Re: Ultrasonic [HC-SR04] + PIC16F877A