一種高效定時(shí)器模塊的設(shè)計(jì)與實(shí)現(xiàn)
typedef enum{
本文引用地址:http://butianyuan.cn/article/118088.htmFEEDWATCHDOG_300MS,
INPUT_DETECT_MS,
LIN_TIMESLOT_MS,
SPEED_PULSE_ACCUMU_200MS,
ALLDR_LOCK_200MS,
ALARM_30S,
IGNITION_OFF_60S,
...
...
MAX_TIMER_NUM
}TimerId;
這樣,定義數(shù)組Timer TIMER[MAX_TIMER_NUM],在初始化中,TIMER [i].timer_id=i;之后通過(guò)TIMER[timer_id]即可枚舉某軟件定時(shí)器節(jié)點(diǎn)。
定時(shí)器管理
定時(shí)器管理包括啟動(dòng)、運(yùn)行、停止、重啟和讀取計(jì)時(shí)時(shí)間等功能[4],相應(yīng)API函數(shù)定義如下:
void TimerStart(TimerId timer_id,ulong timeout,Bool cycle,uchar cnt_times);
void TimerReStart(TimerId timer_id);
void TimerTick(void);
void TimerStop(TimerId timer_id);
void TimerStall(TimerId timer_id);
ulong TimerGet(TimerId timer_id);
啟動(dòng)定時(shí)器函數(shù)如下:
#dene RTIPERIOD 2
void TimerStart(TimerId timer_id,ulong timeout,Bool cycle,uchar cnt_times)
{
TIMER[timer_id].timer_state=RUNNING;
TIMER[timer_id].duration=0;
TIMER[timer_id].timeout=timeout/RTIPERIOD;
TIMER[timer_id].cycle=cycle;
TIMER[timer_id].cnt_times=cnt_times;
TIMER[timer_id].timer_id=timer_id;
}
評(píng)論