[/code]"> [/code]"> [/code]">
[code]#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);
}
}[/code]
JLCPCB – Prototype 10 PCBs for $2 (For Any Color)
China’s Largest PCB Prototype Enterprise, 600,000+ Customers & 10,000+ Online Orders Daily
How to Get PCB Cash Coupon from JLCPCB: https://bit.ly/2GMCH9w