#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit TX=P1^0; //脉冲发送
sbit RX=P1^1; //接受
sbit wela=P2^7;
sbit dula=P2^6;
uchar m;
unsigned long juli;
uchar code duan[]={0x3f,0x6,0x5b,0x4f,0x66,0x6d,0x7d,0x27,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //段选码
uchar code wei[]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; //数码管位选码
uchar shuju[6];
/**************************************晶振是12M的**********************************/
void delay20us(void) //误差 0us
{
unsigned char a,b;
for(b=3;b>0;b--)
for(a=1;a>0;a--);
}
void delay2ms(void) //误差 0us
{
unsigned char a,b;
for(b=133;b>0;b--)
for(a=6;a>0;a--);
}
void init()
{
EA=1;
ET1=1;
TMOD=0x11;
TH0=0;
TL0=0;
TH1=(65535-10000)/256;
TL1=(65535-10000)%256;
TR1=1;
}
void display(uchar *p)
{
uchar i;
for(i=0;i<6;i++)
{
dula=1;
P0=duan[*p];
dula=0;
wela=1;
P0=wei;
wela=0;
p++;
delay2ms();
}
}
void timer1() interrupt 3
{
display(shuju);
TH1=(65535-10000)/256;
TL1=(65535-10000)%256;
m++;
if(m==80)
{
m=0;
TX=0;
TX=1;
delay20us();
TX=0;
}
}
void chuli()
{
unsigned long time;
time=TH0*256+TL0;
TH0=0;
TL0=0;
juli=time*1.7/100; //转换成CM显示在数码管上
shuju[5]=juli/100000;
shuju[4]=juli%100000/10000;
shuju[3]=juli%10000/1000;
shuju[2]=juli%1000/100;
shuju[1]=juli%100/10;
shuju[0]=juli%10;
}
void main()
{
init();
while(1)
{
while(!RX)
TR0=1;
while(RX)
TR0=0;
chuli();
}
}
测的数据就是不对呢!!!!!! |