#include<reg52.h>
unsigned char code LedChar[] = { // 数码管真值表
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
unsigned char LedBuff[6] = { // 数码管缓存数组
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
unsigned int cnt = 0; // 中断计数器
void main() {
unsigned long sec = 0, base = 1; // 秒数, 秒数的基数, 十进制
unsigned int j = 0; // 数码管模块的编号(0-5)
P1 = 0x00; //使能U3,选择控制数码管
TMOD = 0x01; // 定时器工作模式1
TH0 = 0xFC; // 设定定时器的计时长度为0.02秒
TL0 = 0x67;
TR0 = 1;
ET0 = 1;
EA = 1;
while(1) {
if(cnt >= 1000) {
cnt = 0;
sec++;
for(j = 0; j < 6; j++) {
LedBuff[j] = LedChar[sec/base%10]; // 带前导0
//sec/base ? (LedBuff[j] = LedChar[sec/base%10]) : (LedBuff[j] = 0xFF); // 不带前导0
base *= 10;
}
}
base = 1;
}
}
unsigned char i = 0;
void InterruptTimer0() interrupt 1 {
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
cnt++; //中断次数计数值加1
//以下代码完成数码管动态扫描刷新
P0 = 0xFF; //显示消隐
switch (i) {
case 0: P1 = 0x08, i++, P0 = LedBuff[0]; break;
case 1: P1 = 0x09, i++, P0 = LedBuff[1]; break;
case 2: P1 = 0x0A, i++, P0 = LedBuff[2]; break;
case 3: P1 = 0x0B, i++, P0 = LedBuff[3]; break;
case 4: P1 = 0x0C, i++, P0 = LedBuff[4]; break;
case 5: P1 = 0x0D, i=0, P0 = LedBuff[5]; break;
default: break;
}
}
wokao。。。还有写单片机的。。。