1、在.h文件中定义定时器结构体:
typedef struct TIMER_ST
{
unsigned char enable;
unsigned long tic_c ; //current tick
unsigned long tic_cc; //tick count
unsigned char once; //1:run once 0:period
void (*Timer_Callback)(struct TIMER_ST * timer);
unsigned long args;
}Timer_t;
2、在中断函数中置位标志位
extern uint8_t time_1ms;//此时间可根据自己实际需要修改
void TIMER5_IRQHandler(void)
{
if(SET == timer_interrupt_flag_get(TIMER5, TIMER_INT_FLAG_UP))
{
/* clear channel 0 interrupt bit */
timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
time_1ms++;
}
}
3、在main.c 文件中做以下处理(可根据需要在另外文件中处理)
Timer_t Timer_SEQ[2] =
{
{0,0,1000,0,Red_Led_Twinkle,0},
{0,0,2000,1,Blue_Led_TurnOn,0},
};
uint8_t time_1ms = 0;
void Red_Led_Twinkle(Timer_t * timer)
{
...
}
void Blue_Led_TurnOn(Timer_t * timer)
{
...
}
void Timer_Process_1Ms(void)
{
int i;
for( i=0;i<Timer_CC1;i++)
{
if(Timer_SEQ[i].enable)
{
Timer_SEQ[i].tic_c=Timer_SEQ[i].tic_c+1;
if(Timer_SEQ[i].tic_c>=Timer_SEQ[i].tic_cc)
{
if(Timer_SEQ[i].once)
{
Timer_SEQ[i].enable=0;
}
if(Timer_SEQ[i].Timer_Callback)
(*Timer_SEQ[i].Timer_Callback)(&Timer_SEQ[i]);
Timer_SEQ[i].tic_c=0;
}
}
}
}
int main(void)
{
Timer_SEQ[0].enable = 1;
Timer_SEQ[1].enable = 1;
while(1)
{
if(time_1ms)
{
time_1ms = 0;
Timer_Process_1Ms();
}
}
return 0;
}