EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / can any one explin me about

can any one explin me about

|

Microcontroller › 8051 › MICRO CONTROLLER BASED ULTRASONIC DISTANCE MEASUREMENT SYSTEM › can any one explin me about

April 12, 2014 at 11:40 am #11536
sai
Participant
can any one explin me about this code to measure distance  in mm using 3 pin ultrasonic sensor…plz…
 
 
#include<reg51.h>
#include<intrins.h>   
#include<lcd_busy.h>
#define lcd_port P1
 
 
 
bit rec_flag=0;
bit data_flag=0;
void lcd_dispaly();
void int_char_con(void);
void serial_init();
unsigned char array[5],count=0,dis_count=0;
unsigned char rec=0;
unsigned int dis=0,rot_count=0,ror_flag=0,step=0;
unsigned int dis_array[10],direction_count=0,loop_count=0;
unsigned int THOUSANDS,HUNDREDS, TENS, UNITS, DISTANCE, TEMP_VAL;
////////////////////////////////
 
 
void main()
{
// buzzer=0;
lcd_init();
lcd_init();
serial_init();
serial_init();
message(0x80,”Ultrasonic target”);
delay(500);
init(0x01);
while(1)
{
 
if(data_flag==1)
{
ES=0;
rec_flag=0;
data_flag=0;
DISTANCE=array[1]*256+array[2];
 
lcd_dispaly();
 
 
 
 
 
 
 
delay(5);
count=0;
ES=1;
}
}
}
//////////////////////////
void serial_init()
{
SCON=0X50;         //SM2 SM1 SM0 REN TB8 RB8 TI RI   G C/T M1 M0 G C/T M1 M0
TMOD=0X20; //0010 0000 // 0   0   1   1   0   0   0  0    0  0  1 0 0  0  0 0
TH1=0XFD; //9600
TR1=1;
EA=1;
ES=1;
}
////////////////////////////
void serial(void) interrupt 4
{
if(RI==1)
{
RI=0;
rec=SBUF;
if(rec==0xff)
{
rec_flag=1;
}
 
if(rec_flag==1)
{
array[count++]=SBUF;
if(count==3)
data_flag=1;
}
}
}
/////////////////////////////////////////////
void lcd_dispaly()
{
 
 
int_char_con();
dis_count=0;
}
 
void int_char_con(void)
{
init(0x80);
THOUSANDS = DISTANCE/1000;//to get thousands num
TEMP_VAL = DISTANCE%1000;
HUNDREDS = TEMP_VAL/100; //to get hundreds num
TEMP_VAL %= 100;
TENS = TEMP_VAL/10; //to get tens num
UNITS = TEMP_VAL%10;
 
write_lcd(THOUSANDS+0x30);
write_lcd(HUNDREDS+0x30);
write_lcd(TENS+0x30);
write_lcd(UNITS+0x30);
}
 
 

RSS Recent Posts

  • Buffer design? February 9, 2026
  • ANOTHER OLD PROJECT REDO February 9, 2026
  • BPF February 9, 2026
  • To couple or decouple February 9, 2026
  • Op amp shock February 9, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise