STC89C52-秒表(精确10ms)同时LED二进制计数
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsignedlong uint32;
/*数码管0到F定义 */
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e};
uint8 a[6];
uint16 counter1 = 0;
uint8 counter = 0;
sbit EN LED= P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
/*定时器1初始化,并定时1ms*/
void timer1_init()
{
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xFC;
TL1 = 0x67;
TR1 = 1;
}
/*中断初始化*/
void int_init()
{
ET1 = 1;
EA = 1;
}
/*刷新数码管*/
void refresh_ led()
{
static uint8 j = 0;
uint8 b = number[a[2]] - 0x80 ;
switch(j)
{
case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;P0 = number[a[0]];break;
case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;P0 = number[a[1]];break;
case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;P0 = b;break;
case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;P0 = number[a[3]];break;
case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;P0 = number[a[4]];break;
case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;P0 = number[a[5]];break;
default: break;
}
if(7 == j++) j = 0;
}
main()
{
ENLED = 0; ADDR3 = 1;
timer1_init();
int_init();
while(1);
}
/*中断函数*/
void interrupt_timer1() interrupt 3
{
static uint32 sec = 0;
TH1 = 0xFC;
TL1 = 0x66;
counter++;
if(counter == 10)
{
sec++;
counter = 0;
a[0] = sec%10;
a[1] = sec/10%10;
a[2] = sec/100%10;
a[3] = sec/1000%10;
a[4] = sec/10000%10;
a[5] = sec/100000%10;
if(sec%100 == 0) counter1++; //LED时间1s
if(counter1 == 256) counter1 = 0;
}
if(counter == 9)
{ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;P0 = ~counter1 ;}
refresh_led();
}
评论