In: Electrical Engineering
A PIC C code for Interfacing Ultrasonic sensor using
PIC C 18F8722 microcontroller
#define trig RB0
#define echo RB1
#include <xc.h>
// Control Pin Declarations
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = ON
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP =
OFF
int t,S;
int main()
{
TRISB0 = 0; //make trigger pin as output
pin
TRISB1 = 1; //make echo pin as input
pin
T1CON=0x20;
while(1)
{
TMR1H =0; TMR1L =0;
//clear the timer bits
trig= 1;
__delay_us(10);
trig = 0;
while (echo==0);
TMR1ON = 1;
while (echo==1);
TMR1ON = 0;
t = (TMR1L |
(TMR1H<<8)); // time take for
receiving echo
S =
(0.0272*t)/2; // Distance measured
}
return 0;
}