21ic问答首页 - #求助。有没有大佬帮我看看代码要怎么改进,
#求助。有没有大佬帮我看看代码要怎么改进,
oo32024-12-24
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "TIM.h"
#include "RS485.h"
#include "HCSR04.h"
#include "SG90.h"
#include "hongwai.h"
#include "DHT11.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include <string.h>
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永无BUG 永不修改 //
////////////////////////////////////////////////////////////////////
extern unsigned int rec_data[4];
// 定义一个二进制信号量
SemaphoreHandle_t xDHT11Semaphore;
// Queue for UART data
QueueHandle_t uartQueue;
//PA1,2,3. PB0,5,6,8,9
//任务函数
void DHT11_Read_Task(void *pvParameters);
void DHT11_Process_Task(void *pvParameters);
//任务优先级
#define START_TASK_PRIO 1
//任务堆栈大小
#define START_STK_SIZE 128
//任务句柄
TaskHandle_t StartTask_Handler;
//任务函数
void start_task(void *pvParameters);
//红外感应打开垃圾桶
#define HongWAI_PRIO 4
#define HongWai_SIZE 512 //之前的128貌似太小了,改大之后终于舵机动了
TaskHandle_t HongWaiTask_Handler;
void hongwai_task(void *pvParameters);
//显示标语任务优先级
#define OLED_TASK_PRIO 1
//任务堆栈大小
#define OLED_STK_SIZE 128
//任务句柄
TaskHandle_t OLEDTask_Handler;
//任务函数
void oled_task(void *pvParameters);
////检测距离任务优先级
//#define CEJU_HCSR04_PRIO 1
////任务堆栈大小
//#define CEJU_HCSR04_SIZE 128
////任务句柄
//TaskHandle_t CEJUTask_Handler;
////任务函数
//void ceju_task(void *pvParameters);
//传输食物发酵温度提示
#define FAJIAO_PRIO 4
#define FAJIAO_SIZE 128
TaskHandle_t FAJIAOTask_Handler;
void fajiao_task(void *pvParameters);
int main(void)
{
OLED_Init();
Timer_Init();
Serial_Init();
Servo_Init();
HongWai_Init();
DHT11_Start();
// 创建一个二进制信号量
xDHT11Semaphore = xSemaphoreCreateBinary();
uartQueue = xQueueCreate(10, sizeof(char)); // Queue size of 10 characters
if (xDHT11Semaphore == NULL) {
// 信号量创建失败的处理
}
OLED_ShowChinese(1,1,7);
OLED_ShowChinese(1,2,8);
OLED_ShowChinese(1,3,0);
OLED_ShowChinese(1,4,1);
OLED_ShowChinese(1,5,2);
OLED_ShowChinese(1,6,3);
OLED_ShowString(2,1,"2340711137");
OLED_ShowChinese(3,1,4);
OLED_ShowChinese(3,2,5);
OLED_ShowChinese(3,3,6);
// Servo_SetAngle(0);
xTaskCreate(start_task,"start_task",START_STK_SIZE,NULL,START_TASK_PRIO,&StartTask_Handler);
// 启动FreeRTOS调度器
vTaskStartScheduler();
// 如果执行到这里,说明内存分配失败,调度器无法启动
while (1){}
}
void start_task(void *pvParameters)
{
Serial_Printf("start_task create successful\r\n");
taskENTER_CRITICAL();//进入临界区
if (xTaskCreate(hongwai_task, "hongwai_task", HongWai_SIZE, NULL, HongWAI_PRIO, &HongWaiTask_Handler) != pdPASS) {
Serial_Printf("Failed to create HongWai task\r\n");
}
if (xTaskCreate(oled_task, "oled_task", OLED_STK_SIZE, NULL, OLED_TASK_PRIO, &OLEDTask_Handler) != pdPASS) {
Serial_Printf("Failed to create OLED task\r\n");
}
if (xTaskCreate(DHT11_Read_Task, "DHT11_Read_Task", 128, NULL, 2, NULL) != pdPASS) {
Serial_Printf("Failed to create DHT11 Read task\r\n");
}
if (xTaskCreate(DHT11_Process_Task, "DHT11_Process_Task", 128, NULL, 2, NULL) != pdPASS) {
Serial_Printf("Failed to create DHT11 Process task\r\n");
}
//如果温度异常,传输食物发酵提示
//xTaskCreate(fajiao_task,"fajiao_task",FAJIAO_SIZE,NULL,FAJIAO_PRIO,&FAJIAOTask_Handler);
vTaskDelete(NULL); //删除启动任务自己
taskEXIT_CRITICAL();//退出临界区
}
void hongwai_task(void *pvParameters)
{
UBaseType_t uxHighWaterMark;
Serial_Printf("Initial stack high water mark for HongWai task: %lu\r\n", uxHighWaterMark);
Serial_Printf("HongWai task initialized\r\n");
uint8_t lastState = read_sensor();
for (;;) {
uint8_t currentState = read_sensor();
if (currentState != lastState) {
lastState = currentState;
if (!currentState) {
Servo_SetAngle(75);
vTaskDelay(pdMS_TO_TICKS(2000));
} else {
Servo_SetAngle(0);
}
}
vTaskDelay(pdMS_TO_TICKS(100));
// 检查并打印当前任务的堆栈使用情况
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial_Printf("Current stack high water mark for HongWai task: %lu\r\n", uxHighWaterMark);
}
}
void oled_task(void *pvParameters) {
UBaseType_t uxHighWaterMark;
Serial_Printf("OLED task initialized\r\n");
uint8_t RxData;
// 初始堆栈使用情况
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial_Printf("Initial stack high water mark for OLED task: %lu\r\n", uxHighWaterMark);
for (;;) {
if (Serial_GetRxFlag()) {
RxData = Serial_GetRxData();
OLED_ShowHexNum(4, 1, RxData, 8);
}
vTaskDelay(pdMS_TO_TICKS(500));
// 检查并打印当前任务的堆栈使用情况
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial_Printf("Current stack high water mark for OLED task: %lu\r\n", uxHighWaterMark);
}
}
//void fajiao_task(void *pvParameters)
//{
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// Serial_Printf("fajiao_task create successful\r\n");
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// for(;;){
// DHT11_REC_Data(); //接收温度和湿度的数据
// if(rec_data[2]>20 && rec_data[0]>10){
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// Serial_SendString("warning\r\n");
// vTaskDelay(pdMS_TO_TICKS(1000)); // 延时100ms
// }
// else
// {
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// Serial_SendString("good\r\n");
// vTaskDelay(pdMS_TO_TICKS(1000));
// }
// vTaskDelay(pdMS_TO_TICKS(1000)); // 每2秒检测一次
// }
//}
void DHT11_Read_Task(void *pvParameters) {
for (;;) {
DHT11_REC_Data();
Serial_Printf("DHT11 Read Task Started\r\n");
xSemaphoreGive(xDHT11Semaphore);
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void DHT11_Process_Task(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(xDHT11Semaphore, pdMS_TO_TICKS(1000)) == pdTRUE) {
if (rec_data[2] > 20 && rec_data[0] > 10) {
Serial_SendString("warning\r\n");
} else {
Serial_SendString("good\r\n");
}
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
串口输出的是:start_task create successful Initial stack high water mark for HongWai task: 2779096485 HongWai task initialized DHT11 Read Task Started warning OLED task initialized Initial stack high water mark for OLED task: 56
#include "Delay.h"
#include "OLED.h"
#include "TIM.h"
#include "RS485.h"
#include "HCSR04.h"
#include "SG90.h"
#include "hongwai.h"
#include "DHT11.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "queue.h"
#include <string.h>
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永无BUG 永不修改 //
////////////////////////////////////////////////////////////////////
extern unsigned int rec_data[4];
// 定义一个二进制信号量
SemaphoreHandle_t xDHT11Semaphore;
// Queue for UART data
QueueHandle_t uartQueue;
//PA1,2,3. PB0,5,6,8,9
//任务函数
void DHT11_Read_Task(void *pvParameters);
void DHT11_Process_Task(void *pvParameters);
//任务优先级
#define START_TASK_PRIO 1
//任务堆栈大小
#define START_STK_SIZE 128
//任务句柄
TaskHandle_t StartTask_Handler;
//任务函数
void start_task(void *pvParameters);
//红外感应打开垃圾桶
#define HongWAI_PRIO 4
#define HongWai_SIZE 512 //之前的128貌似太小了,改大之后终于舵机动了
TaskHandle_t HongWaiTask_Handler;
void hongwai_task(void *pvParameters);
//显示标语任务优先级
#define OLED_TASK_PRIO 1
//任务堆栈大小
#define OLED_STK_SIZE 128
//任务句柄
TaskHandle_t OLEDTask_Handler;
//任务函数
void oled_task(void *pvParameters);
////检测距离任务优先级
//#define CEJU_HCSR04_PRIO 1
////任务堆栈大小
//#define CEJU_HCSR04_SIZE 128
////任务句柄
//TaskHandle_t CEJUTask_Handler;
////任务函数
//void ceju_task(void *pvParameters);
//传输食物发酵温度提示
#define FAJIAO_PRIO 4
#define FAJIAO_SIZE 128
TaskHandle_t FAJIAOTask_Handler;
void fajiao_task(void *pvParameters);
int main(void)
{
OLED_Init();
Timer_Init();
Serial_Init();
Servo_Init();
HongWai_Init();
DHT11_Start();
// 创建一个二进制信号量
xDHT11Semaphore = xSemaphoreCreateBinary();
uartQueue = xQueueCreate(10, sizeof(char)); // Queue size of 10 characters
if (xDHT11Semaphore == NULL) {
// 信号量创建失败的处理
}
OLED_ShowChinese(1,1,7);
OLED_ShowChinese(1,2,8);
OLED_ShowChinese(1,3,0);
OLED_ShowChinese(1,4,1);
OLED_ShowChinese(1,5,2);
OLED_ShowChinese(1,6,3);
OLED_ShowString(2,1,"2340711137");
OLED_ShowChinese(3,1,4);
OLED_ShowChinese(3,2,5);
OLED_ShowChinese(3,3,6);
// Servo_SetAngle(0);
xTaskCreate(start_task,"start_task",START_STK_SIZE,NULL,START_TASK_PRIO,&StartTask_Handler);
// 启动FreeRTOS调度器
vTaskStartScheduler();
// 如果执行到这里,说明内存分配失败,调度器无法启动
while (1){}
}
void start_task(void *pvParameters)
{
Serial_Printf("start_task create successful\r\n");
taskENTER_CRITICAL();//进入临界区
if (xTaskCreate(hongwai_task, "hongwai_task", HongWai_SIZE, NULL, HongWAI_PRIO, &HongWaiTask_Handler) != pdPASS) {
Serial_Printf("Failed to create HongWai task\r\n");
}
if (xTaskCreate(oled_task, "oled_task", OLED_STK_SIZE, NULL, OLED_TASK_PRIO, &OLEDTask_Handler) != pdPASS) {
Serial_Printf("Failed to create OLED task\r\n");
}
if (xTaskCreate(DHT11_Read_Task, "DHT11_Read_Task", 128, NULL, 2, NULL) != pdPASS) {
Serial_Printf("Failed to create DHT11 Read task\r\n");
}
if (xTaskCreate(DHT11_Process_Task, "DHT11_Process_Task", 128, NULL, 2, NULL) != pdPASS) {
Serial_Printf("Failed to create DHT11 Process task\r\n");
}
//如果温度异常,传输食物发酵提示
//xTaskCreate(fajiao_task,"fajiao_task",FAJIAO_SIZE,NULL,FAJIAO_PRIO,&FAJIAOTask_Handler);
vTaskDelete(NULL); //删除启动任务自己
taskEXIT_CRITICAL();//退出临界区
}
void hongwai_task(void *pvParameters)
{
UBaseType_t uxHighWaterMark;
Serial_Printf("Initial stack high water mark for HongWai task: %lu\r\n", uxHighWaterMark);
Serial_Printf("HongWai task initialized\r\n");
uint8_t lastState = read_sensor();
for (;;) {
uint8_t currentState = read_sensor();
if (currentState != lastState) {
lastState = currentState;
if (!currentState) {
Servo_SetAngle(75);
vTaskDelay(pdMS_TO_TICKS(2000));
} else {
Servo_SetAngle(0);
}
}
vTaskDelay(pdMS_TO_TICKS(100));
// 检查并打印当前任务的堆栈使用情况
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial_Printf("Current stack high water mark for HongWai task: %lu\r\n", uxHighWaterMark);
}
}
void oled_task(void *pvParameters) {
UBaseType_t uxHighWaterMark;
Serial_Printf("OLED task initialized\r\n");
uint8_t RxData;
// 初始堆栈使用情况
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial_Printf("Initial stack high water mark for OLED task: %lu\r\n", uxHighWaterMark);
for (;;) {
if (Serial_GetRxFlag()) {
RxData = Serial_GetRxData();
OLED_ShowHexNum(4, 1, RxData, 8);
}
vTaskDelay(pdMS_TO_TICKS(500));
// 检查并打印当前任务的堆栈使用情况
uxHighWaterMark = uxTaskGetStackHighWaterMark(NULL);
Serial_Printf("Current stack high water mark for OLED task: %lu\r\n", uxHighWaterMark);
}
}
//void fajiao_task(void *pvParameters)
//{
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// Serial_Printf("fajiao_task create successful\r\n");
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// for(;;){
// DHT11_REC_Data(); //接收温度和湿度的数据
// if(rec_data[2]>20 && rec_data[0]>10){
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// Serial_SendString("warning\r\n");
// vTaskDelay(pdMS_TO_TICKS(1000)); // 延时100ms
// }
// else
// {
// vTaskDelay(pdMS_TO_TICKS(100)); // 延时100ms
// Serial_SendString("good\r\n");
// vTaskDelay(pdMS_TO_TICKS(1000));
// }
// vTaskDelay(pdMS_TO_TICKS(1000)); // 每2秒检测一次
// }
//}
void DHT11_Read_Task(void *pvParameters) {
for (;;) {
DHT11_REC_Data();
Serial_Printf("DHT11 Read Task Started\r\n");
xSemaphoreGive(xDHT11Semaphore);
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
void DHT11_Process_Task(void *pvParameters) {
for (;;) {
if (xSemaphoreTake(xDHT11Semaphore, pdMS_TO_TICKS(1000)) == pdTRUE) {
if (rec_data[2] > 20 && rec_data[0] > 10) {
Serial_SendString("warning\r\n");
} else {
Serial_SendString("good\r\n");
}
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
串口输出的是:start_task create successful Initial stack high water mark for HongWai task: 2779096485 HongWai task initialized DHT11 Read Task Started warning OLED task initialized Initial stack high water mark for OLED task: 56
您需要登录后才可以回复 登录 | 注册