如何使用这个API"xTaskCreate"创建多个任务?
How to use this API "xTaskCreate" to create multiple tasks?
我在link看过,这个xTaskCreate FreeRTOSAPI是用来创建任务的。使用这个 API 我们可以创建更多的任务:
/* Task to be created. */
/* Task to be created. */
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
/* Task code goes here. */
}
}
我看过这个程序示例。我想创建两个任务。第一个任务 blink led1
,第二个任务 blink led2
。
我不明白如何为两个任务编写程序。
要创建第二个任务,只需调用 xTaskCreate
两次,如下所示:
void vTaskLedGreen( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
vTaskDelay(pdMS_TO_TICKS(1000));
GreenLedOff();
vTaskDelay(pdMS_TO_TICKS(1000));
GreenLedOn();
}
}
void vTaskLedRed( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
vTaskDelay(pdMS_TO_TICKS(1000));
RedLedOff();
vTaskDelay(pdMS_TO_TICKS(1000));
RedLedOn();
}
}
/* Function that creates a task. */
void main( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
xReturned = xTaskCreate(
vTaskLedRed, /* Function that implements the task. */
"RedLed", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned != pdPASS )
{
ErrorHandler();
}
xReturned = xTaskCreate(
vTaskLedGreen, /* Function that implements the task. */
"GreenLed", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned != pdPASS )
{
ErrorHandler();
}
// Start the real time scheduler.
vTaskStartScheduler();
}
我在link看过,这个xTaskCreate FreeRTOSAPI是用来创建任务的。使用这个 API 我们可以创建更多的任务:
/* Task to be created. */
/* Task to be created. */
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
/* Task code goes here. */
}
}
我看过这个程序示例。我想创建两个任务。第一个任务 blink led1
,第二个任务 blink led2
。
我不明白如何为两个任务编写程序。
要创建第二个任务,只需调用 xTaskCreate
两次,如下所示:
void vTaskLedGreen( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
vTaskDelay(pdMS_TO_TICKS(1000));
GreenLedOff();
vTaskDelay(pdMS_TO_TICKS(1000));
GreenLedOn();
}
}
void vTaskLedRed( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below.
configASSERT( ( ( uint32_t ) pvParameters ) == 1 );
for( ;; )
{
vTaskDelay(pdMS_TO_TICKS(1000));
RedLedOff();
vTaskDelay(pdMS_TO_TICKS(1000));
RedLedOn();
}
}
/* Function that creates a task. */
void main( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;
xReturned = xTaskCreate(
vTaskLedRed, /* Function that implements the task. */
"RedLed", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned != pdPASS )
{
ErrorHandler();
}
xReturned = xTaskCreate(
vTaskLedGreen, /* Function that implements the task. */
"GreenLed", /* Text name for the task. */
STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&xHandle ); /* Used to pass out the created task's handle. */
if( xReturned != pdPASS )
{
ErrorHandler();
}
// Start the real time scheduler.
vTaskStartScheduler();
}