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
 Re: Ultrasonic [HC-SR04] + PIC16F877A
		 
				
				
				
				
			

Bookmarks