Microcontroller › 8051 › Interfacing AT89S51 with ultrasonic sensor and LCD › Use this Function
January 30, 2018 at 6:29 pm
#14704
Shirsendu Das
Participant
#this function will only work on 8051 @12MHz
unsigned int get_distance()
{
unsigned int time;
trig=1;trig=1;trig=1;trig=1;trig=1; // 5us /
TMOD=0x01; // 2us / use timer0 16 bit mode
TH0=0x00; // 1us / clear reg
TL0=0x00; // 1us / clear reg
trig=0; // 1us / Total(5+2+1+1+1)=10us
while(!echo); // Wait until echo beome high
TR0=1; // Start the Timer
while(echo); // Wait until echo become low
TR0=0; // Stop the timer
time=((TH0<<+TL0)/58; // Load timer value to register. 58us per cm.
return time;
}