软件定时器
软件定时器简介
软件定时器相关API函数
软件定时器实验
1、实验目的:学习 FreeRTOS 的软件定时器相关API函数的使用 。
2、实验设计:将设计两个任务:start_task、task1
start_task:用来创建task1任务,并创建两个定时器(单次和周期)
task1:用于按键扫描,并对软件定时器进行开启、停止操作
freertos_demo.c
#include "freertos_demo.h"
#include "./SYSTEM/usart/usart.h"
#include "./BSP/LED/led.h"
#include "./BSP/LCD/lcd.h"
#include "./BSP/KEY/key.h"
/*FreeRTOS*********************************************************************************************/
#include "FreeRTOS.h"
#include "task.h"
#include "event_groups.h"
/******************************************************************************************************/
/*FreeRTOS配置*/
/* START_TASK 任务 配置
* 包括: 任务句柄 任务优先级 堆栈大小 创建任务
*/
#define START_TASK_PRIO 1
#define START_TASK_STACK_SIZE 128
TaskHandle_t start_task_handler;
void start_task( void * pvParameters );
/******************************************************************************************************/
void timer1_callback( TimerHandle_t pxTimer );
void timer2_callback( TimerHandle_t pxTimer );
/* task1 任务 配置
* 包括: 任务句柄 任务优先级 堆栈大小 创建任务
*/
#define TASK1_PRIO 2
#define TASK1_STACK_SIZE 128
TaskHandle_t task1_handler;
void task1( void * pvParameters );
/**
* @brief FreeRTOS例程入口函数
* @param 无
* @retval 无
*/
void freertos_demo(void)
{
xTaskCreate( (TaskFunction_t ) start_task,
(char * ) "start_task",
(configSTACK_DEPTH_TYPE) START_TASK_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) START_TASK_PRIO,
(TaskHandle_t * ) &start_task_handler );
vTaskStartScheduler();
}
TimerHandle_t timer1_handle = 0; //单次定时器
TimerHandle_t timer2_handle = 0; //周期定时器
// Task to be created.
void start_task( void * pvParameters )
{
taskENTER_CRITICAL();
timer1_handle = xTimerCreate("timer1", 1000,pdFAIL,(void *)1,timer1_callback);
timer2_handle = xTimerCreate("timer2", 1000,pdTRUE,(void *)2,timer2_callback);
xTaskCreate( (TaskFunction_t ) task1,
(char * ) "task1",
(configSTACK_DEPTH_TYPE) TASK1_STACK_SIZE,
(void * ) NULL,
(UBaseType_t ) TASK1_PRIO,
(TaskHandle_t * ) &task1_handler );
vTaskDelete(start_task_handler);
taskEXIT_CRITICAL();
}
void task1( void * pvParameters )
{
uint8_t key = 0;
while(1)
{
key = key_scan(0);
if(key == KEY0_PRES)
{
printf("开启定时器1、定时器2\r\n");
xTimerStart(timer1_handle,portMAX_DELAY);
xTimerStart(timer2_handle,portMAX_DELAY);
}else if(key == KEY1_PRES)
{
printf("关闭定时器1、定时器2\r\n");
xTimerStop(timer1_handle,portMAX_DELAY);
xTimerStop(timer2_handle,portMAX_DELAY);
}
vTaskDelay(50);
}
}
void timer1_callback( TimerHandle_t pxTimer )
{
static uint32_t timer = 0;
printf("timer1运行次数:%d\r\n",++timer);
}
void timer2_callback( TimerHandle_t pxTimer )
{
static uint32_t timer = 0;
printf("timer2运行次数:%d\r\n",++timer);
}