基于STM32U575RIT6智能除湿器项目
目录
智能除湿器项目说明
除湿器原理
知识点
功能概述
基本要求
思路
项目实现
上电自检
STH20温湿度采集
ADC转换,实时获取电压
控制函数,根据温湿度,阈值,进行操作
LCD实时显示数据
按键中断,选择模式
开启定时器,每2s向上位机发送数据信息
STM32CubeMX配置
main.c 总代码
源码
注意
智能除湿器项目说明
除湿器原理
知识点
GPIO、定时器、中断、ADC、LCD屏幕、SHT20、SPI、IIC、UART
功能概述
模块 | 功能 |
LCD屏幕 | 显示温湿度,风机开关情况,制冷⽚开关情况,加热片开关情况,温湿度上下阈值,设备ID,电池电压 |
LED\风扇 | 两个灯分别模拟制冷片(LED1),加热片(LED3)(灯亮表示开,灯灭表示关),风扇模拟风机 |
SHT20 | 温湿度传感器 |
内部 | 电池电压采集 |
串口 | 数据接收及下发数据控制 |
控温模式 | 当环境温度低于设置的阈值时,加热片启动。加热到温度阈值加2摄氏度停 |
当环境温度高于设置的阈值时,风机冷凝片启动。降温到温度阈值减2摄氏度停 | |
除湿模式 | 当环境湿度高于设置的阈值时,风机冷凝片加热片启动。湿度低于设置的阈值停 |
按键 | 按键控制当前的温湿度阈值和调节当前模式 底板USER按键、拓展板KEY1\KEY2\KEY3均可用 |
基本要求
除湿器上电设备自检(有电池电压检测,有电压正常无电压异常),⾃检通过后进入自动模式
LCD显示温湿度,风机开关情况,制冷⽚开关情况,加热片开关情况,温湿度上下阈值,设备ID,电池电压。
通过按键选择模式并调整温湿度的阈值大小。
与上位机通信(串口助手),每2s发送设备状态信息到上位机。上位机可发送命令设置除湿器(这里需要识别设备ID),上位机可发送指令获取设备状态信息。
思路
项目实现
上电自检
复位后,ADC转换一次进行上电自检,通过后显示自检通过动画,开机动画,并默认进入自动模式
ILI9341_Init(); //LCD初始化
//上拉自检//启动adc转换HAL_ADC_Start(&hadc4);HAL_ADC_PollForConversion(&hadc4,100); //等待转换完成,第二个参数表示超时时间,单位msval=HAL_ADC_GetValue(&hadc4); //获取ADC转换结果Vbat=(val*3.3*4)/4095;if(Vbat>3){ HAL_TIM_Base_Start_IT(&htim2); //开启定时USART1_RxCounter = 0; //中断标志位初始化SystemSelfTest(); //自检通过动画LCD_Picture2(); //开机动画LCD_Picture(gImage_2); //背景/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){}
//开机自检通过动画
void SystemSelfTest()
{ Gui_DrawFont_GBK16(10, 30, RED,BLACK, (uint8_t *)"Vbat OK");HAL_Delay(500);Gui_DrawFont_GBK16(10, 50, RED,BLACK, (uint8_t *)"Fan OK");HAL_Delay(500);Gui_DrawFont_GBK16(10, 70, RED,BLACK, (uint8_t *)"Condenser OK");HAL_Delay(500);Gui_DrawFont_GBK16(10, 90, RED,BLACK, (uint8_t *)"Heater OK");HAL_Delay(500);ILI9341_Clear(BLACK);
}
自检失败,显示失败信息并蓝屏
else{SystemSelfTest_Fail(); Gui_DrawFont_GBK16(40, 150, WHITE,BLUE, (uint8_t *)"The self-test failed"); }
//自检失败动画
void SystemSelfTest_Fail()
{ Gui_DrawFont_GBK16(10, 30, RED,BLACK, (uint8_t *)"Vbat Fail");Gui_DrawFont_GBK16(10, 50, RED,BLACK, (uint8_t *)"Fan Fail");Gui_DrawFont_GBK16(10, 70, RED,BLACK, (uint8_t *)"Condenser Fail");Gui_DrawFont_GBK16(10, 90, RED,BLACK, (uint8_t *)"Heater Fail");HAL_Delay(1000);ILI9341_Clear(BLUE);
}
STH20温湿度采集
驱动文件采集,主函数extern调用
/********************************************************************************* @file bsp_sht20.c* @brief 温湿度传感器SHT20驱动* *******************************************************************************/
#include "bsp_sht20.h"
#include "i2c.h"
#include "stdio.h"
//
volatile SHT20_TemRH_Val gTemRH_Val; //存储温湿度值
//
/*
**********************************************************************
* @fun :BSP_SHT20_Read
* @brief :根据指令读取sht20内部的寄存器
* @param :
* @return :sht20_reg_val
**********************************************************************
*/
uint16_t BSP_SHT20_Read(uint8_t sht20_cmd)
{uint16_t sht20_reg_val = 0;uint8_t sht20_reg_buff[2]={0x00,0x00};HAL_I2C_Master_Transmit(&hi2c1,SHT20_ADDR_WRITE,&sht20_cmd,1,100); //发送控制指令HAL_Delay(10);HAL_I2C_Master_Receive(&hi2c1,SHT20_ADDR_READ,sht20_reg_buff,2,100); //读取数据,两个字节HAL_Delay(10);sht20_reg_val=(sht20_reg_buff[0]<<8)|sht20_reg_buff[1]; //合并数据return (sht20_reg_val);
}
/*
**********************************************************************
* @fun :BSP_SHT20_GetData
* @brief :获取sht20的温湿度数据
* @param :
* @return :none
**********************************************************************
*/
void BSP_SHT20_GetData(void)
{uint16_t pTem=0,pHum=0;//读取数据pTem=BSP_SHT20_Read(SHT20_HOLD_M_READ_T);pHum=BSP_SHT20_Read(SHT20_HOLD_M_READ_RH);pHum &=0xFFFC;//数据转换gTemRH_Val.Tem = -46.85f+175.72f*((float)pTem/65536);gTemRH_Val.Hum = -6+125*((float)pHum/65536);//printf("temperature:%.2f, humidity:%.2f\n",gTemRH_Val.Tem,gTemRH_Val.Hum);
ADC转换,实时获取电压
接收中断,获取上位机(串口)指令
//接收中断处理
void uart_handle(void)
{if(USART1_RxCounter) {if(strncmp(USART1_RxBuff,"16",2)){if(strstr(USART1_RxBuff,"Hum_level=")){if(flag==1|flag==0)sscanf(USART1_RxBuff,"Hum_level= %f",&Hum_level);}else if(strstr(USART1_RxBuff,"Tem_maxlevel=")){if(flag==0|flag==2)sscanf(USART1_RxBuff,"Tem_maxlevel= %f",&Tem_maxlevel);}else if(strstr(USART1_RxBuff,"Tem_minlevel=")){if(flag==0|flag==2)sscanf(USART1_RxBuff,"Tem_minlevel= %f",&Tem_minlevel);}else if(strstr(USART1_RxBuff,"flag=")){sscanf(USART1_RxBuff,"flag= %d",&flag);}else if(strstr(USART1_RxBuff,"tem")){printf("tem:%.2f\n",gTemRH_Val.Tem);}else if(strstr(USART1_RxBuff,"hum")){printf("hum:%.2f\n",gTemRH_Val.Hum);}USART1_RxCounter=0;memset( USART1_RxBuff,0,1024);}elseprintf("未识别到该设备!\n");}}
控制函数,根据温湿度,阈值,进行操作
//控制
//PC6 风扇
//PC4 LED1 冷凝器
//PC13 LED3 加热器
void control(void)
{static uint8_t f = 0; //添加标志位,解决自动模式下控温bugif(flag==0){//先除湿if(gTemRH_Val.Hum>Hum_level){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON ");strcpy(heater,"heater : ON ");f = 0;}else{if(f == 0){f = 1;HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");strcpy(heater,"heater : OFF");}//后控温if(gTemRH_Val.Tem>Tem_maxlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON "); }else if(gTemRH_Val.Tem<Tem_maxlevel-2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");}if(gTemRH_Val.Tem<Tem_minlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);strcpy(heater,"heater : ON ");}else if(gTemRH_Val.Tem>Tem_minlevel+2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);strcpy(heater,"heater : OFF");}}}if(flag==1) //除湿模式{if(gTemRH_Val.Hum>Hum_level){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON ");strcpy(heater,"heater : ON ");}else{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");strcpy(heater,"heater : OFF");} }if(flag==2) //控温模式{if(gTemRH_Val.Tem>Tem_maxlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON "); }else if(gTemRH_Val.Tem<Tem_maxlevel-2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");}if(gTemRH_Val.Tem<Tem_minlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);strcpy(heater,"heater : ON ");}else if(gTemRH_Val.Tem>Tem_minlevel+2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);strcpy(heater,"heater : OFF");}}}
LCD实时显示数据
//LCD显示
void lcd_display(void)
{sprintf(temperature,"temperature : %.2f C",gTemRH_Val.Tem);sprintf(humidity,"humidity : %.2f %%",gTemRH_Val.Hum);sprintf(Hum_threshold,"Hum_threshold : %.2f %%",Hum_level);sprintf(Threshold_max,"Threshold_max : %.2f C",Tem_maxlevel);sprintf(Threshold_min,"Threshold_min : %.2f C",Tem_minlevel);sprintf(ID,"ID : %d",hi2c1.Init.OwnAddress1);sprintf(vbat,"vbat : %.2f V",Vbat);sprintf(Mode,"Mode : %02d ",flag);Gui_DrawFont_GBK16(46,30,RED,WHITE,(uint8_t *)"Smart dehumidifier");Gui_DrawFont_GBK16(20,65,BLACK,WHITE,temperature);Gui_DrawFont_GBK16(20,90,BLACK,WHITE,humidity);Gui_DrawFont_GBK16(20,125,BLACK,WHITE,Threshold_max);Gui_DrawFont_GBK16(20,145,BLACK,WHITE,Threshold_min);Gui_DrawFont_GBK16(20,165,BLACK,WHITE,Hum_threshold);Gui_DrawFont_GBK16(35,190,BLACK,WHITE,fun);Gui_DrawFont_GBK16(35,205,BLACK,WHITE,condenser);Gui_DrawFont_GBK16(35,220,BLACK,WHITE,heater);Gui_DrawFont_GBK16(120,240,BLACK,WHITE,ID);Gui_DrawFont_GBK16(120,260,BLACK,WHITE,vbat);Gui_DrawFont_GBK16(120,280,BLACK,WHITE,Mode);
}
按键中断,选择模式
flag:0自动模式,1除湿模式,2控温模式(mode:0调节温度下阈值,1调节温度上阈值)
//按键中断,选择模式
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{if(GPIO_Pin == GPIO_PIN_12){//按键抬手检测:while(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));flag++;if(flag>2)flag=0; }//PC5 KEY3if(GPIO_Pin == GPIO_PIN_5){while(!HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_5));if(flag==2){ mode++;if(mode>1)mode=0;}}//PC8 KEY2if(GPIO_Pin == GPIO_PIN_8){while(!HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_8));if(flag == 1)Hum_level++;if(flag==2){if(mode==0) //制冷Tem_minlevel++;else //制热Tem_maxlevel++;}} //PC9 KEY1if(GPIO_Pin == GPIO_PIN_9){while(!HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_9));if(flag == 1) Hum_level--;if(flag==2) {if(mode==0) //制冷Tem_minlevel--;else //制热Tem_maxlevel--;}}
}
开启定时器,每2s向上位机发送数据信息
//定时2s
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim==&htim2){printf("id:%d,flag:%d,mode:%d,temperature:%.2f, humidity:%.2f,Hum_level:%.2f,Tem_maxlevel:%.2f,Tem_minlevel:%.2f,Vbat:%.2f",hi2c1.Init.OwnAddress1,flag,mode,gTemRH_Val.Tem,gTemRH_Val.Hum,Hum_level,Tem_maxlevel,Tem_minlevel,Vbat);}
}
STM32CubeMX配置
main.c 总代码
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2024 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "i2c.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "bsp_sht20.h"
#include "bg.h"
#include <string.h>
#include "bsp_ili9341_4line.h"/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD *//* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM *//* USER CODE END PM *//* Private variables ---------------------------------------------------------*//* USER CODE BEGIN PV *///调用采集文件中的函数
extern volatile SHT20_TemRH_Val gTemRH_Val; //存储温湿度值int flag=0; //模式标志位
int mode=0; //制冷|热标志位float Hum_level=70.00; //湿度阈值,初值50float Tem_maxlevel=26.00; //温度上阈值
float Tem_minlevel=16.00; //温度下阈值volatile uint8_t USART1_RxBuff[1024]; //接收数组
volatile uint8_t USART1_RxCounter=0;uint32_t val;
float Vbat=0; //电压uint8_t fun[32]; //风机
uint8_t condenser[32]; //冷凝器
uint8_t heater[32]; //加热片
uint8_t temperature[32]; //温度
uint8_t humidity[32]; //湿度
uint8_t Hum_threshold[32]; //湿度阈值
uint8_t Threshold_max[32]; //温度上阈值
uint8_t Threshold_min[32]; //温度下阈值
uint8_t ID[32]; //设备ID
uint8_t vbat[32]; //电压
uint8_t Mode[32]; //模式 0自动,1除湿,2控温uint8_t lcd_flag = 0;/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
/* USER CODE BEGIN PFP *///LCD显示
void lcd_display(void);
//接收中断
void uart_handle(void);
//控制函数
void control(void);/* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 *///printf重定向
int fputc(int ch, FILE * p)
{while(!(USART1->ISR & 1<<7));//等待TDR为空,即TXE置1,跳出while,然后往TDR中放数据USART1->TDR=ch;return 0;
}//按键中断,选择模式
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{if(GPIO_Pin == GPIO_PIN_12){//按键抬手检测:while(!HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_12));flag++;if(flag>2)flag=0; }//PC5 KEY3if(GPIO_Pin == GPIO_PIN_5){while(!HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_5));if(flag==2){ mode++;if(mode>1)mode=0;}}//PC8 KEY2if(GPIO_Pin == GPIO_PIN_8){while(!HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_8));if(flag == 1)Hum_level++;if(flag==2){if(mode==0) //制冷Tem_minlevel++;else //制热Tem_maxlevel++;}} //PC9 KEY1if(GPIO_Pin == GPIO_PIN_9){while(!HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_9));if(flag == 1) Hum_level--;if(flag==2) {if(mode==0) //制冷Tem_minlevel--;else //制热Tem_maxlevel--;}}
}
//开机自检
void SystemSelfTest()
{ Gui_DrawFont_GBK16(10, 30, RED,BLACK, (uint8_t *)"Vbat OK");HAL_Delay(500);Gui_DrawFont_GBK16(10, 50, RED,BLACK, (uint8_t *)"Fan OK");HAL_Delay(500);Gui_DrawFont_GBK16(10, 70, RED,BLACK, (uint8_t *)"Condenser OK");HAL_Delay(500);Gui_DrawFont_GBK16(10, 90, RED,BLACK, (uint8_t *)"Heater OK");HAL_Delay(500);ILI9341_Clear(BLACK);
}void SystemSelfTest_Fail()
{ Gui_DrawFont_GBK16(10, 30, RED,BLACK, (uint8_t *)"Vbat Fail");Gui_DrawFont_GBK16(10, 50, RED,BLACK, (uint8_t *)"Fan Fail");Gui_DrawFont_GBK16(10, 70, RED,BLACK, (uint8_t *)"Condenser Fail");Gui_DrawFont_GBK16(10, 90, RED,BLACK, (uint8_t *)"Heater Fail");HAL_Delay(1000);ILI9341_Clear(BLUE);
}//定时2s
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim==&htim2){printf("id:%d,flag:%d,mode:%d,temperature:%.2f, humidity:%.2f,Hum_level:%.2f,Tem_maxlevel:%.2f,Tem_minlevel:%.2f,Vbat:%.2f",hi2c1.Init.OwnAddress1,flag,mode,gTemRH_Val.Tem,gTemRH_Val.Hum,Hum_level,Tem_maxlevel,Tem_minlevel,Vbat);}
}/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* Configure the System Power */SystemPower_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_I2C1_Init();MX_USART1_UART_Init();MX_ADC4_Init();MX_SPI1_Init();MX_TIM2_Init();/* USER CODE BEGIN 2 */HAL_PWREx_EnableVddA();HAL_PWREx_EnableVddIO2();HAL_ADCEx_Calibration_Start(&hadc4,ADC_CALIB_OFFSET,ADC_SINGLE_ENDED); //校准单端ADC采样HAL_UART_Receive_IT(&huart1, USART1_RxBuff,1024);__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//空闲中断 ILI9341_Init(); //LCD初始化
//上拉自检//启动adc转换HAL_ADC_Start(&hadc4);HAL_ADC_PollForConversion(&hadc4,100); //等待转换完成,第二个参数表示超时时间,单位msval=HAL_ADC_GetValue(&hadc4); //获取ADC转换结果Vbat=(val*3.3*4)/4095;if(Vbat>3){ HAL_TIM_Base_Start_IT(&htim2); //开启定时USART1_RxCounter = 0; //中断标志位初始化SystemSelfTest(); LCD_Picture2(); //开机动画LCD_Picture(gImage_2); //背景/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/* USER CODE END WHILE *//* USER CODE BEGIN 3 *///温湿度采集BSP_SHT20_GetData();//启动adc转换HAL_ADC_Start(&hadc4);HAL_ADC_PollForConversion(&hadc4,100); //等待转换完成,第二个参数表示超时时间,单位msval=HAL_ADC_GetValue(&hadc4); //获取ADC转换结果Vbat=(val*3.3*4)/4095; //接收中断uart_handle(); //控制control();//LCD显示lcd_display(); }}else{SystemSelfTest_Fail(); Gui_DrawFont_GBK16(40, 150, WHITE,BLUE, (uint8_t *)"The self-test failed"); }/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Configure the main internal regulator output voltage*/if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_MSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.MSIState = RCC_MSI_ON;RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_0;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV4;RCC_OscInitStruct.PLL.PLLM = 3;RCC_OscInitStruct.PLL.PLLN = 10;RCC_OscInitStruct.PLL.PLLP = 2;RCC_OscInitStruct.PLL.PLLQ = 2;RCC_OscInitStruct.PLL.PLLR = 1;RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;RCC_OscInitStruct.PLL.PLLFRACN = 0;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2|RCC_CLOCKTYPE_PCLK3;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK){Error_Handler();}
}/*** @brief Power Configuration* @retval None*/
static void SystemPower_Config(void)
{/** Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral*/HAL_PWREx_DisableUCPDDeadBattery();
/* USER CODE BEGIN PWR */
/* USER CODE END PWR */
}/* USER CODE BEGIN 4 *///LCD显示
void lcd_display(void)
{sprintf(temperature,"temperature : %.2f C",gTemRH_Val.Tem);sprintf(humidity,"humidity : %.2f %%",gTemRH_Val.Hum);sprintf(Hum_threshold,"Hum_threshold : %.2f %%",Hum_level);sprintf(Threshold_max,"Threshold_max : %.2f C",Tem_maxlevel);sprintf(Threshold_min,"Threshold_min : %.2f C",Tem_minlevel);sprintf(ID,"ID : %d",hi2c1.Init.OwnAddress1);sprintf(vbat,"vbat : %.2f V",Vbat);sprintf(Mode,"Mode : %02d ",flag);Gui_DrawFont_GBK16(46,30,RED,WHITE,(uint8_t *)"Smart dehumidifier");Gui_DrawFont_GBK16(20,65,BLACK,WHITE,temperature);Gui_DrawFont_GBK16(20,90,BLACK,WHITE,humidity);Gui_DrawFont_GBK16(20,125,BLACK,WHITE,Threshold_max);Gui_DrawFont_GBK16(20,145,BLACK,WHITE,Threshold_min);Gui_DrawFont_GBK16(20,165,BLACK,WHITE,Hum_threshold);Gui_DrawFont_GBK16(35,190,BLACK,WHITE,fun);Gui_DrawFont_GBK16(35,205,BLACK,WHITE,condenser);Gui_DrawFont_GBK16(35,220,BLACK,WHITE,heater);Gui_DrawFont_GBK16(120,240,BLACK,WHITE,ID);Gui_DrawFont_GBK16(120,260,BLACK,WHITE,vbat);Gui_DrawFont_GBK16(120,280,BLACK,WHITE,Mode);
}//接收中断处理
void uart_handle(void)
{if(USART1_RxCounter) {if(strncmp(USART1_RxBuff,"16",2)){if(strstr(USART1_RxBuff,"Hum_level=")){if(flag==1|flag==0)sscanf(USART1_RxBuff,"Hum_level= %f",&Hum_level);}else if(strstr(USART1_RxBuff,"Tem_maxlevel=")){if(flag==0|flag==2)sscanf(USART1_RxBuff,"Tem_maxlevel= %f",&Tem_maxlevel);}else if(strstr(USART1_RxBuff,"Tem_minlevel=")){if(flag==0|flag==2)sscanf(USART1_RxBuff,"Tem_minlevel= %f",&Tem_minlevel);}else if(strstr(USART1_RxBuff,"flag=")){sscanf(USART1_RxBuff,"flag= %d",&flag);}else if(strstr(USART1_RxBuff,"tem")){printf("tem:%.2f\n",gTemRH_Val.Tem);}else if(strstr(USART1_RxBuff,"hum")){printf("hum:%.2f\n",gTemRH_Val.Hum);}USART1_RxCounter=0;memset( USART1_RxBuff,0,1024);}elseprintf("未识别到该设备!\n");}}//控制
//PC6 风扇
//PC4 LED1 冷凝器
//PC13 LED3 加热器
void control(void)
{static uint8_t f = 0;if(flag==0){//先除湿if(gTemRH_Val.Hum>Hum_level){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON ");strcpy(heater,"heater : ON ");f = 0;}else{if(f == 0){f = 1;HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");strcpy(heater,"heater : OFF");}//后控温if(gTemRH_Val.Tem>Tem_maxlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON "); }else if(gTemRH_Val.Tem<Tem_maxlevel-2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");}if(gTemRH_Val.Tem<Tem_minlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);strcpy(heater,"heater : ON ");}else if(gTemRH_Val.Tem>Tem_minlevel+2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);strcpy(heater,"heater : OFF");}}}if(flag==1) //除湿模式{if(gTemRH_Val.Hum>Hum_level){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON ");strcpy(heater,"heater : ON ");}else{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_13,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");strcpy(heater,"heater : OFF");} }if(flag==2) //控温模式{if(gTemRH_Val.Tem>Tem_maxlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,1);strcpy(fun,"fun : ON ");strcpy(condenser,"condenser : ON "); }else if(gTemRH_Val.Tem<Tem_maxlevel-2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6 | GPIO_PIN_4 ,0);strcpy(fun,"fun : OFF");strcpy(condenser,"condenser : OFF");}if(gTemRH_Val.Tem<Tem_minlevel){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);strcpy(heater,"heater : ON ");}else if(gTemRH_Val.Tem>Tem_minlevel+2){HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);strcpy(heater,"heater : OFF");}}}/* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
源码
已上传
注意
后续更新连接WiFi模块及阿里云,实现远程控制的版本,视频讲解也将在阿里云版本上传