任务挂起函数
vTaskSuspend(Task1_Handler);
任务恢复函数
vTaskResume(Task1_Handler);
任务挂起后 进入就绪态 会停止运行
实验:创建三个任务 任务一打印 任务二打印 任务三控制任务一的挂起和恢复
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#include "key.h"#define START_TASK_PRIO 1 //任务优先级
#define START_STK_SIZE 120 //任务堆栈
TaskHandle_t Start_Handler;
void start_task(void *pvParameters);#define Task1_TASK_PRIO 3 //任务优先级
#define Task1_STK_SIZE 120 //任务堆栈
TaskHandle_t Task1_Handler;
void task1_task(void *pvParameters);#define Task2_TASK_PRIO 4 //任务优先级
#define Task2_STK_SIZE 120 //任务堆栈
TaskHandle_t Task2_Handler;
void task2_task(void *pvParameters);#define KEY_TASK_PRIO 2 //任务优先级
#define KEY_STK_SIZE 120 //任务堆栈
TaskHandle_t KEY_Handler;
void key_task(void *pvParameters);//*******************下面注释掉的代码是通过 位带 操作实现IO口控制**************************************int main(void)
{ uart_init(115200);delay_init(168); //初始化延时函数LED_Init(); //初始化LED端口KEY_Init(); while(1){xTaskCreate((TaskFunction_t) start_task,(char *) "start_task",(uint16_t) START_STK_SIZE,(void *) NULL,(UBaseType_t) START_TASK_PRIO,(TaskHandle_t *) &Start_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */vTaskStartScheduler();// 开启任务调度器 注意一定要开启}
}//创建开始任务
void start_task(void *pvParameters)
{//创建task1xTaskCreate((TaskFunction_t) task1_task,(char *) "task1_task",(uint16_t) Task1_STK_SIZE,(void *) NULL,(UBaseType_t) Task1_TASK_PRIO,(TaskHandle_t *) &Task1_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. *///创建task2xTaskCreate((TaskFunction_t) task2_task,(char *) "task2_task",(uint16_t) Task2_STK_SIZE,(void *) NULL,(UBaseType_t) Task2_TASK_PRIO,(TaskHandle_t *) &Task2_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
//创建KEY_task任务
xTaskCreate((TaskFunction_t) key_task,(char *) "key_task",(uint16_t) KEY_STK_SIZE,(void *) NULL,(UBaseType_t) KEY_TASK_PRIO,(TaskHandle_t *) &KEY_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
vTaskDelete(Start_Handler); }void task1_task(void *pvParameters)
{static int i=0;while(1){i++;printf("task 1 运行了%d次\r\n",i);vTaskDelay(1000);}}
void task2_task(void *pvParameters)
{static int a=0;while(1){a++;printf("task 2 运行了 %d 次\r\n",a);vTaskDelay(1000);}}
void key_task(void *pvParameters)
{u8 key;printf("key_task is ok!!!\r\n");while(1){key=KEY_Scan(0);switch(key){case 1:vTaskSuspend(Task1_Handler); //任务挂起printf("Task1 Suspend!!!\r\n");break;case 2:vTaskResume(Task1_Handler); //恢复任务运行printf("Task1 Resume!!!\r\n");break;}vTaskDelay(10);}
}