定时器
硬件定时器:
CPU内部自带的定时器模块,通过初始化、配置可以实现定时,定时时间到以后就会执行相应的定时器中断处理函数。硬件定时器一般都带有其他功能,比如PWM输出、输入捕获等功能。但缺点是硬件定时器数量少!!!
软件定时器:
软件定时器允许设置一段时间,当设置的时间到达以后就执行指定的功能函数,被定时器调用的这个功能函数就叫做定时器的回调函数。回调函数的两次执行间隔,就叫做定时器的定时周期。简而言之,当定时器的定时周期到了就会执行回调函数。
回调函数的注意事项:
软件定时器的回调函数是在定时器服务任务中执行的,所以一定不能在回调函数中调用任何会阻塞任务的 API 函数!比如,定时器回调函数中千万不能调用 vTaskDelay()、vTaskDelayUnti(),还有一些访问队列或者信号量的非零阻塞时间的 API 函数也不能调用。
定时器服务
FreeRTOS软件定时器的API函数,大多都使用队列发送命令给定时器服务任务。这个队列叫做定时器命令队列。定时器命令队列是提供给FreeRTOS的软件定时器使用的,用户不能直接访问!通信过程如下所示:

通信过程详解:

定时器服务任务,是在任务调度器中创建的:

在创建定时器服务任务中,有创建定时器任务的函数:

定时器服务任务函数分析:

定时器消息队列的命令,是在第3个函数中处理的。
其中,创建队列的函数如下所示:
首先创建了一个定时器消息队列结构体:

接下来创建定时器命令消息队列:

定时器相关配置:
之前提到,软件定时器有一个定时器服务任务和定时器命令队列,这两个东西肯定是要配置的,配置方法和之前其他条件编译 一样,而且相关的配置也是放到文件 FreeRTOSConfig.h 中的,涉及到的配置如下:

单次定时器和周期定时器:

定时器相关的操作与API函数:
1 复位软件定时器




2 创建软件定时器:


这个宏 portTICK_PERIOD_MS 非常有用,如下所示:
#define configTICK_RATE_HZ ((TickType_t)1000)
#define portTICK_PERIOD_MS ((TickType_t)1000 / configTICK_RATE_HZ)
其中,configTICK_RATE_HZ 为每秒的 Tick 频率,portTICK_PERIOD_MS 则为每 Tick 一次需要多少 ms(周期ms)。
这个宏定义,可以用于将时间转换成 Tick 数,即 Tick 数 = 总时间 / (每次 Tick 的时间)
如我们需要延时800ms,则 Tick_count = 800 / (portTICK_PERIOD_MS)
3 开启定时器:



4 停止定时器:



测试实验
软件定时器测试实验:
创建两个任务, start_task 用于创建 timer_ctrl_task 和两个软件定时器;
周期定时器(周期1000节拍),单次定时器(周期2000节拍)
timer_ctrl_task 任务通过检测2个按键,来开始或者停止两个软件定时器;
程序很简单,如下所示:
TimerHandle_t auto_reload_timer_handle;
TimerHandle_t one_shot_timer_handle;
void start_task(void *pvParameters)
auto_reload_timer_handle = xTimerCreate((char * )"auto_reload_timer",
(TickType_t )(1000 / portTICK_PERIOD_MS),
(TimerCallbackFunction_t)auto_reload_timer_callback);
one_shot_timer_handle = xTimerCreate((char * )"one_shot_timer",
(TickType_t )(2000 / portTICK_PERIOD_MS),
(TimerCallbackFunction_t)one_shot_timer_callback);
xTaskCreate((TaskFunction_t )timer_ctrl_task,
(char * )"timer_ctrl_task",
(uint16_t )TIMER_TASK_SIZE,
(UBaseType_t )TIMER_TASK_PRIO,
(TaskHandle_t * )&timer_task_Handle);
vTaskDelete(Start_Task_Handle);
void timer_ctrl_task(void *pvParameters)
printf("\r\ntimer_ctrl_task start!\r\n");
printf("press KEY1 to start auto_reload_timer\r\n");
printf("press KEY2 to start one_shot_timer\r\n");
if (key_scan(KEY1_GPIO_Port, KEY1_Pin) == KEY_ON)
if (xTimerStart(auto_reload_timer_handle, 0) == pdFAIL)
printf("auto_reload_timer start failed\r\n");
if (key_scan(KEY2_GPIO_Port, KEY2_Pin) == KEY_ON)
if (xTimerStart(one_shot_timer_handle, 0) == pdFAIL)
printf("one_shot_timer start failed\r\n");
void auto_reload_timer_callback(TimerHandle_t pxTimer)
static uint16_t times = 0;
printf("auto_reload_timer running = %d times\r\n", ++times);
void one_shot_timer_callback(TimerHandle_t pxTimer)
static uint16_t times = 0;
printf("one_shot_timer running = %d times\r\n", ++times);
程序运行结果如下:

多定时器共用回调函数:
前面提到,创建定时器时有一个pvTimerID,可以用于区分不同的定时器,如下所示:

函数原型如下所示:
其实就是根据定时器句柄,来获取 TimerID

通过在回调函数中调用这个函数获取ID,就能直到当前是哪个定时器调用回调函数,如下所示:
void timer_common_callback(TimerHandle_t pxTimer)
static uint16_t one_shot_times = 0;
static uint16_t auto_reload_times = 0;
if (pvTimerGetTimerID(pxTimer) == (void *)1)
printf("周期定时器运行%d次\r\n", ++auto_reload_times);
if (pvTimerGetTimerID(pxTimer) == (void *)2)
printf("单次定时器运行%d次\r\n", ++one_shot_times);
测试结果和之前分开写回调函数一致,如下所示:

既然创建定时器时,已经给每个定时器创建了句柄,那么定时器回调函数中,应该也可以直接通过句柄来进行当前是那个定时器在调用回调函数,如下所示:
void timer_common_callback(TimerHandle_t pxTimer)
static uint16_t one_shot_times = 0;
static uint16_t auto_reload_times = 0;
if (pvTimerGetTimerID(pxTimer) == (void *)1)
printf("周期定时器运行%d次\r\n", ++auto_reload_times);
if (pvTimerGetTimerID(pxTimer) == (void *)2)
printf("单次定时器运行%d次\r\n", ++one_shot_times);
if (pxTimer == auto_reload_timer_handle)
printf("周期定时器运行%d次\r\n", ++auto_reload_times);
if (pxTimer == one_shot_timer_handle)
printf("单次定时器运行%d次\r\n", ++one_shot_times);
两种方式的运行结果相同,但推荐使用获取ID的方式,因为这是FreeRTOS推荐的。
转载于:https://blog.csdn.net/dingyc_ee/article/details/104118508
商业资讯 2021-7-29 14:59
pidaneng 2020-9-2 01:00
abner_ma 2020-8-26 19:04