#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ul unsigned long
sbit beep=P2^3;
sbit dula=P2^6;//声明锁存器U1的锁存端
sbit wela=P2^7;//声明锁存器U2的锁存端
uchar code table[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71
};
uchar code tablwe[]=
{
0xdf,0xef,0xf7,0xfb,0xfd,0xfe
};
uint count,num1,swan,wan,qian,bai,shi,ge,wei,shuzi1,shuzi2;
ul num;
void delayms(uint);
void wedu(uint,uint);
void display();
void main()
{
TMOD=0x01; //定时器0工作于方式1
TH0=(65536-45872)/256;//装初值
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
while(1)//程序在此处不停地对数码管进行动态扫描等待中断发生数码管示数发生变化
{
display();
}
}
void display()
{
for(wei=0;wei<6;wei++)
{
switch(wei)
{
case 0: wedu(ge,wei);break;
case 1: wedu(shi,wei);break;
case 2: wedu(bai,wei);break;
case 3: wedu(qian,wei);break;
case 4: wedu(wan,wei);break;
case 5: wedu(swan,wei);break;
}
}
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=1;j>0;j--);
}
void wedu(uint shuzi1,uint shuzi2)
{
dula=1;
P0=table[shuzi1];//送入段选数据
dula=0;
P0=0xff;//送位选数据前关闭所有显示,防止打开位选锁存器时原来段选数据通过位选锁存器造成混乱
wela=1;
/*switch(shuzi2)//送位选数据
{
case 0: P0=0xdf;break;
case 1: P0=0xef;break;
case 2: P0=0xf7;break;
case 3: P0=0xfb;break;
case 4: P0=0xfd;break;
case 5: P0=0xfe;break;
}*/
P0=tablwe[shuzi2];//进一步简化
wela=0;
P0=0xff;//送位选数据前关闭所有显示,防止打开位选锁存器时原来段选数据通过位选锁存器造成混乱
delayms(100);
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256;//重装初值
TL0=(65536-45872)%256;
num1++;
if(num1==20)
{
num1=0;
num++;
if(num==1000000) num=0;
beep=0;
delayms(1);
swan=num/100000;
wan=(num%100000)/10000;
qian=((num%100000)%10000)/1000;
bai=(((num%100000)%10000)%1000)/100;
shi=((((num%100000)%10000)%1000)%100)/10;
ge=((((num%100000)%10000)%1000)%100)%10;
beep=1;
delayms(1);
}
}
|