一、需求

  1、完成定时器需要:

    定时器为计数模式

    打开定时器中断

    编辑中断回调函数

  2、使用

    开启定时器

    中断回调函数处理数据

二、实施

  配置文件

合泰单片机3-定时器_学习

  1、定时器

void Timer0Initial(void)
{//Timer0 : 1mS
            _tmr0c=0b10010101;           //Timer mode,Fsys/64   
//                   ||||||||    
//    b7: _t0m1 -------||||||||
//    b6: _t0m0 --------|||||||
//    b5:  N/A-----------||||||
//    b4: _t0on ----------|||||
//  b3: _t0e ------------||||
//    b2: _t0psc2 ----------|||            //101: fsys/32=> 8Mhz /32 = 250Khz (4uS)
//    b1: _t0psc1 -----------||
//    b0: _t0psc0 ------------|
            _tmr0=256-25;                //1ms = 4uS * 250    
}

  2、回调函数

u8 tim_cnt = 0;
//定时 / 计数器 0 溢出中断
void __attribute( ( interrupt(0x14) ) ) ISR_tmr0 (void)
{    
    if(++tim_cnt>=10)
    {
        tim_cnt = 0;
        event.t1mS = 1;    
    }
    event.t100uS = 1;        
}

  3、使能计数和中断

void inline TimerInit(void)
{
    Timer0Initial();

       _toe = 1;                //Timer0 interrupt enable
    _t2e_ade = 1;
}