Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Interfacing AT89S51 with ultrasonic sensor and LCD

40 posts / 0 new
Last post
zulnorain
zulnorain's picture
Offline
Last seen: 4 years 7 months ago
Joined: 17/01/2013 - 16:44
Interfacing AT89S51 with ultrasonic sensor and LCD

I want to make a project to measure distance using ultrasonic sensor HC-S04 and LCD to display the distance in cm. I use AT89S51, the trigger pin for ultrasonic sensor is P3.5 and the echo pin is P3.2. The echo pin is connected to P3.2 because I use interrupt so that whenever there is a HIGH it will activate timer 0 and the duration of HIGH condition (in microsecond) will be converted to cm (divided by 59). I already make sure that the ultrasonic sensor works well. The problem is the LCD always display output the same number 45. It is not varying although the distance between object and ultrasonic sensor change. Can anyone help me to solve this problem?

 

#include<REGX51.h>
#include<intrins.h>// for using _nop_() function
#define port P2
#define dataport P0 
sfr16 DPTR =0x82;
sbit trig=P3^5;
sbit rs=port^0;
sbit rw=port^1;
sbit e=port^2;
 
 
void delay(unsigned int msec)
{
int i,j;
for(i=0;i<msec;i++)
    for(j=0;j<1275;j++);
}
 
void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
return;
}
 
void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
return;
}
 
void lcd_data_string(unsigned char *str) // Function to send string to LCD
{
int i=0;
while(str[i]!='\0')
{
  lcd_data(str[i]);
  i++;
  delay(1);
}
return;
}
 
void send_pulse(void) //to generate 10 microseconds delay
{
TH0=0x00;TL0=0x00; 
 trig=1;
 _nop_();_nop_();_nop_();_nop_();_nop_();
 _nop_();_nop_();_nop_();_nop_();_nop_();
 trig=0;
    
} 
 
unsigned int get_range(void)
{
 int range=0;
 int s;
 send_pulse();
 while(!INT0);//         in sake of these lines you can generate a delay of 40 Milli seconds=40000 micro
    while (INT0);//         seconds
 DPH=TH0;DPL=TL0;  
 TH0=0xFF;TL0=0xFF;
 lcd_cmd(0x81);
 delay(2);
 lcd_data_string("output:");
 lcd_cmd(0x8a);
 if(DPTR<35000)
 {//actually you need to use 38000 but the sensor may not work at higher levels 
  range=DPTR/59;
  s=range/100;
  range=range%100;
  if(s!=0)
  {
  lcd_data(s+48);
  }
  else
  {
  lcd_cmd(0x06);
  s=range/10;
  range=range%10;
  lcd_data(s+48);
  lcd_data(range+48);
  lcd_data(' ');
  }
 }
 else 
 {
 range=0; // indicates that there is no obstacle in front of the sensor 
 lcd_cmd(0x06);
 lcd_data(0);
 }
 return range;
}
 
void main()
{
  lcd_cmd(0x38);
  lcd_cmd(0x0c);
  delay(2);
  lcd_cmd(0x01);
  delay(2);
  lcd_cmd(0x81);
  delay(2);
  lcd_data_string("start");
  delay(20);
  TMOD=0x09;//timer0 in 16 bit mode with gate enable
  TR0=1;//timer run enabled
  TH0=0x00;TL0=0x00;
 
  P3=0x04;//setting pin P3.2 
     
  
 while(1)
 { get_range();
   delay(2);   
 }
}

 

nidhin.k
nidhin.k's picture
Offline
Last seen: 2 years 3 months ago
Joined: 14/01/2013 - 16:43
 Try this code..... it will

 

Try this code..... it will work....
 
 
#include<reg52.h>
 
#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On  0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define Data_Port P0
 
sbit Lcd_rs = P2^7;
sbit Lcd_rw = P2^6;
sbit Lcd_en = P2^5;
 
sbit RX= P3^2; 
sbit TX= P3^5;
 
void Distance_Measure();
void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Lcd8_Decimal3(unsigned char,unsigned char);
void Lcd8_Decimal4(unsigned char,unsigned int);
void Delay(unsigned int);
void del();  
 
unsigned char i,inch1,inch2,result,obj_distance,rept,sec,j,a=0,c=0,kc,cu=0xc0,l,e[20],num[20],n,v[20];
unsigned long int count=0,count1=0;
 
void main()
{
 
EA=1;
ET0=1;
TMOD=0x01;
TH0=TL0=0x00;
TR0=1;
relay2=0;
Lcd8_Init();Delay(65000); 
    Lcd8_Display(0x80,"Ultrasonic      ",16);
Lcd8_Display(0xc0,"                ",16);
Delay(65000);Delay(65000);
Lcd8_Display(0xC0,"Distance:       ",16);
 
while(1)
{
 
Distance_Measure();
 
}
}
 
 
void Distance_Measure()
{
count=count1=0;
TH0=0x00;TL0=0x00; 
TR0=0;
for(i=0;i<10;i++)         
{
TX=0;           
Delay(1000);
TX=1;           
Delay(10);
TX=0;   
while(RX==0);
TR0=1; 
while(RX==1);
TR0=0;
count=TH0<<8;      
count=count|TL0;    
TH0=TL0=0x00; 
count1=count1+count;
Delay(5000);        
}
count=count1/10;
inch1=count%10000/1000;
inch2=count%1000/100;
result=(inch1*10)+inch2;
 
obj_distance=result-2;  
Lcd8_Decimal3(0XCb,obj_distance); 
 
}
 
void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}
 
void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
 
void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
 
Data_Port=lr; // Data 
Lcd_en=Lcd_rs=1;
Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
 
void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
 
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
  }
}
 
void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
 
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
 
Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}
 
 
void Lcd8_Decimal3(unsigned char com,unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
 
Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
 
Lcd8_Write(com,Lcd_h+0x30);
Lcd8_Write(com+1,Lcd_t+0x30);
Lcd8_Write(com+2,Lcd_o+0x30);
}
 
 
 
void Delay(unsigned int del)
{
while(del--);
}
 
 
nishant
nishant's picture
Offline
Last seen: 5 months 3 weeks ago
Joined: 02/10/2013 - 22:19
thanks

very nice..

 

Shirsendu Das
Shirsendu Das's picture
Offline
Last seen: 3 weeks 13 hours ago
Joined: 01/08/2015 - 18:22
please explain
Please explain the calculation part, And why you use for loop 10times. And the delay function.)????!!! void Distance_Measure() { count=count1=0; TH0=0x00;TL0=0x00; TR0=0; for(i=0;i
abhishek
abhishek's picture
Offline
Last seen: 1 year 3 months ago
Joined: 25/01/2016 - 14:46
ultersonic code for ARM7

hi...

this is abhishek shettar,plz help me i want to ARM7 code for ultersonic sensor...if u have send me

my email id is:abhi17shettar@gmail.com

Sarika Devkar
Sarika Devkar's picture
Offline
Last seen: 7 months 4 weeks ago
Joined: 27/11/2015 - 16:00
THANK YU..

THANK YU..

Karina
 Karina's picture
I want the same program using
I want the same program using 8051 microcontroller only... Plz help me out...
AJISH ALFRED
AJISH ALFRED's picture
Offline
Last seen: 2 years 10 months ago
Joined: 10/01/2012 - 12:10
I suspect the working of this

I suspect the working of this function.

 

void send_pulse(void) //to generate 10 microseconds delay
{
TH0=0x00;TL0=0x00; 
 trig=1;
 _nop_();_nop_();_nop_();_nop_();_nop_();
 _nop_();_nop_();_nop_();_nop_();_nop_();
 trig=0;
    
} 

How can you make sure that this code will generate exactly 10 microsecond delay? It depends on your crystal frequency and how your compiler compiles the code.

Suggest you use a timer for that also

zulnorain
zulnorain's picture
Offline
Last seen: 4 years 7 months ago
Joined: 17/01/2013 - 16:44
hey ,nidhin.k can you please

hey ,

nidhin.k can you please explain the code ?? and in what units the range is displayed(cm/m)

nidhin.k
nidhin.k's picture
Offline
Last seen: 2 years 3 months ago
Joined: 14/01/2013 - 16:43
hai zulnorain.....      in

hai zulnorain.....

 

 

    in that program, i just high low the transitter pin and waiting for the  recieve high low pulse.

during that time i will on the timer and counter for calulating the receieve timing. the values in TH0 and TL0 will give the exact timing.

 

after some calculation it will convert it to inch.

 

 

u can check and caliber it into cm....

zulnorain
zulnorain's picture
Offline
Last seen: 4 years 7 months ago
Joined: 17/01/2013 - 16:44
nidhin.k thanx for the code

nidhin.k thanx for the code buddy its working fine....now if i want to introduce leds and buzzer in it how will that be done?

for example 4 red leds (P0^0 to P0^3) glow when distance is <= 20 inches and buzzer (P1.3) is sound...if distance > 20

4 green leds glow(p0.4 to p0.7)

 

please help...

nidhin.k
nidhin.k's picture
Offline
Last seen: 2 years 3 months ago
Joined: 14/01/2013 - 16:43
hai zulorain....  u just edit

hai zulorain....

 

 

u just edit that code...

 

 

sbit red1=P0^0;

sbit red2=P0^1;

sbit red3=P0^2;

sbit red3=P0^3;

 

sbit green1=P0^4;

sbit green2=P0^5;

sbit green3=P0^6;

sbit green4=P0^7;

sbit buzzer=P1^3;

 

 

 

if(obj_distance<20){red1=red2=red3=red4=1;green1=green2=green3=green4=0;buzzer=1;}

else{red1=red2=red3=red4=0;green1=green2=green3=green4=1;buzzer=0;}

 

the conditional statements are placed in while condition.

pranali
pranali's picture
Offline
Last seen: 6 months 2 weeks ago
Joined: 04/03/2017 - 18:16
i want to interface buzzer
i want to interface buzzer and ultrasonic with 8051 can u pls hlp....
zulnorain
zulnorain's picture
Offline
Last seen: 4 years 7 months ago
Joined: 17/01/2013 - 16:44
hey nidhin.k, if i introduce

hey nidhin.k,

 

if i introduce dc motor (with L293D ic) in this project how can i control motor speed using ultrasonic sensor.?

so at a certain say (40 inches) motor speed reduces and at 20 inches motor stops.?

nidhin.k
nidhin.k's picture
Offline
Last seen: 2 years 3 months ago
Joined: 14/01/2013 - 16:43
hai zulnorain....  u can

hai zulnorain....

 

 

u can control the speed of a DC motor... by using PWM signal.... 

 

try... and if any problem occur.. post here....

 

all the best.....

 

 

 

zulnorain
zulnorain's picture
Offline
Last seen: 4 years 7 months ago
Joined: 17/01/2013 - 16:44
hey nidhin.k can you explain

hey nidhin.k can you explain this section of the code:

 

void Distance_Measure()
{
count=count1=0;
TH0=0x00;TL0=0x00; 
TR0=0;
for(i=0;i<10;i++)         
{
TX=0;           
Delay(1000);
TX=1;           
Delay(10);
TX=0;   
while(RX==0);
TR0=1; 
while(RX==1);
TR0=0;
count=TH0<<8;      
count=count|TL0;    
TH0=TL0=0x00; 
count1=count1+count;
Delay(5000);        
}
count=count1/10;
inch1=count%10000/1000;
inch2=count%1000/100;
result=(inch1*10)+inch2;
 
obj_distance=result-2;  
Lcd8_Decimal3(0XCb,obj_distance); 
 
}
B V RAjeswararao
B V RAjeswararao's picture
Offline
Last seen: 2 years 11 months ago
Joined: 03/03/2013 - 16:54
can u help me the ultrasonic

can u help me the ultrasonic code using serial communication

 

Electronics king
Electronics king's picture
Offline
Last seen: 2 years 4 weeks ago
Joined: 23/08/2015 - 12:58
HC-SR04 Interfacing with 8051

may be openelectronicsproject.blogspot.com help you 

rohit pandey
rohit pandey's picture
Offline
Last seen: 2 years 11 months ago
Joined: 20/03/2013 - 14:10
nidhin i need ur help in

nidhin i need ur help in making a similar project but i dont have to display it i just have to convert the range into vibration like maximum range gives minimum vibrations and min range gives maximum vibration using pwm can you provide somthing on the it will be highly appriciable i am using 6 volt dc motor using maimum 5 volt range as entier circuit will be powered by a 9 volt battry and i am using hc sr04 ultrasonic range module and atmel 89s51 as processor please help as i am new to c programming and dont know how to do all this thanks in advance

rohit pandey
rohit pandey's picture
Offline
Last seen: 2 years 11 months ago
Joined: 20/03/2013 - 14:10
and the cicuit diagram if

and the cicuit diagram if possible for you or zulhoriz please provide the schematics for connection of sensor to micro and to dc motor thnx in advance

rohit pandey
rohit pandey's picture
Offline
Last seen: 2 years 11 months ago
Joined: 20/03/2013 - 14:10
 #include<REGX51.h>#include<i

 

#include<REGX51.h>
#include<intrins.h>// for using _nop_() function
sfr16 DPTR =0x82;
sbit trig=P3^1;
void send_pulse(void) //to generate 10 microseconds delay
{
TH0=0x00;TL0=0x00; 
 trig=1;
 _nop_();_nop_();_nop_();_nop_();_nop_();
 _nop_();_nop_();_nop_();_nop_();_nop_();
 trig=0;
    
 
unsigned char get_range(void) 
{
 unsigned char range;
 send_pulse();
    while(!INT0);//         in sake of these lines you can generate a delay of 40 Milli seconds=40000 micro 
    while (INT0);//         seconds
 DPH=TH0;DPL=TL0; 
 TH0=0xFF;TL0=0xFF;
 if(DPTR<35000)//actually you need to use 38000 but the sensor may not work at higher levels 
  range=DPTR/59;
  else
  range=0; // indicates that there is no obstacle in front of the sensor 
  return range; 
}
void main()
{
  TMOD=0x09;//timer0 in 16 bit mode with gate enable
  TR0=1;//timer run enabled
  TH0=0x00;TL0=0x00;
 
 P1=0xff;
  P3|=0x04;//setting pin P3.2    
         
}
 
this is my code please help me in howing distance on led as above mentioned led code it is giving me errors please patch them into this 
Hussein
Hussein's picture
Offline
Last seen: 3 years 10 months ago
Joined: 28/10/2012 - 23:19
hello Mr Zulnorain,,, or his

hello Mr Zulnorain,,, 
or his helpful friend,,

can someone email me or paste the last edited full code for ultrasonic with leds and motor ??
 

i really appreciate it @@ thanks so much
Hussein-Yemen
 

 

ggc
ggc's picture
Offline
Last seen: 1 year 2 months ago
Joined: 03/10/2011 - 17:32
send the circuit diagram i

send the circuit diagram i will send u the complete code to u  my mail id is ggcamie@gmail.com

 

aniket r kambli
aniket r kambli's picture
Offline
Last seen: 3 years 11 months ago
Joined: 03/08/2013 - 17:23
           pls help me with

           pls help me with 80c51 ultrasonic distance finder using  hc-sr04 module and led display   assembly code

Robert Wilson
Robert Wilson's picture
Offline
Last seen: 3 years 6 months ago
Joined: 31/10/2013 - 13:32
pls i neen help.Vending

pls i neen help.Vending machines are more common these days and these can be seen all around. The vending machines dispense the drink of your choice based on the item you select. You might have come across these machines dispensing either hot drinks or cool drinks.

            In this task you are expected to design a vending machine that can dispense both hot and cool drinks of you choice. The requirements are as follows,

  • Turn “ON” the machine
  • Select hot or cool drink
  • Select options in the hot drink
  • Select options in the cool drink
  • Select the drink of your choice
  • Display the availability/unavailability of the drink
  • Select options in case of unavailability
  • Option to select another drink (go to main menu)
  • Display the cost of that drink
  • Count the cash credited and display the amount
  • Beep when the amount is paid fully
  • Vending machine dispense the selected item
  • pls i need idea to write the codes. in keli uvision and interfacing with lcd
  •  
ANITHA
ANITHA's picture
Offline
Last seen: 3 years 1 month ago
Joined: 06/02/2014 - 15:47
#include<reg52.h> #define
#include<reg52.h>
 
#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On  0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01
#define Data_Port P0
 
sbit Lcd_rs = P2^7;
sbit Lcd_rw = P2^6;
sbit Lcd_en = P2^5;
 
sbit RX= P3^2; 
sbit TX= P3^5;
 
void Distance_Measure();
void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Lcd8_Decimal3(unsigned char,unsigned char);
void Lcd8_Decimal4(unsigned char,unsigned int);
void Delay(unsigned int);
void del();  
 
unsigned char i,inch1,inch2,result,obj_distance,rept,sec,j,a=0,c=0,kc,cu=0xc0,l,e[20],num[20],n,v[20];
unsigned long int count=0,count1=0;
 
void main()
{
 
EA=1;
ET0=1;
TMOD=0x01;
TH0=TL0=0x00;
TR0=1;
relay2=0;
Lcd8_Init();Delay(65000); 
    Lcd8_Display(0x80,"Ultrasonic      ",16);
Lcd8_Display(0xc0,"                ",16);
Delay(65000);Delay(65000);
Lcd8_Display(0xC0,"Distance:       ",16);
 
while(1)
{
 
Distance_Measure();
 
}
}
 
 
void Distance_Measure()
{
count=count1=0;
TH0=0x00;TL0=0x00; 
TR0=0;
for(i=0;i<10;i++)         
{
TX=0;           
Delay(1000);
TX=1;           
Delay(10);
TX=0;   
while(RX==0);
TR0=1; 
while(RX==1);
TR0=0;
count=TH0<<8;      
count=count|TL0;    
TH0=TL0=0x00; 
count1=count1+count;
Delay(5000);        
}
count=count1/10;
inch1=count%10000/1000;
inch2=count%1000/100;
result=(inch1*10)+inch2;
 
obj_distance=result-2; 
Lcd8_Decimal3(0XCb,obj_distance); 
 
}
 
void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function set
Lcd8_Command(0x06); //entry mode set
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}
 
void Lcd8_Command(unsigned char com)
{
Data_Port=com;
Lcd_en=1;
Lcd_rs=Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
 
void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
 
Data_Port=lr; // Data 
Lcd_en=Lcd_rs=1;
Lcd_rw=0;
Delay(125);
Lcd_en=0;
Delay(125);
}
 
void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
 
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
  }
}
 
void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;
 
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
 
Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}
 
 
void Lcd8_Decimal3(unsigned char com,unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;
 
Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;
 
Lcd8_Write(com,Lcd_h+0x30);
Lcd8_Write(com+1,Lcd_t+0x30);
Lcd8_Write(com+2,Lcd_o+0x30);
}
 
 
 
void Delay(unsigned int del)
{
while(del--);
}
 
i am not getting output for this code.here u r using timer 1 pin is tx=P3^5; but tx pin will always high then u r using rx=P3^2;
it is low after detecting the object it will be high.ET0=1;u r not using the timer 1 pin then how the interrupt will enable................
              while i am debugging this program i got access violation error at lcd int();....can you please explain for me sir i cannot understand sir.
pls sir
 
ANITHA
ANITHA's picture
Offline
Last seen: 3 years 1 month ago
Joined: 06/02/2014 - 15:47
i need c code for measuring

i need c code for measuring the distance by using ultrasonic sensor...............can u pls send me 

ANITHA
ANITHA's picture
Offline
Last seen: 3 years 1 month ago
Joined: 06/02/2014 - 15:47
y u r enabling the interrupt 

y u r enabling the interrupt

 

MOHIT SARAF
MOHIT SARAF's picture
Offline
Last seen: 3 years 6 months ago
Joined: 13/03/2014 - 02:33
how can i generate o/p using

how can i generate o/p using buzzzer instead of led where buzzer frequency depends on the range usin c code

sai
sai's picture
Offline
Last seen: 3 years 5 months ago
Joined: 16/12/2013 - 19:24
why we will use sbit is that

why we will use sbit is that mandatory?

boominathan
boominathan's picture
Offline
Last seen: 2 months 5 days ago
Joined: 20/10/2012 - 12:44
help me ultrasonic with 8051

sir

   i need ultrasonic sensor and 8051 using object identifier. anyone help me

Mayank
Mayank's picture
Offline
Last seen: 2 years 6 months ago
Joined: 19/10/2011 - 10:21
Please!upload the circuit

Please!upload the circuit diagram of above project

 

NIDHI SHARMA
NIDHI SHARMA's picture
Offline
Last seen: 2 years 4 months ago
Joined: 14/05/2015 - 01:31
Sir i need a C code for the

Sir i need a C code for the interfacing of ultrasonic sensor with AT89s51.Pls reply

nitesh sinha
nitesh sinha's picture
Offline
Last seen: 2 years 4 months ago
Joined: 09/03/2015 - 11:23
plz help me to finished this simple project

i am doing distance measurement using at89c51 and ultrasonic sensor but i m not getting anything on the seven-segment display ,i am using three seven-segment display ,the code is given below

ORG 00H
MOV DPTR,#SSDisplay // moves the address of LUT to DPTR
MOV P1,#00000000B // sets P1 as output port
MOV P0,#00000000B // sets P0 as output port
CLR P3.0 // sets P3.0 as output for sending trigger
SETB P3.1 // sets P3.1 as input for receiving echo
MOV TMOD,#00100000B // sets timer1 as mode 2 auto reload timer
MAIN: MOV TL1,#210D // loads the initial value to start counting from
      MOV TH1,#210D // loads the reload value
      MOV A,#00000000B // clears accumulator
      SETB P3.0 // starts the trigger pulse
      ACALL DELAY1 // gives 10uS width for the trigger pulse
      CLR P3.0 // ends the trigger pulse
HERE: JNB P3.1,HERE // loops here until echo is received
BACK: SETB TR1 // starts the timer1
HERE1: JNB TF1,HERE1 // loops here until timer overflows (ie;48 count)
      CLR TR1 // stops the timer
      CLR TF1 // clears timer flag 1
      INC A // increments A for every timer1 overflow
      JB P3.1,BACK // jumps to BACK if echo is still available
      MOV R4,A // saves the value of A to R4
      ACALL DLOOP // calls the display loop
      SJMP MAIN // jumps to MAIN loop
 
DELAY1: MOV R6,#2D // 10uS delay
LOOP1: DJNZ R6,LOOP1
RET     
 
DLOOP: MOV R5,#50D // loads R5 with 100D
BACK1: MOV A,R4 // loads the value in R4 to A
       MOV B,#100D // loads B with 100D
       DIV AB   // isolates the first digit
       SETB P1.0 // activates LED display unit D1
       ACALL DISPLAY // calls DISPLAY subroutine
       MOV P0,A // moves digit drive pattern for 1st digit to P0
       ACALL DELAY   // 1mS delay
       ACALL DELAY
       MOV A,B // moves the remainder of 1st division to A
       MOV B,#10D // loads B with 10D
       DIV AB   // isolates the second digit
       CLR P1.0 // deactivates LED display unit D1
       SETB P1.1 // activates LED display unit D2
       ACALL DISPLAY
       MOV P0,A // moves digit drive pattern for 2nd digit to P0
       ACALL DELAY
       ACALL DELAY
       MOV A,B // moves the remainder of 2nd division to A
       CLR P1.1 // deactivates LED display unit D2
       SETB P1.2 // activates LED display unit D3
       ACALL DISPLAY
       MOV P0,A // moves the digit drive pattern for 3rd digit to P0
       ACALL DELAY
       ACALL DELAY
       CLR P1.2 // deactivates LED display unit D3
       DJNZ R5,BACK1 // repeats the display loop 100 times
       RET
 
DELAY: MOV R7,#250D   // 1mS delay
LABEL2: DJNZ R7,LABEL2
        RET
 
DISPLAY: MOVC A,@A+DPTR // gets the digit drive pattern for the content in A
         RET
 
SSDisplay: DB 0C0H // Hex code to display DISPLAY 0
DB 0F9H // DISPLAY 1
DB 0A2H // DISPLAY 2
DB 0B0H // DISPLAY 3
DB 99H // DISPLAY 4
DB 94H // DISPLAY 5
DB 84H // DISPLAY 6
DB 0F8H // DISPLAY 7
DB 80H // DISPLAY 8
DB 90H // DISPLAY 9
 
END
 
 
Electronics king
Electronics king's picture
Offline
Last seen: 2 years 4 weeks ago
Joined: 23/08/2015 - 12:58
HC-SR04 Interfacing with 8051

you can visit 

Openelectronicsproject.blogspot.com

Nilesh Bhavsar
Nilesh Bhavsar's picture
Offline
Last seen: 1 year 4 months ago
Joined: 24/07/2015 - 10:51
Ultrasonic interfacing using LPC2148

#include /* LPC21xx definitions */ #include "lcd.h" void LCD_Init(void); unsigned char String1[16]={" iblocks "}; unsigned char String2[16]={" ARM LPC2148 "}; unsigned int ultrasonic_read() { int b; T1TCR = 0x01; T1TC=0; T1PR=0; T1PC=0; IO1SET =(1<<16); // trig pin of ultrasonic sensor Delay1(100); IO1CLR =(1<<16); while((IO1PIN &0x00020000)==0); //echo pin of ultrasonic sensor T1TCR = 0x00; //enable timer while((IO1PIN &0x00020000)==1); T1TCR = 0x01; //stop timer b=T1TC; return b; } int main(void) { unsigned int a; PINSEL0 = 0x00000000; // Enable GPIO on all pins PINSEL1 |= 0x15000000; PINSEL2 = 0x00000000; Delay(20); IO0DIR = (1<<22) | (1<<21) | (1<<20) | (1<<19) | (1<<18) | (1<<17) | (1<<16); // Set P0.16, P0.17, P0.18, P0.19, P0.20, P0.21, P0.22 as Output IO1DIR =(1<<16)|(0<<17); LCD_Init(); // Initialize LCD LCD_Command(0x01); Delay(20); LCD_Command(0x80); LCD_String(&String1[0]); LCD_Command(0xC0); LCD_String(&String2[0]); Delay(200); LCD_Command(0x01); LCD_String("value:"); while(1) { a=ultrasonic_read(); LCD_Command(0xc0); LCDWriteInt(a,3); } }

zaid hussam
zaid hussam's picture
Offline
Last seen: 1 year 5 months ago
Joined: 31/03/2016 - 14:10
please , i want the block

please , i want the block circuit and the program  to the ultrasonic finder distance with lcd using at89s51

apoorva
apoorva's picture
Offline
Last seen: 1 year 5 months ago
Joined: 31/03/2016 - 10:15
need code in keil

can someone provide me the code using lcd to display the distance in Keil uvision software ??

prasannakumar
prasannakumar's picture
Offline
Last seen: 2 months 1 week ago
Joined: 04/09/2012 - 12:44
how to measure distance from ultrasonic sensor using lpc2148

hi,

can any body tell me how measure distance using ultasonic sensor HC-SR04 using lpc2148 

 

Hari Prasaath K
Hari Prasaath K's picture
Offline
Last seen: 15 hours 36 min ago
Joined: 20/01/2015 - 23:56
Interfacing Ultrasonic sensor

Ultrasonic sensor uses a technique called ECHO in which you somthing reflecting back after striking from the surface. sensor provides an output signal proportional to distance based on the echo. The sensor here generates a sound vibration in ultrasonic range upon giving a trigger, after that it waits for the sound vibration to return.

steps to calculate distance:

1. Triggering the sensor by pulling up the trigger pin for atleast 12uS.
2. Once echo goes high we get an external interrupt and we are going to start a counter (enabling a counter) in the ISR (Interrupt Service Routine) which is executed right after an interrupt triggered.
3. Once echo goes low again an interrupt is generated, this time we are going to stop the counter (disabling the counter).
4. So for a pulse high to low at echo pin, we have started a counter and stopped it. This count is updated to memory for getting the distance, as we have the width of echo in count now.
5. We are going to do further calculations in the memory to get the distance in cm
6. The distance is displayed on 16x2 LCD display.