#define PB1 2 // 164 时钟位
#define PB0 1 // 164 数据位xdata unsigned char OUTBIT _at_ 0x8002; // 位控制口
xdata unsigned char OUTSEG _at_ 0x8004; // 段控制口unsigned char LEDBuf[LEDLen]; // 显示缓冲
code unsigned char LEDMAP[] = { // 八段管显示码
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};void Delay(unsigned char CNT)
{
unsigned char i; while (CNT-- !=0)
for (i=100; i !=0; i--);
}void DisplayLED()
{
unsigned char i, j;
unsigned char Pos;
unsigned char LED; Pos = 0x20; // 从左边开始显示
for (i = 0; i < LEDLen; i++) {
OUTBIT = 0; // 关所有八段管
LED = LEDBuf[i];
OUTSEG = LED;
OUTBIT = Pos; // 显示一位八段管
Delay(1);
Pos >>= 1; // 显示下一位
}
}// ===============================================================================#define Tick 10000 // 10000 x 100us = 1s
#define T100us (256-50) // 100us时间常数(6M)unsigned char Hour, Minute, Second;
unsigned int C100us; // 100us记数单元void T0Int() interrupt 1
{
C100us--;
if (C100us == 0) {
C100us = Tick; // 100us 记数器为0, 重置记数器
Second++;
if (Second == 60) {
Second = 0;
Minute++;
if (Minute == 60) {
Minute = 0;
Hour++;
if (Hour == 24) Hour = 0;
}
}
}
}void main()
{
TMOD = 0x02; // 方式2, 定时器
TH0 = T100us;
TL0 = T100us; IE = 0x82; // EA=1, IT0 = 1 Hour = 0;
Minute = 0;
Second = 0;
C100us = Tick;
TR0 = 1; // 启动定时器0 while (1) {
LEDBuf[0] = LEDMAP[Hour/10];
LEDBuf[1] = LEDMAP[Hour%10] | 0x80;
LEDBuf[2] = LEDMAP[Minute/10];
LEDBuf[3] = LEDMAP[Minute%10] | 0x80;
LEDBuf[4] = LEDMAP[Second/10];
LEDBuf[5] = LEDMAP[Second%10]; DisplayLED();
}
}