久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標(biāo)題: 完全由C語(yǔ)言編寫,高度可移植,超級(jí)牛逼的軟件定時(shí)器! [打印本頁(yè)]

作者: 李牧林    時(shí)間: 2019-9-14 19:57
標(biāo)題: 完全由C語(yǔ)言編寫,高度可移植,超級(jí)牛逼的軟件定時(shí)器!
帖子來(lái)自芯片之家公眾號(hào)

作為一個(gè)搞電子嵌入式的你來(lái)說(shuō),定時(shí)器相信絕對(duì)不會(huì)陌生,去翻看STM32的官方數(shù)據(jù)手冊(cè),可能有四分之一的篇幅都在講定時(shí)器,可見定時(shí)器的重要性,定時(shí)器定時(shí)器嘛,最重要的功能還是定時(shí)。之前經(jīng)常有師弟跟我抱怨,哎呀,這個(gè)單片機(jī)怎么只有2個(gè)定時(shí)器,我都不夠用了,怎么辦呀,然后一頭霧水,其實(shí),只要有一個(gè)硬件定時(shí)器,在實(shí)際不是要求特別特別精準(zhǔn)的場(chǎng)合,我們可以模擬很多個(gè)軟件定時(shí)器出來(lái),今天,群主給大家分享一個(gè)完全由C語(yǔ)言編寫,高度可移植,超級(jí)牛逼的軟件定時(shí)器!

MultiTimer 是一個(gè)軟件定時(shí)器擴(kuò)展模塊,可無(wú)限擴(kuò)展你所需的定時(shí)器任務(wù),取代傳統(tǒng)的標(biāo)志位判斷方式, 更優(yōu)雅更便捷地管理程序的時(shí)間觸發(fā)時(shí)序。一共有3個(gè)文件:
multi_timer.c--定時(shí)器c源文件;
multi_timer.h--定時(shí)器h頭文件;
main--用戶代碼文件。

我們直接上代碼:
  1. /*
  2. *     main.c
  3. *
  4. *      Created on: 20161229
  5. *      @Author   : 曉宇
  6. *      @version  :V1.0.0
  7. *
  8. *                        ,%%%%%%%%,
  9. *                      ,%%/\%%%%/\%%
  10. *                     ,%%%\c''''J/%%%
  11. *           %.        %%%%/ o  o \%%%
  12. *           `%%.      %%%%       |%%%
  13. *            `%%      `%%%%(__Y__)%%'
  14. *            //        ;%%%%`\-/%%%'
  15. *            ((      /   `%%%%%%%'
  16. *             \\     .'           |
  17. *              \\   /        \  | |
  18. *               \\/          ) | |
  19. *                \          /_ | |__
  20. *                (____________))))))) 攻城獅
  21. *
  22. */

  23. #include "multi_timer.h"

  24. struct Timer timer1;
  25. struct Timer timer2;

  26. void timer1_callback()
  27. {
  28.    printf("timer1 timeout!\r\n");
  29. }

  30. void timer2_callback()
  31. {
  32.    printf("timer2 timeout!\r\n");
  33. }

  34. int main()
  35. {
  36. timer_init(&timer1, timer1_callback, 1000, 1000); //1s loop
  37. timer_start(&timer1);

  38. timer_init(&timer2, timer2_callback, 50, 0); //50ms delay
  39. timer_start(&timer2);

  40. while(1) {
  41.      
  42.      timer_loop();
  43. }
  44. }

  45. void HAL_SYSTICK_Callback(void)
  46. {
  47.    timer_ticks(); //1ms ticks
  48. }
復(fù)制代碼


1、先申請(qǐng)一個(gè)定時(shí)器管理handle
  1. struct Timer timer1;
復(fù)制代碼


2、初始化定時(shí)器對(duì)象,注冊(cè)定時(shí)器回調(diào)處理函數(shù),設(shè)置定時(shí)時(shí)間(ms),循環(huán)定時(shí)觸發(fā)時(shí)間

  1. timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
復(fù)制代碼


3.啟動(dòng)定時(shí)器
  1. timer_start(&timer1);
復(fù)制代碼



4.設(shè)置1ms的硬件定時(shí)器循環(huán)調(diào)用 *timer_ticks()* 以提供時(shí)間基準(zhǔn)

  1. void HAL_SYSTICK_Callback(void)
  2. {
  3.    timer_ticks();
  4. }
復(fù)制代碼



5.在主循環(huán)調(diào)用定時(shí)器后臺(tái)處理函數(shù)

  1. int main()
  2. {    ...
  3.    while(1) {
  4.        ...
  5.        timer_loop();
  6.    }
  7. }
復(fù)制代碼



上面,我們定義了兩個(gè)定時(shí)器,第一個(gè)以每1秒一次進(jìn)行循環(huán)執(zhí)行,第二個(gè)定時(shí)器50ms之后只執(zhí)行一次,執(zhí)行之后,可以通過(guò)相應(yīng)的回調(diào)函數(shù)打印出來(lái),里面就可以添加我們的定時(shí)任務(wù)了。怎么樣,是不是很簡(jiǎn)單,只需要設(shè)置好定時(shí)基準(zhǔn),申請(qǐng)好定時(shí)器,初始化完畢,你想要的效果,是定時(shí)一次還是進(jìn)行循環(huán),都沒(méi)問(wèn)題,多個(gè)定時(shí)器之間使用鏈表來(lái)實(shí)現(xiàn),可以啟動(dòng)暫停每個(gè)定時(shí)器,用起來(lái)之后,是不是很爽?具體的代碼,需要大家仔細(xì)去推敲其中的原理,當(dāng)然直接用也是可以的。

部分源代碼展示:
  1. /*
  2. *     multi_timer.h
  3. *
  4. *      Created on: 20161229
  5. *      @Author   : 曉宇
  6. *      @version  :V1.0.0
  7. */

  8. #ifndef _MULTI_TIMER_H_
  9. #define _MULTI_TIMER_H_

  10. #include "stdint.h"

  11. typedef struct Timer {
  12.    uint32_t timeout;
  13.    uint32_t repeat;
  14.    void (*timeout_cb)(void);
  15.    struct Timer* next;
  16. }Timer;

  17. #ifdef __cplusplus  
  18. extern "C" {  
  19. #endif  

  20. void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);
  21. int  timer_start(struct Timer* handle);
  22. void timer_stop(struct Timer* handle);
  23. void timer_ticks(void);
  24. void timer_loop(void);

  25. // void timer_again(struct Timer* handle);
  26. // void timer_set_repeat(struct Timer* handle, uint32_t repeat);

  27. #ifdef __cplusplus
  28. }
  29. #endif

  30. #endif/*
  31. *     multi_timer.c
  32. *
  33. *      Created on: 20161229
  34. *      @Author   : 曉宇
  35. *      @version  :V1.0.0
  36. */


  37. #include "multi_timer.h"

  38. //timer handle list head.
  39. static struct Timer* head_handle = NULL;

  40. //Timer ticks
  41. static uint32_t _timer_ticks = 0;

  42. /**
  43. * @brief  Initializes the timer struct handle.
  44. * @param  handle: the timer handle strcut.
  45. * @param  timeout_cb: timeout callback.
  46. * @param  repeat: repeat interval time.
  47. * @retval None
  48. */
  49. void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat)
  50. {
  51. // memset(handle, sizeof(struct Timer), 0);
  52. handle->timeout_cb = timeout_cb;
  53. handle->timeout = _timer_ticks + timeout;
  54. handle->repeat = repeat;
  55. }

  56. /**
  57. * @brief  Start the timer work, add the handle into work list.
  58. * @param  btn: target handle strcut.
  59. * @retval 0: succeed. -1: already exist.
  60. */
  61. int timer_start(struct Timer* handle)
  62. {
  63. struct Timer* target = head_handle;
  64. while(target) {
  65.    if(target == handle) return -1;  //already exist.
  66.    target = target->next;
  67. }
  68. handle->next = head_handle;
  69. head_handle = handle;
  70. return 0;
  71. }

  72. /**
  73. * @brief  Stop the timer work, remove the handle off work list.
  74. * @param  handle: target handle strcut.
  75. * @retval None
  76. */
  77. void timer_stop(struct Timer* handle)
  78. {
  79. struct Timer** curr;
  80. for(curr = &head_handle; *curr; ) {
  81.    struct Timer* entry = *curr;
  82.    if (entry == handle) {
  83.      *curr = entry->next;
  84. //      free(entry);
  85.    } else
  86.      curr = &entry->next;
  87. }
  88. }

  89. /**
  90. * @brief  main loop.
  91. * @param  None.
  92. * @retval None
  93. */
  94. void timer_loop()
  95. {
  96. struct Timer* target;
  97. for(target=head_handle; target; target=target->next) {
  98.    if(_timer_ticks >= target->timeout) {
  99.      if(target->repeat == 0) {
  100.        timer_stop(target);
  101.      } else {
  102.        target->timeout = _timer_ticks + target->repeat;
  103.      }
  104.      target->timeout_cb();
  105.    }
  106. }
  107. }

  108. /**
  109. * @brief  background ticks, timer repeat invoking interval 1ms.
  110. * @param  None.
  111. * @retval None.
  112. */
  113. void timer_ticks()
  114. {
  115. _timer_ticks++;
  116. }
復(fù)制代碼

全部資料51hei下載地址:
MultiTimer.zip (2.26 KB, 下載次數(shù): 47)


作者: leo2002zhang    時(shí)間: 2019-9-16 10:30
學(xué)習(xí)一下,給樓主點(diǎn)個(gè)贊。軟定時(shí)還是經(jīng)常用到的。
作者: 李牧林    時(shí)間: 2019-9-23 08:08
leo2002zhang 發(fā)表于 2019-9-16 10:30
學(xué)習(xí)一下,給樓主點(diǎn)個(gè)贊。軟定時(shí)還是經(jīng)常用到的。

32用的比較方便,51直接用標(biāo)志位去完成軟定時(shí)器功能了
作者: jizhongbiao    時(shí)間: 2020-9-10 00:13
把github地址貼出來(lái)好一點(diǎn),這一個(gè)是有開源協(xié)議的。




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 欧美男人亚洲天堂 | 美女天天操 | 日韩精品免费在线 | 国产亚洲一区精品 | 欧美区日韩区 | 国产成人精品免费 | 亚洲精品视频在线看 | 久久久久久综合 | 成人av片在线观看 | 亚洲一区自拍 | 欧美成人a | 91久久精品日日躁夜夜躁欧美 | 久久精品国产一区二区电影 | 99视频免费在线 | 嫩草视频在线免费观看 | 成人国产在线观看 | 欧美日韩黄色一级片 | 久久99精品久久久久久国产越南 | 久久中文一区二区 | 精品一区av | 亚洲日韩中文字幕一区 | 久久精品在线播放 | 色狠狠一区| 久久网站免费视频 | 青青草av网站 | 91精品国产美女在线观看 | 一区二区三区免费 | 欧美日韩91 | www国产成人| 在线婷婷| 免费精品 | 羞羞视频网站 | 日本黄视频在线观看 | 日本不卡一区二区三区在线观看 | 亚洲欧美bt | 午夜a级理论片915影院 | 久久精品亚洲一区二区三区浴池 | 欧美激情欧美激情在线五月 | 成人在线观看免费 | 天堂在线91 | 操操网站 |