用89c51设计59秒定时器,代码如下:
#include <reg51.h>
#define SEG P2
#define SCANP P1
sbit LED=P0^0;
#define count_M1 5000
#define TH_M1 (65536-count_M1)/256
#define TL_M1 (65536-count_M1)%256
int count_T0=0;
#define count_M2 25
#define TH_M2 (256-count_M2)
#define TL_M2 (256-count_M2)
char count_T1=0;
char TAB[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x83,0xF8,0x80,0x98};
char disp[2]={0xC0,0xC0};
char seconds=0;
char scan=0;
void main()
{
IE=0x8A;
TMOD=0x21;
TH0=TH_M1;TL0=TL_M1;
TR0=1;
TH1=TH_M2;TL1=TL_M2;
TR1=1;
LED=1;
while(1);
}
void T0_1s()interrupt 1
{ TH0=TH_M1;TL0=TL_M1;
if(++count_T0==20)
{
count_T0=0;
seconds++;
if(seconds==60)
{seconds=0;LED=~LED;}
}
disp[1]=TAB[seconds/10];
disp[0]=TAB[seconds%10];
}
void T1_8ms()interrupt 3
{
if(++count_T1==32)
{
count_T1=0;
if(++scan==3) scan=1;
SEG=0xFF;
SCANP=~scan;
SEG=disp[scan-1];
}
}
我很奇怪,main函数里已经指定了TL0和TH0的数值,为何timer0的中断函数还要再次指定?
而且还有一点,如果去掉timer0里指定TL0和TH0的这句代码,在define里更改TL0和TH0的大小,timer0里不会体现出这个变化来?? |