顾名思义 免费的实时操作系统
用法基本和Linux下的多线程编程类似
探索者开发版实验
动态创建4个任务start_task task1 task2 task3 优先级依次为1 2 3 4 (注意优先级不能为0,0是空闲任务)
我的理解:主线程start_task 主线程 task1 task2 task3为子线程
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "FreeRTOS.h"
#include "task.h"
#define START_TASK_PRIO 1 //任务优先级
#define START_STK_SIZE 120 //任务堆栈
TaskHandle_t Start_Handler;
void start_task(void *pvParameters);
#define Task1_TASK_PRIO 2 //任务优先级
#define Task1_STK_SIZE 120 //任务堆栈
TaskHandle_t Task1_Handler;
void task1_task(void *pvParameters);
#define Task2_TASK_PRIO 3 //任务优先级
#define Task2_STK_SIZE 120 //任务堆栈
TaskHandle_t Task2_Handler;
void task2_task(void *pvParameters);
#define Task3_TASK_PRIO 4 //任务优先级
#define Task3_STK_SIZE 120 //任务堆栈
TaskHandle_t Task3_Handler;
void task3_task(void *pvParameters);
//*******************下面注释掉的代码是通过 位带 操作实现IO口控制**************************************
int main(void)
{
uart_init(115200);
delay_init(168); //初始化延时函数
LED_Init(); //初始化LED端口
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)
{
//创建task1
xTaskCreate((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. */
//创建task2
xTaskCreate((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. */
//创建task3
xTaskCreate((TaskFunction_t) task3_task,
(char *) "task3_task",
(uint16_t) Task3_STK_SIZE,
(void *) NULL,
(UBaseType_t) Task3_TASK_PRIO,
(TaskHandle_t *) &Task3_Handler ); /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
vTaskDelete(Start_Handler);
}
void task1_task(void *pvParameters)
{
while(1)
{
printf("task 1 is ok\r\n");
vTaskDelay(500);
}
}
void task2_task(void *pvParameters)
{
while(1)
{
printf("task 2 is ok\r\n");
vTaskDelay(800);
}
}
void task3_task(void *pvParameters)
{
while(1)
{
printf("task 3 is ok\r\n");
vTaskDelay(1000);
}
}
n: 运行态的任务调用vTaskDelay(),将把自己从就绪列表搬移到主动延时列表,并让就绪表的其他任务获取CPU资源。
h: 延时态的任务在延时结束时,若优先级比当前任务优先级高,就加入就绪队列,并立即获得CPU资源,进入运行。
g: 延时态的任务在延时结束时,若优先级比当前任务优先级低,就加入就绪队列,但是由于优先级不够,只能等待获取CPU资源。
如果不加延时 会一直执行高优先级的任务