【FreeRTOS】小白进阶之如何创建FreeRTOS任务(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liwei16611/article/details/82531316

该文章主要介绍任务中使用的系统延时:通过 TICKS 实现。

1、头文件

#include "FreeRTOS.h"
#include "task.h"
#include "supporting_functions.h"

void vTaskFunction( void *pvParameters );

2、定义任务打印字符串

const char *pcTextForTask1 = "Task 1 is running\r\n";
const char *pcTextForTask2 = "Task 2 is running\r\n";

3、启动任务

int main( void )
{
	xTaskCreate( vTaskFunction, "Task 1", 1000, (void*)pcTextForTask1, 1, NULL );
	xTaskCreate( vTaskFunction, "Task 2", 1000, (void*)pcTextForTask2, 2, NULL );

	vTaskStartScheduler();

	for( ;; );
	return 0;
}

4、任务处理函数

void vTaskFunction( void *pvParameters )
{
    char *pcTaskName;
    TickType_t xLastWakeTime;
   
    // 实现 MS 延时
    const TickType_t xDelay250ms = pdMS_TO_TICKS( 250UL );
	pcTaskName = ( char * ) pvParameters;

	// 记录当前时钟计数
	xLastWakeTime = xTaskGetTickCount();

	for( ;; )
	{
		// 打印传递给任务的参数
		vPrintString( pcTaskName );

		// 延时并更新时钟计数到 xLastWakeTime
		vTaskDelayUntil( &xLastWakeTime, xDelay250ms );
	}
}

猜你喜欢

转载自blog.csdn.net/liwei16611/article/details/82531316