#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit trig=P3^1;
sbit echo=P3^2;
sbit ge=P2^1;
sbit shi=P2^2;
sbit bai=P2^3;
uint time,s;
uchar code digitron[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void Delay2ms()
{
uchar i, j;
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
void Delay20us()
{
uchar i;
_nop_();
i = 52;
while (--i);
}
void Timer0Init()
{
TMOD=0x01;
TL0=0;
TH0=0;
TR0=0;
}
void xianshi()
{
uchar a,b,c;
a=s/100;
b=s%100/10;
c=s%10;
P1=digitron[c];
ge=1;
shi=0;
bai=0;
Delay2ms();
P1=digitron[b];
ge=0;
shi=1;
bai=0;
Delay2ms();
P1=digitron[a];
ge=0;
shi=0;
bai=1;
Delay2ms();
}
void cout()
{
trig=1;
Delay20us();
trig=0;
while(!echo);
TR0=1;
while(echo);
TR0=0;
time=TH0*256+TL0;
s=(time*1.7)/100;
TH0=0;
TL0=0;
}
void main()
{
Timer0Init();
while(1)
{
cout();
xianshi();
}
}
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit trig=P3^1;
sbit echo=P3^2;
sbit ge=P2^1;
sbit shi=P2^2;
sbit bai=P2^3;
uint time,s;
uchar code digitron[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
void Delay2ms()
{
uchar i, j;
i = 22;
j = 128;
do
{
while (--j);
} while (--i);
}
void Delay20us()
{
uchar i;
_nop_();
i = 52;
while (--i);
}
void Timer0Init()
{
TMOD=0x01;
TL0=0;
TH0=0;
TR0=0;
}
void xianshi()
{
uchar a,b,c;
a=s/100;
b=s%100/10;
c=s%10;
P1=digitron[c];
ge=1;
shi=0;
bai=0;
Delay2ms();
P1=digitron[b];
ge=0;
shi=1;
bai=0;
Delay2ms();
P1=digitron[a];
ge=0;
shi=0;
bai=1;
Delay2ms();
}
void cout()
{
trig=1;
Delay20us();
trig=0;
while(!echo);
TR0=1;
while(echo);
TR0=0;
time=TH0*256+TL0;
s=(time*1.7)/100;
TH0=0;
TL0=0;
}
void main()
{
Timer0Init();
while(1)
{
cout();
xianshi();
}
}