一直想在单片机上实现一个像MFC和WPF一样的软定时器,花了点时间终于写出来了,使用时需把timer_run函数放到定时器中断服务函数里面
typedef struct _TIMER
{
unsigned char isEnable;
unsigned int count;
unsigned int countNeed;
void (*func)();
}TIMER;
void timer_Run(TIMER *timer)
{
if(timer->isEnable)
{
timer->count ++;
if(timer->count == timer->countNeed)
{
timer->count = 0;
timer->func();
}
}
}
void startTimer(TIMER *timer)
{
timer->count = 0;
timer->isEnable = 1;
}
void stopTimer(TIMER *timer)
{
timer->isEnable = 0;
timer->count =0;
}