1,概述

我们在写代码的时候往往会设置定时器进行一个定时任务,在使用的时候又分为硬件定时器和软件定时器两种,一般情况下在项目工程中不怎么使用硬件定时器、大部分都是使用软件定时器。

2,API参考

2.1 软件定时器部分

  • 软件定时器部分调用RTOS接口

    2.1.1 软件定时器定义

  • 函数原型:

    1. #define osTimerDef(name, function) \
    2. extern const osTimerDef_t os_timer_def_##name
  • 函数功能:定义一个定时器与其处理函数

  • 函数参数:

    • name:定时器ID
    • function:定时器处理函数

      2.1.2 软件定时器创建

  • 函数原型:

    1. osTimerId osTimerCreate (const osTimerDef_t *timer_def,
    2. os_timer_type type, void *argument)
  • 函数功能:创建软件定时器

  • 函数参数:
    • *timer_def:以定义的Timer
    • type:软件定时器工作模式
    • *argument:默认为NULL
  • 返回值:

    • osTimerId

      2.1.3 软件定时器开启

  • 函数原型:

    1. osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)
  • 函数功能:开启已设置的软件定时器

  • 函数参数:
    • timer_id:定时器id
    • ticks:定时器周期
  • 返回值:

    • timer状态

      2.1.4 软件定时器停止

  • 函数原型:

    1. osStatus_t osTimerStop (osTimerId_t timer_id)
  • 函数功能:停止软件定时器

  • 函数参数:
    • timer_id:定时器id
  • 返回值:

    • timer状态

      2.2 硬件定时器部分

      2.2.1 硬件定时器初始化

  • 函数原型:

    1. void hwtimer_init(void);
  • 函数功能:初始化硬件定时器

    2.2.2 创建硬件定时器

  • 函数原型:

    1. HWTIMER_ID hwtimer_alloc(HWTIMER_CALLBACK_T callback, void *param);
  • 函数功能:创建硬件定时器

  • 函数参数:
    • callback:硬件定时器回调函数
    • *param:默认为NULL
  • 返回值:

    • 定时器ID

      2.2.3 开启硬件定时器

  • 函数原型:

    1. enum E_HWTIMER_T hwtimer_start(HWTIMER_ID id, unsigned int ticks);
  • 函数功能:开启已创建的硬件定时器

  • 函数参数:
    • id:定时器ID
    • ticks:定时器周期
  • 返回值:

    • 定时器状态

      2.2.4 更新定时器

  • 函数原型:

    1. enum E_HWTIMER_T hwtimer_update(HWTIMER_ID id, HWTIMER_CALLBACK_T callback,
    2. void *param);
  • 函数功能:更新已有定时器的回调函数与参数

  • 函数参数:
    • id:定时器ID
    • callback:硬件定时器回调函数
    • *param:默认为NULL
  • 返回值:

    • 定时器状态

      2.2.5 关闭硬件定时器

  • 函数原型:

    1. enum E_HWTIMER_T hwtimer_stop(HWTIMER_ID id);
  • 函数功能:关闭硬件定时器

  • 函数参数:
    • id:定时器ID
  • 返回值:

    • 定时器状态

      2.2.6 清除硬件定时器

  • 函数原型:

    1. enum E_HWTIMER_T hwtimer_free(HWTIMER_ID id);
  • 函数功能:删除指定定时器并释放内存

  • 函数参数:
    • id:定时器ID
  • 返回值:
    • 定时器状态

      3,使用教程

      3.1软件定时器使用

      ```c 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 indexing
app_sw_timer_test = osTimerCreate(osTimer(APP_SW_TIMER_TEST), osTimerPeriodic, NULL); } // The following are the Stop and Start Timer tasks, which take the parameters of the created Timer ID and time
osTimerStop(app_sw_timer_test); osTimerStart(app_sw_timer_test, APP_TEST_TIMER_MS); }

// 测试函数 int maintest(void) { TRACE(“%s %d.”, func, _LINE); // test creat a SW timer task app_creat_sw_timer();

  1. return 0;

}

  1. <a name="vT6Z0"></a>
  2. ## 3.2硬件定时器使用
  3. - 一般硬件定时器使用在需要精准时间的task上(如Key、USB host等),其余情况一般使用软件定时器
  4. ```c
  5. static HWTIMER_ID debounce_timer = NULL;
  6. static void hal_key_debounce_handler(void *param)
  7. {
  8. // you can look hal_key.c
  9. }
  10. // creat a hw timer task
  11. void app_creat_hw_Timer(void)
  12. {
  13. if (debounce_timer == NULL)
  14. debounce_timer = hwtimer_alloc(hal_key_debounce_handler, NULL);
  15. }
  16. // close hw timer task
  17. void app_close_hw_Timer(void)
  18. {
  19. if (debounce_timer) {
  20. hwtimer_stop(debounce_timer);
  21. hwtimer_free(debounce_timer);
  22. debounce_timer = NULL;
  23. }
  24. }