当事件A发生某段时间后再继续执行事件A,而这段时间需要去处理B事件,就可以使用定时器。如果这段时间不需要处理其他事件,可以直接使用延时函数。
软件定时器应用主要包括四个步骤:
- 创建定时器,配置定时器ID,指定定时类型(周期性/一次性);
- 开启定时任务,指定定时时长,定时器ID;
- 定时器处理函数,关联定时器ID,定义当定时时长达到后需要执行哪些操作;
- 关闭定时任务。
#ifndef SW_TIMER
#define SW_TIMER
#endif
#ifdef SW_TIMER
void app_timer_test_handle(void const *param);
// timer time
#define APP_TEST_TIMER_MS 1000
// 将定时器的handle 进行索引
osTimerDef (APP_SW_TIMER_TEST, app_timer_test_handle);
// timer iD
static osTimerId app_sw_timer_test = NULL;
// sw timer hanlde func
void app_timer_test_handle(void const *param)
{
// do anything
}
// crest a SW timer task
void app_creat_sw_timer(void)
{
// If it hasn't been created, it will be created
if (app_sw_timer_test == NULL) {
// Create a software Timer and return a Timer ID for later in