#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit fa=P1^0;
sbit shou=P1^1;
sbit dula=P2^6;
sbit wela=P2^7;
unsigned int time;
unsigned long distance;
uchar shi,ge,bai,t;
bit flag;
void display();
unsigned char code dx[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay1()
{
unsigned int x,y;
for(x=1;x>0;x--)
for(y=110;y>0;y--);
}
//void delay_20us()
// {
// uchar a ;
// for(a=0;a<100;a++);
// }
void delay10(uint us)
{
while(--us)
{
_nop_();
}
}
void main()
{
TMOD=0x11;
TR0=0; //1.085us
TF0=0;
TH0=0;
TL0=0;
shou=0;
TH1=0x3c;
TL1=0xb0; //50ms
EA=1;
ET1=1;
TR1=1;
while(1)
{
display();
if(flag)
{ flag=0;
EA=0;
fa=1;
delay10(10);
fa=0;
while(shou==0);
TR0=1;
TF0=0;
while(shou);
TR0=0;
TF0=0;
time=TH0*256+TL0;
TH0=0;
TL0=0;
distance=time*0.0172;
shi=distance%100/10;
ge=distance%10;
bai=distance/100;
EA=1;
}
}
}
void display()
{
// dula=1;
// P0=dx[n];
// dula=0;
// P0=0xff;
// wela=1;
// P0=0xfe;
// wela=0;
// delay1();
// 第二位
dula=1;
P0=dx[bai];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay1();
////////第三位//////
dula=1;
P0=dx[shi];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay1();
/////第四位/////
dula=1;
P0=dx[ge];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay1();
}
void timer1() interrupt 3
{
TH1=0x3c;
TL1=0xb0; //50ms
t++;
if(t==10)
{
t=0;
flag=1;
}
}
|