项目说明
除湿器原理
知识点
GPIO、定时器、中断、ADC、LCD屏幕、SHT20、SPI、IIC、UART
功能概述
模块 | 功能 |
LCD屏幕 | 显示温湿度,风机开关情况,制冷 开关情况,加热片开关情况,温 湿度上下阈值,设备ID,电池电压 |
LED\风扇 | 两个灯分别模拟制冷片(LED1),加热片(LED3)(灯亮表示开,灯灭表示 关),风扇模拟风机 |
SHT20 | 温湿度传感器 |
内部 | 电池电压采集 |
串口 | 数据接收及下发数据控制 |
控温模式 | 当环境温度低于设置的阈值时,加热片启动。加热到温度阈值加2摄 氏度停 当环境温度高于设置的阈值时,风机冷凝片启动。降温到温度阈值减2 摄氏度停 |
除湿模式 | 当环境湿度高于设置的阈值时,风机冷凝片加热片启动。湿度低于设 置的阈值停 |
按键 | 按键控制当前的温湿度阈值和调节当前模式 底板USER按键、拓展板KEY1\KEY2\KEY3均可用 |
基本要求
除湿器上电设备自检(有电池电压检测,有电压正常无电压异常), 检通过后进入自动模式 LCD显示温湿度,风机开关情况,制冷 开关情况,加热片开关情况,温湿度上下阈值,设备 ID,电池电压。 通过按键选择模式并调整温湿度的阈值大小。 与上位机通信(串口助手),每2s发送设备状态信息到上位机。上位机可发送命令设置除湿器 (这里需要识别设备ID),上位机可发送指令获取设备状态信息。
项目配置
项目代码
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 "icache.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "wifi.h"
#include "ali_yun.h"
#include "bsp_sht20.h"
#include "bsp_ili9341_4line.h"
#include <string.h>
#include "demo.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 */
float T,H;
uint8_t USART1_RxBuff[1024];
uint8_t USART1_RxCounter;
uint8_t show_value[64] = {0};
uint8_t show_fan[64] = {0};
uint8_t show_cold[64] = {0};
uint8_t show_hot[64] = {0};
uint8_t show_id[64] = {0};
uint8_t show_volt[64] = {0};
uint8_t show_Tem[64] = {0};
uint8_t show_Hum[64] = {0};
int MODE; //控制模式
int MODE1; //控制阀值
float TempLow = 20; //温度下限阀值
float TempHigh = 40; //温度上限阀值
float humidity = 45; //湿度阀值
char fan[4]="OFF"; //风扇开关状态
char zhilengji[4]={0}; //制冷片状态
char jiarepian[4]={0}; //加热片状态
char id[4] = "666"; //设备id
uint32_t val; //设备电池电压
float vbat;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch,FILE * p)
{
while(!(USART1->ISR & (1<<7)));
//判断发送数据寄存器是否为空
//第七位为1的时候为空,应该发送数据,此时跳出阻塞循环
USART1->TDR = ch;
return ch;
}
uint8_t USART5_RxBuff[1024]; //不定长接收缓冲区
uint8_t USART5_RxCounter; //不定长接受标志位
uint8_t message[1024];
enum Device_Identify
{
IDENTIFY_FAIL = 0,
IDENTIFY_SUCCESS = 1,
};
int state = IDENTIFY_FAIL; //PC端识别设备默认失败
void uart_control(void);
void lcd_display(void);
void uart_display(void);
void auto_control(void);
void SystemSelfTest(void);
void SystemSelfTestFail(void);
/* 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_ICACHE_Init();
MX_UART5_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
MX_ADC4_Init();
MX_SPI1_Init();
MX_TIM3_Init();
MX_TIM17_Init();
/* USER CODE BEGIN 2 */
ILI9341_Init(); //初始化屏幕
HAL_UART_Receive_IT(&huart1,USART1_RxBuff,1024); //开启接收中断
__HAL_UART_ENABLE_IT(&huart1,UART_FLAG_IDLE); //开启空闲中断
HAL_UART_Receive_IT(&huart5,USART5_RxBuff,1024); //串口接收中断
__HAL_UART_ENABLE_IT(&huart5,UART_IT_IDLE); //串口空闲中断
HAL_PWREx_EnableVddA(); //启用VDDA电压
HAL_PWREx_EnableVddIO2(); //启用VDDIO电压
HAL_ADCEx_Calibration_Start(&hadc4,ADC_CALIB_OFFSET,ADC_SINGLE_ENDED); //校准单端ADC采样
//启动ADC转换
HAL_ADC_Start(&hadc4);
HAL_ADC_PollForConversion(&hadc4,100);
val = HAL_ADC_GetValue(&hadc4);
vbat=(val*3.3*4)/4095;
if(vbat > 0)
{
USART1_RxCounter = 0; //中断标志位初始化
USART5_RxCounter=0;
SystemSelfTest(); //开机自检
ILI9341_Clear(NULL);
Gui_DrawFont_GBK16(40, 150, RED,BLACK, (uint8_t *)"Network connecting..."); //网络连接动画
WIFI_Connect();
Ali_Yun_Init();
HAL_TIM_Base_Start_IT(&htim17); //开启定时器
ILI9341_Clear(NULL);
LCD_Picture();
int time=0;
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
//温湿度采集
BSP_SHT20_GetData();
//启动ADC转换
HAL_ADC_Start(&hadc4);
HAL_ADC_PollForConversion(&hadc4,100);
val = HAL_ADC_GetValue(&hadc4);
vbat=(val*3.3*4)/4095;
lcd_display();
uart_control();
auto_control();
if(USART5_RxCounter==1)
{
Ali_Yun_GetRCV();
USART5_RxCounter=0;
memset(USART5_RxBuff,0,sizeof(USART5_RxBuff));
}
HAL_Delay(5);
time++;
BSP_SHT20_GetData();
if(time == 20)
{
sprintf(message,"AT+MQTTPUB=0,\"/sys/k255mmCnuLT/ESP001/thing/event/property/post\",\"{\\\"params\\\":{\\\"temperature\\\":{\\\"value\\\":%.2f}\\,\\\"Humidity\\\":{\\\"value\\\":%.2f}}\\,\\\"method\\\":\\\"thing.event.property.post\\\"}\",0,0",T,H);
ESP8266_USART("%s\r\n",message);
time = 0;
HAL_Delay(20);
USART5_RxCounter=0;
memset(USART5_RxBuff,0,sizeof(USART5_RxBuff));
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}
else
{
SystemSelfTestFail();
}
/* 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 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == &htim17)
{
uart_display();
}
}
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
if(MODE == 0) //除湿模式
{
if(MODE1 == 0) //向下模式
{
if(GPIO_Pin == GPIO_PIN_8) //按ke2调节湿度下阀值
{
humidity-=1;
}
}
else if(MODE1 == 1) //向上模式
{
if(GPIO_Pin == GPIO_PIN_8) //按ke2调节湿度上阀值
{
humidity+=1;
}
}
}
if(MODE == 1) //控温模式
{
if(MODE1 == 0) //向下模式
{
if(GPIO_Pin == GPIO_PIN_8) //按ke2调节温度下阀值
{
TempLow-=1;
}
else if(GPIO_Pin == GPIO_PIN_5) //按key3调节温度下阀值
{
TempHigh-=1;
}
}
else if(MODE1 == 1) //向上模式
{
if(GPIO_Pin == GPIO_PIN_8) //按ke2调节温度上阀值
{
TempLow+=1;
}
else if(GPIO_Pin == GPIO_PIN_5) //按key3调节温度上阀值
{
TempHigh+=1;
}
}
}
if(GPIO_Pin == GPIO_PIN_9) //key1调节模式
{
MODE = 1 >> MODE;
}
if(GPIO_Pin == GPIO_PIN_0) //k1调节向上向下模式
{
MODE1 = 1 >> MODE1; //1为上,0为下
}
}
/**
* @brief 开机自检成功显示内容
* @retval None
*/
void SystemSelfTest(void)
{
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 *)"Refrigeration OK");
HAL_Delay(500);
Gui_DrawFont_GBK16(10, 90, RED,BLACK, (uint8_t *)"Heater OK");
HAL_Delay(500);
ILI9341_Clear(BLACK);
}
/**
* @brief 开机自检失败显示内容
* @retval None
*/
void SystemSelfTestFail(void)
{
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 *)"Refrigeration Fail");
Gui_DrawFont_GBK16(10, 90, RED,BLACK, (uint8_t *)"Heater Fail");
HAL_Delay(1000);
ILI9341_Clear(BLUE);
}
/**
* @brief 每隔2s向上位机发送设备状态信息
* @retval None
*/
void uart_display(void)
{
printf("设备电压值:%.4f\n",vbat);
printf("当前温度:%.2f, 当前湿度:%.2f\n",T,H);
printf("风扇状态:%s\n",fan);
printf("加热片状态:%s\n",jiarepian);
printf("制冷片状态:%s\n",zhilengji);
printf("温度阀值 上限:%.2f 下限:%.2f\n",TempLow,TempHigh);
printf("湿度阀值:%.2f\n",humidity);
printf("设备ID:%s\n",id);
if(MODE == 0)
{
printf("当前模式:除湿模式\n");
}
if(MODE == 1)
{
printf("当前模式:控温模式\n");
}
if(MODE1 == 1)
{
printf("向上模式\n");
}
if(MODE1 == 0)
{
printf("向下模式\n");
}
}
/**
* @brief 显示LCD屏幕上的内容
* @retval None
*/
void lcd_display(void)
{
sprintf(show_volt,"Vbat = %.4f V\n\r",vbat);
sprintf(show_value,"Temp:%.2f Humidity:%.2f\n",T,H);
sprintf(show_fan,"FAN: %s\n",fan);
sprintf(show_hot,"Refrigeration Sheet: %s\n",zhilengji);
sprintf(show_cold,"Heating Sheet: %s\n",jiarepian);
sprintf(show_Tem,"TempLow:%.2f TempHigh:%.2f \n",TempLow,TempHigh);
sprintf(show_Hum,"Humidity:%.2f\n",humidity);
sprintf(show_id,"Device_Id: %s\n",id);
Gui_DrawFont_GBK16(10,70,BLACK,WHITE,show_volt); //显示电压
Gui_DrawFont_GBK16(10,90,BLACK,WHITE,show_value); //显示温湿度
Gui_DrawFont_GBK16(10,110,BLACK,WHITE,show_fan); //显示风机开关情况
Gui_DrawFont_GBK16(10,130,BLACK,WHITE,show_hot); //显示制冷机开关情况
Gui_DrawFont_GBK16(10,150,BLACK,WHITE,show_cold); //显示加热片开关情况
Gui_DrawFont_GBK16(10,170,BLACK,WHITE,show_Tem); //显示温度上下阀值
Gui_DrawFont_GBK16(10,190,BLACK,WHITE,show_Hum); //显示湿度阀值
Gui_DrawFont_GBK16(10,210,BLACK,WHITE,show_id); //设备ID
if(MODE == 1)
{
Gui_DrawFont_GBK16(10,230,BLACK,WHITE,(uint8_t *)"TempControl mode");
}
if(MODE == 0)
{
Gui_DrawFont_GBK16(10,230,BLACK,WHITE,(uint8_t *)"DeHumid mode");
}
if(MODE1 == 1)
{
Gui_DrawFont_GBK16(10,250,BLACK,WHITE,(uint8_t *)"Upword mode");
}
if(MODE1 == 0)
{
Gui_DrawFont_GBK16(10,250,BLACK,WHITE,(uint8_t *)"downword mode");
}
}
/**
* @brief 自动控制
* @retval None
*/
void auto_control(void)
{
if(MODE == 1) //控温
{
if(T < TempLow)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
strcpy(fan,"OFF");
strcpy(zhilengji,"OFF");
strcpy(jiarepian,"ON");
}
if(T >=TempLow+2 && T <= TempHigh-2)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);
strcpy(fan,"OFF");
strcpy(zhilengji,"OFF");
strcpy(jiarepian,"OFF");
}
if(T > TempHigh)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);
strcpy(jiarepian,"OFF");
strcpy(fan,"ON");
strcpy(zhilengji,"ON");
}
}
if(MODE == 0)
{
if(H < humidity)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,0);
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,0);
strcpy(fan,"OFF");
strcpy(zhilengji,"OFF");
strcpy(jiarepian,"OFF");
}
else if(H > humidity)
{
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_4,1);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,1);
strcpy(fan,"ON");
strcpy(zhilengji,"ON");
strcpy(jiarepian,"ON");
}
}
}
/**
* @brief 上位机可发送命令设置除湿器,上位机可发送指令获取设备状态信息
* @retval None
*/
void uart_control(void)
{
if(USART1_RxCounter == 1)
{
if(strstr(USART1_RxBuff,"device_id=666"))
{
printf("设备识别成功\n");
state = IDENTIFY_SUCCESS;
}
else
{
}
switch(state)
{
case IDENTIFY_SUCCESS:
if(strstr(USART1_RxBuff,"TC"))
{
printf("已进入控温模式\n");
MODE = 1;
}
else if(strstr(USART1_RxBuff,"DH"))
{
printf("已进入除湿模式\n");
MODE = 0;
}
else if(strstr(USART1_RxBuff,"TempLow="))
{
if(MODE == 1)
sscanf(USART1_RxBuff,"TempLow=%f",&TempLow);
}
else if(strstr(USART1_RxBuff,"TempHigh="))
{
if(MODE == 1)
sscanf(USART1_RxBuff,"TempHigh=%f",&TempHigh);
}
else if(strstr(USART1_RxBuff,"humidity="))
{
if(MODE == 0)
sscanf(USART1_RxBuff,"humidity=%f",&humidity);
}
else if(strstr(USART1_RxBuff,"mode"))
{
if(MODE == 0)
{
printf("当前模式:除湿模式\n");
}
if(MODE == 1)
{
printf("当前模式:控温模式\n");
}
if(MODE1 == 1)
{
printf("向上模式\n");
}
if(MODE1 == 0)
{
printf("向下模式\n");
}
}
else if(strstr(USART1_RxBuff,"vbat"))
{
printf("设备电压值:%.4f\n",vbat);
}
else if(strstr(USART1_RxBuff,"温湿度值"))
{
printf("当前温度:%.2f, 当前湿度:%.2f\n",T,H);
}
else if(strstr(USART1_RxBuff,"fan"))
{
printf("风扇状态:%s\n",fan);
}
else if(strstr(USART1_RxBuff,"加热片"))
{
printf("加热片状态:%s\n",jiarepian);
}
else if(strstr(USART1_RxBuff,"制冷片"))
{
printf("制冷片状态:%s\n",zhilengji);
}
else if(strstr(USART1_RxBuff,"温度阀值"))
{
printf("温度阀值 上限:%.2f 下限:%.2f\n",TempLow,TempHigh);
}
else if(strstr(USART1_RxBuff,"湿度阀值"))
{
printf("湿度阀值:%.2f\n",humidity);
}
else if(strstr(USART1_RxBuff,"设备ID"))
{
printf("设备ID:%s\n",id);
}
default:
break;
}
}
USART1_RxCounter=0;
memset( USART1_RxBuff,0,1024);
}
/* 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模块
阿里云模块
温湿度模块
中断处理程序
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
//判断是否触发串口空闲中断
if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE) == SET)
{
//int datalen = 0;
//用于存储接收数据长度,这里的没有用,如果有需要可以弄全局变量
__HAL_UART_CLEAR_IDLEFLAG(&huart1); //清除空闲中断标志
//datalen = 1024 - huart1.RxXferCount; //接收到多少数据 = 总的缓冲区大小 - 剩余缓冲区的大小
huart1.RxXferCount = 1024; //初始化接收的最大数据量
huart1.pRxBuffPtr = USART1_RxBuff; //重新定义缓冲区首地址
USART1_RxCounter=1;//标识标识数据接收完毕
HAL_UART_Receive_IT(&huart1, USART1_RxBuff,1024);
}
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/**
* @brief This function handles UART5 global interrupt.
*/
void UART5_IRQHandler(void)
{
/* USER CODE BEGIN UART5_IRQn 0 */
/* USER CODE END UART5_IRQn 0 */
HAL_UART_IRQHandler(&huart5);
/* USER CODE BEGIN UART5_IRQn 1 */
//判断是否触发串口空闲中断
if(__HAL_UART_GET_FLAG(&huart5, UART_FLAG_IDLE) == SET)
{
//int datalen = 0;
//用于存储接收数据长度,这里的没有用,如果有需要可以弄全局变量
__HAL_UART_CLEAR_IDLEFLAG(&huart5); //清除空闲中断标志
//datalen = 1024 - huart5.RxXferCount; //接收到多少数据
//HAL_UART_Transmit(&huart1,(uint8_t*)USART5_RxBuff,datalen,10);
//验证是否收到,正式功能里注释掉
//memset(USART5_RxBuff,0,1024);
//正式功能可以哪里使用数据,使用完清除
huart5.RxXferCount = 1024; //初始化接收的最大数据量
huart5.pRxBuffPtr = USART5_RxBuff; //定义缓冲区
HAL_UART_Receive_IT(&huart5, USART5_RxBuff, 1024);
USART5_RxCounter=1;//标识标识数据接收完毕
}
/* USER CODE END UART5_IRQn 1 */
}
项目视频
项目文件
基于stm32的智能除湿器(阿里云版)资源-CSDN文库
基于stm32的智能除湿器(无阿里云版)资源-CSDN文库