我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮。

嵌入式框架Zorb Framework搭建过程

嵌入式框架Zorb Framework搭建一:嵌入式环境搭建、调试输出和建立时间系统

嵌入式框架Zorb Framework搭建二:环形缓冲区的实现

嵌入式框架Zorb Framework搭建三:列表的实现

嵌入式框架Zorb Framework搭建四:状态机的实现

嵌入式框架Zorb Framework搭建五:事件的实现

嵌入式框架Zorb Framework搭建六:定时器的实现

嵌入式框架Zorb Framework搭建七:任务的实现

一、前言

  在嵌入式开发中,我们常常会用到定时器,我们可以用芯片的定时器外设,可以用内核的systick,也可以使用操作系统的定时器。本篇要设计的定时器类似与操作系统的定时器,是软件定时器。如果Zorb Framework运行在操作系统上面,大可以不使用本篇的功能,直接使用操作系统自带的定时器。

二、定时器设计

  我们先来看看要实现的定时器提供什么功能:

  初步要提供的功能如下:

  1、可以设置定时时间

  2、可以设置定时器是单次还是重复运行

  3、可以设置定时器处理函数

  4、定时器函数可以直接运行或者推送异步事件

  5、可以打开和关闭定时器

  因此,初步设计的数据结构如下:

 /* 定时器处理程序 */
typedef void (*ITimerProcess)(void); /* 定时器结构 */
typedef struct _Timer
{
uint8_t Priority; /* 事件优先级 */
uint32_t Interval; /* 时间间隔(ms) */
uint32_t AlarmTime; /* 定时到达时间 */
bool IsAutoReset; /* 重复运行(默认开) */
bool IsRunning; /* 是否正在运行(默认关) */
/* 事件的处理者,事件将推送到处理者的队列 */
/* 不设置处理者则本地执行(挂载Timer_process的地方) */
EventHandler *pEventHandler;
/* 处理事件 */
ITimerProcess TimerProcess; /* 开始定时器 */
void (*Start)(struct _Timer * const pTimer); /* 关闭定时器 */
void (*Stop)(struct _Timer * const pTimer); /* 重新运行定时器 */
void (*Restart)(struct _Timer * const pTimer); /* 销毁定时器(释放空间) */
bool (*Dispose)(struct _Timer * const pTimer);
} Timer;

  事件和事件处理器已经设计好了,再把定时器的处理程序添加到系统滴答程序中即可:

 /******************************************************************************
* 描述 :系统滴答程序(需挂在硬件的时间中断里边)
* 参数 :无
* 返回 :无
******************************************************************************/
void ZF_timeTick (void)
{
/* 系统滴答计数 */
ZF_tick++; /* 软件定时器程序 */
Timer_process();
}

  具体实现请看附件代码或在文末的github地址拉框架源码。

三、定时器结果测试

  简单的测试代码如下:

 /**
*****************************************************************************
* @file app_timer.c
* @author Zorb
* @version V1.0.0
* @date 2018-06-28
* @brief 定时器测试的实现
*****************************************************************************
* @history
*
* 1. Date:2018-06-28
* Author:Zorb
* Modification:建立文件
*
*****************************************************************************
*/ #include "app_timer.h"
#include "zf_includes.h" /* 事件处理器 */
static EventHandler *pEventHandler;
/* 定时器1 */
static Timer *pTimer1;
/* 定时器2 */
static Timer *pTimer2; /******************************************************************************
* 描述 :定时器程序1
* 参数 :void
* 返回 :无
******************************************************************************/
void TimerProcess1(void)
{
ZF_DEBUG(LOG_D, "%dms:timer process 1 run\r\n", ZF_SYSTIME_MS());
} /******************************************************************************
* 描述 :定时器程序2
* 参数 :void
* 返回 :无
******************************************************************************/
void TimerProcess2(void)
{
ZF_DEBUG(LOG_D, "%dms:timer process 2 run\r\n", ZF_SYSTIME_MS());
} /******************************************************************************
* 描述 :任务初始化
* 参数 :无
* 返回 :无
******************************************************************************/
void App_Timer_init(void)
{
/* 初始化事件处理器 */
EventHandler_create(&pEventHandler); /* 创建定时器1 */
Timer_create(&pTimer1);
pTimer1->Priority = ;
pTimer1->Interval = ;
pTimer1->TimerProcess = TimerProcess1;
pTimer1->IsAutoReset = true;
pTimer1->pEventHandler = pEventHandler;
pTimer1->Start(pTimer1); /* 创建定时器2 */
Timer_create(&pTimer2);
pTimer2->Priority = ;
pTimer2->Interval = ;
pTimer2->TimerProcess = TimerProcess2;
pTimer2->IsAutoReset = true;
pTimer2->pEventHandler = pEventHandler;
pTimer2->Start(pTimer2);
} /******************************************************************************
* 描述 :任务程序
* 参数 :无
* 返回 :无
******************************************************************************/
void App_Timer_process(void)
{
while()
{
/* 执行事件 */
if (pEventHandler->GetEventCount(pEventHandler) > )
{
pEventHandler->Execute(pEventHandler);
}
else
{
/* 可在此实现低功耗 */
}
}
}
/******************************** END OF FILE ********************************/

  结果:

500ms:timer process  run
1000ms:timer process run
1002ms:timer process run
1500ms:timer process run
2000ms:timer process run
2002ms:timer process run
2500ms:timer process run
3000ms:timer process run
3002ms:timer process run
3500ms:timer process run
4000ms:timer process run
4002ms:timer process run
4500ms:timer process run
5000ms:timer process run
5002ms:timer process run 省略...

  在测试程序中,定时器1周期为500ms,定时器2周期为1000ms。至于定时器2程序第一次执行的时间为1002ms的原因:定时器1和定时器2同时在1000ms处响应,但定时器1 的优先级比定时器2的优先级高,因此事件处理器先处理完定时器1的事件再处理定时器2的事件,而调试串口波特率115200,定时器1程序把调试数据发送完的时间大约2ms,因此定时器2的第一次执行时间为1002ms。

四、最后

  本篇为Zorb Framework提供了定时器功能。在对定时精度要求不高(毫秒级),完全可以使用软件定时器。软件定时器是在硬件定时器的基础上开发的,好处在于可以挂载多个定时器,不用再为芯片的定时器资源不够而烦恼。

  Zorb Framework github:https://github.com/54zorb/Zorb-Framework

  版权所有,转载请打赏哟

如果你喜欢我的文章,可以通过微信扫一扫给我打赏哟

最新文章

  1. php中计算二维数组中某一元素之和
  2. php设计模式 Proxy (代理模式)
  3. C++基础(纯虚函数与抽象类)
  4. [转]win7下apache2.4响应很慢解决方法
  5. 查看Eclipse版本号的方法
  6. Java内部类——局部内部类
  7. HDU 2094解题报告
  8. restful架构风格设计准则(二)以资源为中心,一个url
  9. linux 退出当前命令的编辑
  10. jacoco + eclipse单元测试覆盖率
  11. Mac 系统下 mysql 的安装与配置
  12. junit中test注解测试使用案列解析一
  13. docker运行python3.6+flask小记
  14. salt-api使用
  15. windows rails new demo时候出错Make sure that `gem install mysql2 -v '0.3.15'` succeeds before bundling.
  16. #pragma once含义及用法
  17. iPhone设备及屏幕适配
  18. vue组件详解(一)——组件与复用
  19. 浮动&定位
  20. excel 笔记

热门文章

  1. 进程状态转换及其PCB的变化
  2. linux中启动网卡报错:Bringing up interface eth1: Error: Connection activation failed
  3. WAKE-WIN10-SOFT-CMAKE
  4. 【转载】#440 - A Class Can Implement More than One Interface
  5. LightOJ-1028 Trailing Zeroes (I)---因子数目
  6. Gym 101308I Inspection
  7. 单独使用JDBC编程
  8. Windows XP和Wndows7误删除了注册表下.exe文件夹之修复办法
  9. java反射 反射构造函数 报 wrong number of arguments 错误
  10. android(eclipse)广播机制知识梳理(三)