找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6054|回復(fù): 2
收起左側(cè)

C語言寫的單片機(jī)定時(shí)器回調(diào)函數(shù)

[復(fù)制鏈接]
ID:372008 發(fā)表于 2018-7-15 16:18 | 顯示全部樓層 |閱讀模式
C語言寫的定時(shí)器回調(diào)函數(shù),可以移植到任何有定時(shí)器的單片機(jī)上,只要設(shè)置單片機(jī)定時(shí)器1ms中斷,這樣在主循環(huán)中定時(shí)時(shí)間到了就會(huì)調(diào)用相應(yīng)的函數(shù),這樣我們就有了無數(shù)個(gè)軟件定時(shí)器使用,軟件定時(shí)器可以啟動(dòng),停止,一次性的。從別的網(wǎng)站下載的,分享給大家。具體參考附件程序。
0.png

單片機(jī)源程序如下:
  1. /*
  2. *                   multi_timer.c
  3. *
  4. *      Created on: 20161229
  5. *      @Author   : 曉宇 <karaxiaoyu@gmail.com>
  6. *                @id       : 芯片之家
  7. *      @version  :V1.0.0
  8. */

  9. #include "multi_timer.h"

  10. //timer handle list head.
  11. static struct Timer* head_handle = NULL;

  12. //Timer ticks
  13. static uint32_t _timer_ticks = 0;

  14. /**
  15.   * @brief  Initializes the timer struct handle.
  16.   * @param  handle: the timer handle strcut.
  17.   * @param  timeout_cb: timeout callback.
  18.   * @param  repeat: repeat interval time.
  19.   * @retval None
  20.   */
  21. void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
  22. {
  23.         // memset(handle, sizeof(struct Timer), 0);
  24.         handle->timeout_cb = timeout_cb;
  25.         handle->timeout = _timer_ticks + timeout;
  26.         handle->repeat = repeat;
  27. }

  28. /**
  29.   * @brief  Start the timer work, add the handle into work list.
  30.   * @param  btn: target handle strcut.
  31.   * @retval 0: succeed. -1: already exist.
  32.   */
  33. int timer_start(struct Timer* handle)
  34. {
  35.         struct Timer* target = head_handle;
  36.         while(target) {
  37.                 if(target == handle) return -1;        //already exist.
  38.                 target = target->next;
  39.         }
  40.         handle->next = head_handle;
  41.         head_handle = handle;
  42.         return 0;
  43. }

  44. /**
  45.   * @brief  Stop the timer work, remove the handle off work list.
  46.   * @param  handle: target handle strcut.
  47.   * @retval None
  48.   */
  49. void timer_stop(struct Timer* handle)
  50. {
  51.         struct Timer** curr;
  52.         for(curr = &head_handle; *curr; ) {
  53.                 struct Timer* entry = *curr;
  54.                 if (entry == handle) {
  55.                         *curr = entry->next;
  56. //                        free(entry);
  57.                 } else
  58.                         curr = &entry->next;
  59.         }
  60. }

  61. /**
  62.   * @brief  main loop.
  63.   * @param  None.
  64.   * @retval None
  65.   */
  66. void timer_loop()
  67. {
  68.         struct Timer* target;
  69.         for(target=head_handle; target; target=target->next) {
  70.                 if(_timer_ticks >= target->timeout) {
  71.                         if(target->repeat == 0) {
  72.                                 timer_stop(target);
  73.                         } else {
  74.                                 target->timeout = _timer_ticks + target->repeat;
  75.                         }
  76.                         target->timeout_cb();
  77.                 }
  78.         }
  79. }

  80. /**
  81.   * @brief  background ticks, timer repeat invoking interval 1ms.
  82.   * @param  None.
  83.   * @retval None.
  84.   */
  85. void timer_ticks()
  86. {
  87.         _timer_ticks++;
  88. }
復(fù)制代碼

所有資料51hei提供下載:
MultiTimer.rar (2.14 KB, 下載次數(shù): 64)


評(píng)分

參與人數(shù) 1黑幣 +90 收起 理由
admin + 90 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:135781 發(fā)表于 2020-4-20 08:35 | 顯示全部樓層
時(shí)鐘_timer_ticks不清零,timeout一直累加,這樣會(huì)出問題的
回復(fù)

使用道具 舉報(bào)

ID:135781 發(fā)表于 2020-4-20 09:30 | 顯示全部樓層
這個(gè)程序里面會(huì)出現(xiàn)一個(gè)問題,就是數(shù)據(jù)溢出的問題,把判斷條件改下就行了
if(_timer_ticks >= target->timeout)改為 if(_timer_ticks == target->timeout)
這樣就不會(huì)在數(shù)據(jù)溢出后從零開始出現(xiàn)的誤判問題
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表