一、前期准备
单片机:STM32F103ZET6
开发环境:MDK5.14
库函数:标准库V3.5
FreeRTOS:V9.0.0(网上下载,官网上目前是V10.0.1)
二、实验效果
总共6个任务,串口1、4个RGB小灯以不同频率闪烁、一个删除任务,并通过串口2打印调试信息。删除任务每秒进行一次自加。当加到3的时候删除第一个小灯的任务,以此类推直到第四个小灯任务删除,之后再自加3次删除自身。这时候只有串口1任务在工作。
三、程序讲解
任务创建函数:此为动态内存分配,由FreeRTOS的heap4.c分配
BaseType_t xTaskCreate(
TaskFunction_t pxTaskCode, /任务函数
const char * const pcName, //任务名称
const uint16_t usStackDepth, //任务堆栈大小
void *const pvParameters, //传递给任务函数的参数
UBaseType_t uxPriority, //任务优先级
TaskHandle_t * const pxCreateTask //任务句柄,删除任务也是删除任务句柄
);
任务删除函数:
vTaskDelete(
TaskHandle_t xTaskToDelete //任务句柄
);
四、测试代码
完整工程请加QQ:1002521871,验证:呵呵 。只附上与FreeRTOS相关的代码,其余串口,LED初始化请参考STM32F103ZE FreeRTOS移植、测试
rtos_app.h
#ifndef __RTOS_APP_H__
#define __RTOS_APP_H__
#include "conf.h"
#include "FreeRTOS.h"
#include "task.h"
#define START_TASK_PRIO 1
#define START_STK_SIZE 128
extern TaskHandle_t STARTTask_Handler;
extern void START_task(void *pvParameters);
#define LED0_TASK_PRIO 3
#define LED0_STK_SIZE 50
#define LED1_TASK_PRIO 4
#define LED1_STK_SIZE 50
#define LED2_TASK_PRIO 5
#define LED2_STK_SIZE 50
#define LED3_TASK_PRIO 6
#define LED3_STK_SIZE 50
#define UART1_TASK_PRIO 7
#define UART1_STK_SIZE 50
#define UART2_TASK_PRIO 7
#define UART2_STK_SIZE 50
#define Mess_TASK_PRIO 2
#define Mess_STK_SIZE 50
#define TEST_TASK_RPIO 7
#define TEST_STK_SIZE 128
#endif
rtos_app.c
#include "rtos_app.h"
TaskHandle_t STARTTask_Handler;
TaskHandle_t LED0Task_Handler;
TaskHandle_t LED1Task_Handler;
TaskHandle_t LED2Task_Handler;
TaskHandle_t LED3Task_Handler;
TaskHandle_t UART1Task_Handler;
TaskHandle_t UART2Task_Handler;
TaskHandle_t MessTask_Handler;
TaskHandle_t TESTTask_Handler;
void LED0_task(void *pvParameters)
{
while (1)
{
RGB_LED1 = ON;
vTaskDelay(200 / portTICK_RATE_MS);
RGB_LED1 = OFF;
vTaskDelay(800 / portTICK_RATE_MS);
}
}
void LED1_task(void *pvParameters)
{
while (1)
{
RGB_LED2 = ON;
vTaskDelay(300 / portTICK_RATE_MS);
RGB_LED2 = OFF;
vTaskDelay(700 / portTICK_RATE_MS);
}
}
void LED2_task(void *pvParameters)
{
while (1)
{
RGB_LED3 = ON;
vTaskDelay(400 / portTICK_RATE_MS);
RGB_LED3 = OFF;
vTaskDelay(600 / portTICK_RATE_MS);
}
}
void LED3_task(void *pvParameters)
{
while (1)
{
RGB_LED4 = ON;
vTaskDelay(500 / portTICK_RATE_MS);
RGB_LED4 = OFF;
vTaskDelay(500 / portTICK_RATE_MS);
}
}
void UART1_task(void *pvParameters)
{
int i = 0;
while (1)
{
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 1;
printf("UART1-%05d: FreeRTOS Test!rn", i);
i ++;
taskEXIT_CRITICAL();
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
void UART2_task(void *pvParameters)
{
int i = 0;
while (1)
{
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("UART2-%05d: FreeRTOS Test!rn", i);
i ++;
taskEXIT_CRITICAL();
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
void Mess_task(void *pvParameters)
{
while (1)
{
//taskENTER_CRITICAL();
if (cmdDealFlag == 1)
{
DealWithUARTMess(RecData);
cmdDealFlag = 0;
}
//taskEXIT_CRITICAL();
}
}
void TEST_task(void *pvParameters)
{
uint8_t times = 0;
while(1)
{
if (times == 3)
{
vTaskDelete(LED0Task_Handler);
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("Task_LED0 has already deleted!rn");
taskEXIT_CRITICAL();
}
else if (times == 6)
{
vTaskDelete(LED1Task_Handler);
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("Task_LED1 has already deleted!rn");
taskEXIT_CRITICAL();
}
else if (times == 9)
{
vTaskDelete(LED2Task_Handler);
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("Task_LED2 has already deleted!rn");
taskEXIT_CRITICAL();
}
else if (times == 12)
{
vTaskDelete(LED3Task_Handler);
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("Task_LED3 has already deleted!rn");
taskEXIT_CRITICAL();
}
else if (times == 15)
{
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("Test task has already deleted!rn");
taskEXIT_CRITICAL();
vTaskDelete(TESTTask_Handler);
taskEXIT_CRITICAL();
}
taskENTER_CRITICAL();
USART_PRINTF_FLAG = 2;
printf("Times = %drn", times);
taskEXIT_CRITICAL();
times ++;
vTaskDelay(1000 / portTICK_RATE_MS);
}
//vTaskDelete(NULL);
}
void START_task(void *pvParameters)
{
taskENTER_CRITICAL();
xTaskCreate(
(TaskFunction_t) TEST_task,
(const char *) "TEST_task",
(uint16_t) TEST_STK_SIZE,
(void *) NULL,
(UBaseType_t) TEST_TASK_RPIO,
(TaskHandle_t *) &TESTTask_Handler
);
xTaskCreate(
(TaskFunction_t) LED0_task,
(const char *) "LED0_task",
(uint16_t) LED0_STK_SIZE,
(void *) NULL,
(UBaseType_t) LED0_TASK_PRIO,
(TaskHandle_t *) &LED0Task_Handler
);
xTaskCreate(
(TaskFunction_t) LED1_task,
(const char *) "LED1_task",
(uint16_t) LED1_STK_SIZE,
(void *) NULL,
(UBaseType_t) LED1_TASK_PRIO,
(TaskHandle_t *) &LED1Task_Handler
);
xTaskCreate(
(TaskFunction_t) LED2_task,
(const char *) "LED2_task",
(uint16_t) LED2_STK_SIZE,
(void *) NULL,
(UBaseType_t) LED2_TASK_PRIO,
(TaskHandle_t *) &LED2Task_Handler
);
xTaskCreate(
(TaskFunction_t) LED3_task,
上一篇:PT2272-M4--4键无线遥控器(STM32)
下一篇:STM32F103ZE FreeRTOS移植、测试
推荐阅读最新更新时间:2024-11-13 05:36
设计资源 培训 开发板 精华推荐
- MIC2341-2YTQ EV,用于 MIC2341 和 MIC2342 双插槽 PCI Express 热插拔电源控制器的评估板
- 数显电子秤信号处理模块
- SY88933V 3.3V/5V 1.25Gbps PECL低功耗限幅后置放大器TTL信号检测典型应用
- FUSB302BGEVB:Type-C 接口检测解决方案评估板
- 具有分立栅极驱动器和 MOSFET 的 LTC3861IUH-1 双相 1.2V/60A 转换器的典型应用电路,fSW = 300kHz
- 使用 Dialog Semiconductor 的 SLG46880V 的参考设计
- ADR3512 微功率、高精度 1.2V 基准电压源的典型应用电路,用于基本基准电压源连接
- 麦伦车底板-842414A
- 使用 Analog Devices 的 LT3420EDD 的参考设计
- LT3091IDE 正输出电流监视器的典型应用