找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5320|回复: 2
收起左侧

C语言写的单片机定时器回调函数

[复制链接]
ID:372008 发表于 2018-7-15 16:18 | 显示全部楼层 |阅读模式
C语言写的定时器回调函数,可以移植到任何有定时器的单片机上,只要设置单片机定时器1ms中断,这样在主循环中定时时间到了就会调用相应的函数,这样我们就有了无数个软件定时器使用,软件定时器可以启动,停止,一次性的。从别的网站下载的,分享给大家。具体参考附件程序。
0.png

单片机源程序如下:
  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. }
复制代码

所有资料51hei提供下载:
MultiTimer.rar (2.14 KB, 下载次数: 63)

评分

参与人数 1黑币 +90 收起 理由
admin + 90 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:135781 发表于 2020-4-20 08:35 | 显示全部楼层
时钟_timer_ticks不清零,timeout一直累加,这样会出问题的
回复

使用道具 举报

ID:135781 发表于 2020-4-20 09:30 | 显示全部楼层
这个程序里面会出现一个问题,就是数据溢出的问题,把判断条件改下就行了
if(_timer_ticks >= target->timeout)改为 if(_timer_ticks == target->timeout)
这样就不会在数据溢出后从零开始出现的误判问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表