FreeRTOS 定时器使用
实验目的
本实验旨在学习和实现 FreeRTOS 中定时器的基本使用方法,包括定时器的创建、启动以及回调函数的设置。
实验平台
- 硬件: STM32F103C8T6
- 软件: MDK-ARM(Keil uVision)
- 操作系统: Windows 11
- 开发环境: VSCode
实验步骤
1. 创建 FreeRTOS 项目
使用 MDK-ARM 创建一个新的 FreeRTOS 项目,并配置好必要的外设(如 USART2 用于串口通信)。
2. 配置定时器
在 main.c 文件中配置两个定时器:
- Timer1: 每 500ms 触发一次回调函数,周期性执行。
- Timer2: 每 5000ms 触发一次回调函数,单次执行。
3. 编写定时器回调函数
为每个定时器编写回调函数,用于在定时器触发时执行特定任务:
- Timer1_callback: 记录定时器触发次数,并打印当前的系统滴答计数。
- Timer2_callback: 打印当前的系统滴答计数。
4. 初始化定时器
在 InitializeTasks 函数中初始化定时器,并启动它们:
void InitializeTasks(void *pvParameters)
{
taskENTER_CRITICAL();
Timer1_Handle = xTimerCreate("Timer1", pdMS_TO_TICKS(500), pdTRUE, NULL, Timer1_callback);
if (Timer1_Handle != NULL)
{
xTimerStart(Timer1_Handle, 0); // 启动定时器1
printf("Timer1 created and started successfully\r\n");
}
Timer2_Handle = xTimerCreate("Timer2", pdMS_TO_TICKS(5000), pdFALSE, NULL, Timer2_callback);
if (Timer2_Handle != NULL)
{
xTimerStart(Timer2_Handle, 0); // 启动定时器2
printf("Timer2 created and started successfully\r\n");
}
xTaskCreate(LED_Task, "LED_Task", configMINIMAL_STACK_SIZE, NULL, 2, &LED_Task_Handle);
vTaskDelete(InitializeTasks_Handle); // Delete the initialization task after creating other tasks
taskEXIT_CRITICAL();
}
5. 创建 LED 任务
创建一个 LED 任务,用于每隔 500ms 切换 LED 的状态:
void LED_Task(void *pvParameters)
{
while (1)
{
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // 切换LED状态
vTaskDelay(pdMS_TO_TICKS(500)); // 延时500毫秒
}
}
6. 启动 FreeRTOS 调度器
在 main 函数中启动 FreeRTOS 调度器:
int main(void)
{
/* MCU Configuration--------------------------------------------------------*/
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
/* Infinite loop */
printf("FreeRTOS STM32F1xx Example\r\n");
xTaskCreate(InitializeTasks, "init_Task", configMINIMAL_STACK_SIZE, NULL, 1, &InitializeTasks_Handle);
vTaskStartScheduler();
while (1)
{
/* USER CODE END WHILE */
}
}
7. 编译和烧录
使用 MDK-ARM 进行项目编译,并将生成的 .hex 文件烧录到 STM32F103C8T6 开发板上。
8. 观察输出
通过串口监视器观察输出结果,验证定时器是否按预期工作。
实验结果
根据串口监视器的输出,实验结果如下:
FreeRTOS STM32F1xx Example
Timer1 created and started successfully
Timer2 created and started successfully
Timer1 callback executed 1 times, Current tick count: 500
Timer1 callback executed 2 times, Current tick count: 1000
Timer1 callback executed 3 times, Current tick count: 1500
Timer1 callback executed 4 times, Current tick count: 2000
Timer1 callback executed 5 times, Current tick count: 2500
Timer1 callback executed 6 times, Current tick count: 3000
Timer1 callback executed 7 times, Current tick count: 3500
Timer1 callback executed 8 times, Current tick count: 4000
Timer1 callback executed 9 times, Current tick count: 4500
Timer1 callback executed 10 times, Current tick count: 5000
Timer2 callback executed, current tick count: 5005
Timer1 callback executed 11 times, Current tick count: 5500
Timer1 callback executed 12 times, Current tick count: 6000
Timer1 callback executed 13 times, Current tick count: 6500
Timer1 callback executed 14 times, Current tick count: 7000
Timer1 callback executed 15 times, Current tick count: 7500
Timer1 callback executed 16 times, Current tick count: 8000
Timer1 callback executed 17 times, Current tick count: 8500
Timer1 callback executed 18 times, Current tick count: 9000
Timer1 callback executed 19 times, Current tick count: 9500
Timer1 callback executed 20 times, Current tick count: 10000
Timer1 callback executed 21 times, Current tick count: 10500
结论
从实验结果可以看出,两个定时器均按预期工作:
- Timer1: 每 500ms 触发一次回调函数,周期性执行。
- Timer2: 每 5000ms 触发一次回调函数,单次执行。
LED 任务也按预期工作,每隔 500ms 切换一次 LED 的状态。
总结
通过本次实验,成功掌握了 FreeRTOS 中定时器的基本使用方法,包括定时器的创建、启动以及回调函数的设置。这为进一步学习和使用 FreeRTOS 提供了坚实的基础。
1179

被折叠的 条评论
为什么被折叠?



