#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD P0
uchar LCDdata;
uchar b,c,d,e,z=0;
long int a=0;
sbit RS=P3^0;
sbit RW=P2^2;
sbit E=P3^1;
sbit ECHO=P2^3;
sbit TRIG=P2^4;
uchar num[13][1]={"0","1","2","3","4","5","6","7","8","9",".","c","m"};
uchar time[10][1] = {"0","0","0","0","0","0","0","0","0","0"};
void delay(uint x)
{
uchar j;
while(x-->0)
{
for(j=0;j<125;j++){;}
}
}
uchar readBF(void)
{
RS=0;
RW=1;
LCD=0xff;
E=1;
LCDdata=LCD;
E=0;
return(LCDdata&0x80);
}
void send_instr(uchar x)
{
while(readBF());
RS=0;
RW=0;
LCD=x;
E=1;
E=0;
}
void writedat(uchar dat)
{
while(readBF());
RS=1;
RW=0;
LCD=dat;
E=1;
;
;
E=0;
}
void initLCD(void)
{
send_instr(0x34);
delay(1);
send_instr(0x30);
delay(1);
send_instr(0x01);
delay(1);
send_instr(0x06);
delay(1);
send_instr(0x0c);
delay(1);
}
void scan(void)
{
send_instr(0x80);
writedat(time[0][0]);
writedat(time[1][0]);
writedat(time[2][0]);
writedat(num[10][0]);
writedat(time[3][0]);
writedat(num[11][0]);
writedat(num[12][0]);
}
void display(void)
{
b=(a/1000);
c=(a/100)%10;
d=(a/10)%10;
e=a%10;
time[0][0] = num[b][0];
time[1][0] = num[c][0];
time[2][0] = num[d][0];
time[3][0] = num[e][0];
}
void main(void){
initLCD(); //清屏
while(1){
TRIG=1;
delay(1);
TRIG=0;
while(ECHO==0);
while(ECHO==1) a++;
delay(30);
a=(34*a*21)/200;
display();
scan();
z=a;
a=0;
delay(200);
}
}