78K0/KC2单片机有多达7通道的定时器:
16 位定时器/事件计数器: 1 通道
8 位定时器/事件计数器: 2 通道
8 位定时器: 2 通道
钟表定时器: 1 通道
看门狗定时器: 1 通道
其中的16位定时器./事件计数器00有间隔定时器、方波输出、外部事件计数器、单脉冲输出、PPG输出和脉冲宽度测量六大功能。今天我利用他的间隔定时器功能实现了相对精确的LED 1s闪烁灯,程序如下:
#pragma sfr
#pragma ei
#pragma di
#pragma interrupt INTTM000 MD_INTTM000
#define uchar unsigned char
#define uint unsigned int
uchar flag;
void SysInit()
{
IMS = 0xc8;
IXS = 0x0c;
DI();
OSCCTL = 0x40;
PCC = 0x00; //Fcpu=Fxp
RCM = 0x83; //lsrstop,rstop
MOC = 0x00; //X1
/* Selection of clock supplied to main system clock and peripheral hardware (XSEL, MCM0) */
MCM = 0x05;
EI();
}
void main()
{
SysInit();
TMMK000 = 0; //INTTM000 enabled
CRC00 = 0; //as comparator
PRM00 = 0x02; //inter clock = Fpsr/256
CR000 = 0x7fff; //T = 1s
PM6.0 = 0;
P6.0 = 0;
TMC00 = 0x0c; //clear & start occurs on match between TM00 and CR000
for(;;)
{
while(!flag);
flag = 0;
P6.0 = ~P6.0;
}
}
__interrupt void MD_INTTM000(void)
{
flag = 1;
}