freertos定时器Demo
时间: 2023-09-29 18:07:57 浏览: 190
在FreeRTOS中,定时器函数不是在Tick中断中执行的。Tick中断只负责判断定时器是否超时,而定时器函数的执行是在任务中完成的。你可以通过创建一个任务来执行定时器函数。下面是一个示例:
```c
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
TimerHandle_t TimerHandle1;
TimerHandle_t TimerHandle2;
static void timer1Intterp(TimerHandle_t timer1) {
printf("timer1 is doing\r\n");
}
static void timer2Intterp(TimerHandle_t timer2) {
printf("timer2 is doing\r\n");
}
void timerTask(void *pvParameters) {
TimerHandle_t timer = (TimerHandle_t)pvParameters;
while(1) {
vTaskDelay(pdMS_TO_TICKS(100));
if (xTimerIsTimerActive(timer) && xTimerIsTimerExpiredFromISR(timer)) {
xTimerReset(timer, 0);
if (timer == TimerHandle1) {
timer1Intterp(TimerHandle1);
} else if (timer == TimerHandle2) {
timer2Intterp(TimerHandle2);
}
}
}
}
int main( void ) {
prvSetupHardware();
TimerHandle1 = xTimerCreate("timer1", pdMS_TO_TICKS(100), pdFALSE, 0, NULL);
TimerHandle2 = xTimerCreate("timer2", pdMS_TO_TICKS(100), pdTRUE, 0, NULL);
xTaskCreate(timerTask, "timerTask1", configMINIMAL_STACK_SIZE, (void *)TimerHandle1, tskIDLE_PRIORITY + 1, NULL);
xTaskCreate(timerTask, "timerTask2", configMINIMAL_STACK_SIZE, (void *)TimerHandle2, tskIDLE_PRIORITY + 1, NULL);
vTaskStartScheduler();
return 0;
}
```
在这个示例中,我们创建了两个定时器:TimerHandle1和TimerHandle2,并且分别创建了两个任务timerTask1和timerTask2来执行定时器函数timer1Intterp和timer2Intterp。定时器函数的执行是在任务中完成的。
阅读全文