一直想在单片机上实现一个像MFC和WPF一样的软定时器,前段时间看了《编程魔法师》以后自己花了点时间写了个,使用时需把timer_run函数放到定时器中断服务函数里面(设想中实时操作都是短时任务),本来已发到CSDN上的,想了想还是回来也给大家看下,看能不能再优化下
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;
}
|