#include <REGX52.H>
unsigned char i=0;
unsigned int msec=0;
unsigned long sec=0;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P24=P2^4;
unsigned char code LedChar[16]={//数码管显示内容,共阴数码管真值表0~9,A~F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char LedBuff[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};//数码管缓冲区,16进制
void Led(unsigned char a)//激活第a个数码管,a介于0~7
{
switch(a)
{
case 0: P22=0;P23=0;P24=0;break;
case 1: P22=1;P23=0;P24=0;break;
case 2: P22=0;P23=1;P24=0;break;
case 3: P22=1;P23=1;P24=0;break;
case 4: P22=0;P23=0;P24=1;break;
case 5: P22=1;P23=0;P24=1;break;
case 6: P22=0;P23=1;P24=1;break;
case 7: P22=1;P23=1;P24=1;break;
}
}
void main()
{
TMOD=0x01;//定时器模式
TH0=0xFC;
TL0=0x67;//定时1ms
TR0=1;//启动定时器
EA=1;//打开总中断
ET0=1;//定时器0中断使能
while(1)
{
if(msec>=1000)
{
msec=0;
sec++;
LedBuff[0]=LedChar[sec%10];
LedBuff[1]=LedChar[(sec/10)%10];
LedBuff[2]=LedChar[(sec/100)%10];
LedBuff[3]=LedChar[(sec/1000)%10];
LedBuff[4]=LedChar[(sec/10000)%10];
LedBuff[5]=LedChar[(sec/100000)%10];
LedBuff[6]=LedChar[(sec/1000000)%10];
LedBuff[7]=LedChar[(sec/10000000)%10];
}
}
}
void InterruptTimer0() interrupt 1 //中断入口号*8+3=中断向量地址,溢出就触发中断
{
msec++;
TH0=0xFC;
TL0=0x67;
P0=0x00;//消影
Led(i);
P0=LedBuff[i];
i++;
if(i==8)i=0;
}
数码管动态显示,定时器,中断
最新推荐文章于 2024-09-08 10:38:25 发布