#include <reg52.h>
sbit wei3=P3^6; /*百位*/
sbit wei1=P3^4; /*個位*/
sbit wei2=P3^5; /*十位*/
unsigned char code LedChar[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E };
void delay(int z);
unsigned char i=0; //動態掃描的索引
unsigned int cnt=0; //記錄 T0 中斷次數
void main()
{ unsigned long sec=0;
EA=1; //使能總中斷
TMOD=0x01; //設置 T0 為模式 1
TH0=0xB8; //為 T0 賦初值 0xFC67,定時 1ms
TL0=0x00;
ET0=1; //使能 T0 中斷
TR0=1; //啟動 T0
while (1)
{
if (cnt>=1000) //判斷 T0 溢出是否達到 1000 次
{ cnt=0; //達到 1000 次后計數值清零
sec++; //秒計數自加 1
}
wei1=0;
P0=LedChar[sec%10];delay(5);P0=0xff;
wei1=1; wei2=0;P0=LedChar[sec/10%10];delay(5); P0=0xff;wei2=1;
wei3=0;P0=LedChar[sec/100%10];delay(5); P0=0xff;wei3=1;
if(sec>999)
{
sec=0;
}
/*wei1=1;
delay(5);
wei2=0;
P0=LedChar[sec/10%10];
wei2=1;
delay(5);
wei3=0;
P0=LedChar[sec/100%10];
wei3=1;
delay(5);
if(sec>999)
{ sec=0; } */
}
}
void delay(int z)
{ int x,y;
for(x=z;x>0;x--)
for(y=10;y>0;y--);
}
/* 定時器 0 中斷服務函數 */
void InterruptTimer0() interrupt 1
{ TH0 = 0xFC; //重新加載初值
TL0 = 0x67;
cnt++; //中斷次數計數值加 1
}
|