#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f};
sbit wei=P2^3;
sbit duan=P2^4;
uint qian,bai,shi,ge,i=0;
float f;
uint speed;
/*定时器初始化*/
void initime()
{
TMOD=0x51; //T1计数器,T0定时器,方式1
TL0=(65536-10000)%256;
TH0=(65536-10000)/256;
EA=1;
TL1=0;
TH1=0;
ET0=1;
}
void delay(uint xms)
{
uint y,z;
for(y=xms;y>0;y--)
for(z=110;z>0;z--);
}
/*将十进制数拆成送数码管的显示码*/
void dectobit(int dec)
{
qian=dec/1000;
dec=dec % 1000;
bai=dec/100;
dec=dec % 100;
shi=dec/10;
dec=dec % 10;
ge=dec;
}
void display()
{
duan=1;
P0=table[qian];
duan=0;
P0=0x00;
wei=1;
P0=0xf7;
wei=0;
delay(5);
duan=1;
P0=table[bai];
duan=0;
P0=0x00;
wei=1;
P0=0xfb;
wei=0;
delay(5);
duan=1;
P0=table[shi];
duan=0;
P0=0x00;
wei=1;
P0=0xfd;
wei=0;
delay(5);
duan=1;
P0=table[ge];
duan=0;
P0=0x00;
wei=1;
P0=0xfe;
wei=0;
}
void main(void)
{
initime();
TR0=1;
TR1=1;
while(1)
{
dectobit(f);
display();
}
}
timer()interrupt 1 using 2
{
f=(TH1*256+TL1)*0.63;
TL1=0;
TH1=0;
TL0=(65536-10000)%256;
TH0=(65536-10000)/256;
} |