0% found this document useful (0 votes)
125 views3 pages

8051 Ultrasonc Code

This document describes code for interfacing an ultrasonic HC-SR04 module with an 8051 microcontroller to measure distance. It initializes timers and triggers the ultrasonic module to send pulses. It then measures the echo pulse return time using timers to calculate the distance using the speed of sound. The distance is displayed on an LCD and outputs are set based on the distance and states of light sensors and switches.

Uploaded by

NelarapuMahesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views3 pages

8051 Ultrasonc Code

This document describes code for interfacing an ultrasonic HC-SR04 module with an 8051 microcontroller to measure distance. It initializes timers and triggers the ultrasonic module to send pulses. It then measures the echo pulse return time using timers to calculate the distance using the speed of sound. The distance is displayed on an LCD and outputs are set based on the distance and states of light sensors and switches.

Uploaded by

NelarapuMahesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*

Find distance of an Object by interfacing Ultrasonic HC-SR04 module with


8051(AT89S52)
http://www.electronicwings.com
*/
#include<reg52.h>
#include <stdio.h>
#include <LCDDISPLAY.h>
#include <math.h>

#define sound_velocity 34300 /* sound velocity in cm per second */

#define period_in_us pow(10,-6)


#define Clock_period 1.085*period_in_us /* period for clock cycle of 8051*/

sbit Trigger_pin=P1^0; /* Trigger pin */


sbit Echo_pin=P1^1; /* Echo pin */
sbit ldr=P1^2;
sbit sw1=P1^3;
sbit sw2=P1^4;
sbit sw3=P1^5;
sbit sw4=P3^1;
sbit sw5=P3^2;

sbit v1=P2^0;
sbit v2=P2^1;
sbit v3=P2^2;
sbit v4=P2^3;
sbit v5=P2^4;
sbit v6=P2^5;
sbit v7=P2^6;

int distance;
void Delay_us()
{
TL0=0xF5;
TH0=0xFF;
TR0=1;
while (TF0==0);
TR0=0;
TF0=0;
}

void init_timer(){
TMOD=0x01; /*initialize
Timer*/
TF0=0;
TR0 = 0;
}

void send_trigger_pulse(){
Trigger_pin= 1; /* pull trigger pin HIGH */
Delay_us(); /* provide 10uS Delay*/
Trigger_pin = 0; /* pull trigger pin LOW*/
}

void main()
{
float distance_measurement, value;
unsigned char distance_in_cm[10];
lcd_init(); /* Initialize 16x2 LCD */
msgdisplay("DISTANCE:");
buz=1;
init_timer(); /* Initialize Timer*/

while(1)
{
send_trigger_pulse(); /* send trigger pulse of 10us */

while(!Echo_pin); /* Waiting for Echo */


TR0 = 1; /* Timer Starts */
while(Echo_pin && !TF0); /* Waiting for Echo goes LOW */
TR0 = 0; /* Stop the timer */

/* calculate distance using timer */


value = Clock_period * sound_velocity;
distance = (TL0|(TH0<<8)); /* read timer register for time count */
distance = (distance*value)/2.0; /* find distance(in cm) */

sprintf(distance_in_cm, "%.2f", distance_measurement);


// LCD_String_xy(2,1,distance_in_cm); /* show distance on 16x2 LCD */
//LCD_String(" cm ");
lcdcmd(0xc0);
lcddata(distance/1000+48);
lcddata((distance/100)%10+48);
lcddata(((distance/10)%10)+48);
lcddata((distance%10)+48);

if(distance<50)
v1=0;
if(distance>20)
v1=1;
if((ldr==1)&&(i==0))
{
v2=0;
i++;
}
if(ldr==0)
{
v2=1;
i=0;
}
if(!sw1)
{
v3=0;
while(!sw1);
v3=1;
}
if(!sw2)
{
v4=0;
while(!sw2);
v4=1;
}
if(!sw3)
{
v5=0;
while(!sw3);
v5=1;
}
if(!sw4)
{
v6=0;
while(!sw4);
v6=1;
}
if(!sw5)
{
v7=0;
while(!sw5);
v7=1;
}

delay(500);
}
}

You might also like