#include
#include<1602.c>
#include
uchar i;
void delay(uint x)
{
uchar i;
while(x--)
for(i=120;i>0;i--);
}
void init()
{
LCD_init();
u_wave_init();
for(i=0;i<16;i++)
{
write_com(0x80+i);
write_data(tab0[i]);
}
for(i=0;i<16;i++)
{
write_com(0x80+0x40+i);
write_data(tab1[i]);
}
}
void main()
{
init();
while(1)
{
u_wave();
show(handle());
delay(100);
}
}
/************u-wave.c的头文件“u-wave.c“*******************/
sbit trig=P3^1;
sbit echo=P3^2;
sbit fm=P2^3;
uint TH0_1,TL0_1,T,space_2,space_3;
float space_1;
uchar code tab0[]=" space ";
uchar code tab1[]=" cm ";
void delayus()
{
uchar x;
for(x=100;x>0;x--);
}
void u_wave_init()
{
trig=0;
TMOD=0x01;
TH0=0;
TL0=0;
ET0=1;
TR0=0;
EX0=1;
IT0=1;
EA=0;
}
uint handle()
{
uchar i;
T=(TH0_1<<8)|TL0_1;
space_1=0.01853*T;
space_2=space_1;
if(space_2<500)
{
space_3=space_2;
}
else
{
i++;
if(i==2)
{
i=0;
space_3=9999;
fm=~fm;
}
}
return space_3;
}
void u_wave()
{
trig=1;
delayus();
trig=0;
while(echo==0);
EA=1;
TR0=1;
TH0=0;
TL0=0;
}
void time0() interrupt 1
{
TH0=0;
TL0=0;
}
void int0() interrupt 0
{
TR0=0;
TH0_1=TH0;
TL0_1=TL0;
}
/************1602的头文件“1602.c“*******************/
#define uint unsigned int
#define uchar unsigned char
sbit RS=P1^0;
sbit W=P1^1;
sbit EN=P2^5;
uchar dat;
#define w_com RS=0;
#define w_data RS=1;
#define read W=1;
#define write W=0;
uchar N1,N2,N3,N4,N5,N6,choose;
void delay1602(uint x)
{
uchar i;
while(x--)
for(i=120;i>0;i--);
}
void write_com(uchar com)
{
w_com;
P0=com;
delay1602(1);
EN=1;
delay1602(1);
EN=0;
}
void write_data(uchar date)
{
w_data;
P0=date;
delay1602(1);
EN=1;
delay1602(1);
EN=0;
}
void LCD_init()
{
EN=0;
write;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void show(unsigned long space)
{
uchar i;
N1=space/100000;
N2=space%100000/10000;
N3=space%100000%10000/1000;
N4=space%100000%10000%1000/100;
N5=space%100000%10000%1000%100/10;
N6=space%100000%10000%1000%10;
for(i=0;i<6;i++)
{
switch(i)
{
case 0:choose=N1;break;
case 1:choose=N2;break;
case 2:choose=N3;break;
case 3:choose=N4;break;
case 4:choose=N5;break;
case 5:choose=N6;break;
}
write_com(0x80+0x43+i);
write_data(0x30+choose);
}
}