#include<reg52.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit Trig = P2^0;
sbit Echo = P2^1;
sbit dula = P2^6;
sbit wela = P2^7;
uint i;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay_10us();
void delay_ms(uint t);
void init_t0();
void display();
uint fh();
void main()
{
init_t0();
while(1)
{
display();
}
}
void delay_10us()
{
uchar x=3;
while(x--);
}
void delay_ms(uint t)
{
uint x,y;
for(x=t;x>0;x--)
for(y=120;y>0;y--);
}
void init_t0()
{
TMOD = 0X01;
TH0 = 0;
TL0 = 0;
EA = 1;
ET0 = 1;
// TR0 = 1;
}
void display()
{
uint j;
uchar ge,shi,bai;
j=fh();
if(j>=20)
{
P1=0xfe;
}
else
P1=0xff;
bai=j/100;
shi=j%100/10;
ge=j%10;
wela=1;
P0=0xdf;
wela=0;
dula=1;
P0=table[ge];
dula=0;
delay_ms(1);
wela=1;
P0=0xef;
wela=0;
dula=1;
P0=table[shi];
dula=0;
delay_ms(1);
wela=1;
P0=0xf7;
wela=0;
dula=1;
P0=table[bai];
dula=0;
delay_ms(1);
}
uint fh()
{
TR0=0;
TH0=0;
TL0=0;
Trig=1;
delay_10us();
Trig=0;
delay_10us();
delay_10us();
delay_10us();
while(!Echo);
TR0=1;
while(Echo);
TR0=0;
i=(TH0*256+TL0)/58;
return i;
}
void t0() interrupt 1
{
TH0=0;
TL0=0;
} |