/* This file is an example to use timer2 in mode 0 此程序用于定时器2模式0*/
#define MSB_reload_value 0x36
/* msb reload value exemple 高位重载值:延时60ms*/
#define LSB_reload_value 0x36
/* lsb reload value exemple 低位重载值*/
#include "reg_c51.h" //当前文件夹内的文件
/*
* FUNCTION_PURPOSE: This file set up timer 2 in mode 0 (16 bits auto-reload up/down counting timer).
* 用于模式0: 16位自动重载 向上/下计时
* The 16-bits register consist of all 8 bits of TH2 and all 8 bits of TL2.
* 16位寄存器由8位的TH2 和8位的TL2组成
* The EXF2 bit toggles when timer2 overflow or underflow occurs.
* 当timer2向上/下溢出时,置位EXF2位
* EXF2 does not generate interrupt. This bit can be used to provide 17-bit resolution
* FUNCTION_INPUTS: P1.1(T2EX)=0 for down counting or 1 for up counting.
* EXF2位并不引起中断。此位可以用来提供17位输入功能:向上/下计数功能
* FUNCTION_OUTPUTS: void
*/
void main(void)
{
T2MOD &= 0xFC; /* T2OE=0;定时器2输出不允许 */
T2MOD |= 0x01; /* DCEN=1;可以设置为向上/下计数:此程序中为向上达到0xff时溢出*/
EXF2 = 0; /* reset flag 定时器2外部标志位清除*/
TCLK = 0; RCLK = 0; /* disable baud rate generator 不用于波特率发生器*/
EXEN2 = 0; /* ignore events on T2EX 视T2EX(P1.1)端信号无效*/
TH2 = MSB_reload_value; /* Init msb_value 填入初始化值*/
TL2 = LSB_reload_value; /* Init lsb_value */
RCAP2H = MSB_reload_value; /* reload msb_value重载值 */
RCAP2L = LSB_reload_value; /* reload lsb_value */
C_T2 = 0; /* timer mode 定时模式*/
CP_RL2 = 0; /* reload mode 重载模式*/
EA = 1; /* interupt enable 中断允许*/
ET2 = 1; /* enable timer2 interrupt 允许T2中断*/
TR2 = 1; /* timer2 run 开始计时*/
while(1); /* endless */
}
/*
* FUNCTION_PURPOSE: timer2 interrupt
* FUNCTION_INPUTS: void
* FUNCTION_OUTPUTS: P1.2 toggle period = 2
* (65536-reload_value) cycles
*/
void it_timer2(void) interrupt 5
/* interrupt address is 0x002b 定时器2中断向量入口*/
{
P0_2 = ~P0_2; /* P1.2 toggle when interrupt. 中断时取反*/
TF2 = 0; /* reset interrupt flag 复位中断标志位*/
}