下面是一个动态数码管计时器的程序
#include <stc12c5a60s2.h>
sbit ENLED = P1^1;
unsigned char code LedChar[] = {
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};
unsigned char LedBuff[] = {
0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char i = 0;
unsigned int cnt = 0;
unsigned char flagls = 0;
void main()
{
unsigned long sec = 0;
AUXR = 0x80;
ENLED = 0;
TMOD = TMOD & 0xf0;
TMOD = TMOD | 0x01;
TH0 = 0x83;
TL0 = 0x00;
TR0 = 1;
EA = 1;
ET0 = 1;
while(1)
{
if(flagls == 1)
{
flagls = 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];
}
}
}
void InterrupuTimer0() interrupt 1
{
TH0 = 0x83;
TL0 = 0x00;
cnt ++;
if (cnt >= 1000)
{
cnt = 0;
flagls = 1;
}
P0 = 0xff;
P2 = 0xfd-i;
P0 = LedBuff[i];
if(i<5)
i++;
else
i = 0;
} |