如何使用此API" xTaskCreate"创建多个任务?

时间:2017-11-29 14:43:51

标签: freertos

我已阅读链接,此xTaskCreate FreeRTOS API用于创建任务。使用此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

我不明白如何编写两个任务的程序。

1 个答案:

答案 0 :(得分:1)

创建第二个任务只需拨打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();
}