您的位置:首页 > 文旅 > 美景 > vTaskDelay 和 vTaskDelayUntil 的区别

vTaskDelay 和 vTaskDelayUntil 的区别

2024/12/23 7:29:23 来源:https://blog.csdn.net/qq_40008325/article/details/142032156  浏览:    关键词:vTaskDelay 和 vTaskDelayUntil 的区别

1,有两个任务,一个任务 Task1 是用的 vTaskDelay 延迟,延迟 10ms, 另一个任务 Task2 是用的vTaskDelayUntil 延迟,延迟 10ms。vTaskDelayUntil 是绝对延迟, vTaskDelay 是相对延迟。 

Task1:
 vTaskDelay (pdMS_TO_TICKS(rand() % 500)) + vTaskDelay (pdMS_TO_TICKS(1000))
|----xms(500ms以内 + 1000ms 为一个周期----|
这个就是相对性的含义
◆ Task2:
bsp_KeyScan + vTaskDelayUntil ---------> bsp_KeyScan + vTaskDelayUntil

 vTaskDelay (pdMS_TO_TICKS(rand() % 500)) + vTaskDelayUntil(pdMS_TO_TICKS(1000))
|----xms(500ms以内 + 延时Y ms|= 1000ms

#include "FreeRTOS.h"
#include "task.h"
#include<stdio.h>void vApplicationMallocFailedHook() {while(1);
}void vApplicationStackOverflowHook(TaskHandle_t xTask, char * pcTaskName ) {while(1);
}#include "semphr.h"// 用于任务调用的宏定义
#define mainDELAY_LOOP_COUNT    ( 0xffffff )// 定义任务句柄
TaskHandle_t xTask1Handle = NULL;
TaskHandle_t xTask2Handle = NULL;// 定义足够大的缓冲区用于存储统计信息
#define RUN_TIME_STATS_BUFFER_SIZE 2048
char runTimeStatsBuffer[RUN_TIME_STATS_BUFFER_SIZE];#define TASK_PRIORITY   (tskIDLE_PRIORITY + 1)void vTaskDelayExample(void *pvParameters)
{TickType_t xLastWakeTime = xTaskGetTickCount();const TickType_t xDelay = pdMS_TO_TICKS(1000); // 延时1000毫秒int j = 0;for (;;){// 打印当前时间printf("vTaskDelayExample: Before delay, tick count = %lu\n", xTaskGetTickCount());vTaskDelay(pdMS_TO_TICKS(rand() % 500)); // 延时0-500毫秒// 延时vTaskDelay(pdMS_TO_TICKS(1000));// 打印延时后的时间printf("vTaskDelayExample: After delay, tick count = %lu\n", xTaskGetTickCount());}
}void xTaskDelayUntilExample(void *pvParameters)
{const TickType_t xFrequency = pdMS_TO_TICKS(1000); // 任务周期1000毫秒TickType_t xLastWakeTime = xTaskGetTickCount();int j = 0;for (;;){// 打印当前时间printf("xTaskDelayUntilExample: Before delay, tick count = %lu\n", xTaskGetTickCount());// 执行任务代码...vTaskDelay(pdMS_TO_TICKS(rand() % 500)); // 延时0-500毫秒// 延时直到下一个周期xTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(1000));// 打印延时后的时间printf("xTaskDelayUntilExample: After delay, tick count = %lu\n", xTaskGetTickCount());}
}int main(void)
{// 创建任务//xTaskCreate(vTaskDelayExample, "TaskDelay", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, NULL);xTaskCreate(xTaskDelayUntilExample, "TaskDelayUntil", configMINIMAL_STACK_SIZE, NULL, TASK_PRIORITY, NULL);// 启动调度器vTaskStartScheduler();// 如果调度器启动失败,则不会到达这里for (;;);
}

 (1)TaskDelay任务打印tick统计

vTaskDelayExample: Before delay, tick count = 0
vTaskDelayExample: After delay, tick count = 138
vTaskDelayExample: Before delay, tick count = 138
vTaskDelayExample: After delay, tick count = 276
vTaskDelayExample: Before delay, tick count = 276
vTaskDelayExample: After delay, tick count = 403
vTaskDelayExample: Before delay, tick count = 403
vTaskDelayExample: After delay, tick count = 544
vTaskDelayExample: Before delay, tick count = 544
vTaskDelayExample: After delay, tick count = 673
vTaskDelayExample: Before delay, tick count = 673
vTaskDelayExample: After delay, tick count = 806
vTaskDelayExample: Before delay, tick count = 806 

(2) TaskDelayUntil任务打印tick统计

xTaskDelayUntilExample: Before delay, tick count = 0
xTaskDelayUntilExample: After delay, tick count = 100
xTaskDelayUntilExample: Before delay, tick count = 100
xTaskDelayUntilExample: After delay, tick count = 200
xTaskDelayUntilExample: Before delay, tick count = 200
xTaskDelayUntilExample: After delay, tick count = 300
xTaskDelayUntilExample: Before delay, tick count = 300
xTaskDelayUntilExample: After delay, tick count = 400
xTaskDelayUntilExample: Before delay, tick count = 400
xTaskDelayUntilExample: After delay, tick count = 500
xTaskDelayUntilExample: Before delay, tick count = 500
xTaskDelayUntilExample: After delay, tick count = 600
xTaskDelayUntilExample: Before delay, tick count = 600
xTaskDelayUntilExample: After delay, tick count = 700
xTaskDelayUntilExample: Before delay, tick count = 700

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com