#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit trig=P2^0; //
sbit echo=P3^2;
sbit dula=P2^6;
sbit wela=P2^7;
uint time;
uchar s,bai,shi,ge;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void delay20us(void) //误差 -0.46875us
{
unsigned char a,b;
for(b=3;b>0;b--)
for(a=1;a>0;a--);
}
void display(uint temp)
{
bai=temp%1000/100;
shi=temp%100/10;
ge=temp%10;
dula=1;
P0=table[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfe;
wela=0;
delay(5);
dula=1;
P0=table[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);
dula=1;
P0=table[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(5);
}
void main()
{
trig=0;
EA=1;
ET0=1;
TH0=0;
TL0=0;
TMOD=0X01;
while(1)
{
trig=1;
delay20us();
trig=0;
while(!echo);
TR0=1;
while(echo);
TR0=0;
time=TH0*256+TL0;
TH0=0;
TL0=0;
s=time/58;
display(s);
}
}
void TO() interrupt 1
{
TH0=0;
TL0=0;
}
|