目录
一、主要功能
二、硬件资源
三、程序编程
四、实现现象
一、主要功能
基于STM32F103C8T6 采用DHT11读取温度、滑动变阻器模拟读取电流、电压。
通过OLED屏幕显示,设置电流阈值为80,电流小阈值为50,电压阈值为60,温度阈值为30
随便哪个超过预祝,则继电器切断,LED灯灭掉,若电流小于50,则屏幕清屏,表示待机。
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
编辑
三、程序编程
#include "main.h"#include "adc.h"#include "gpio.h"#include "./HAL/key/key.h"#include "./HAL/OLED/OLED\_NEW.H"#include "./HAL/dht11/dht11.h"void Monitor\_function(void); //监测函数void Display\_function(void); //显示函数void Manage\_function(void); //处理函数#define LED(a) (a?HAL\_GPIO\_WritePin(LED\_GPIO\_Port, LED\_Pin, GPIO\_PIN\_RESET):HAL\_GPIO\_WritePin(LED\_GPIO\_Port, LED\_Pin, GPIO\_PIN\_SET)) #define BEEP(a) (a?HAL\_GPIO\_WritePin(BEEP\_GPIO\_Port, BEEP\_Pin, GPIO\_PIN\_RESET):HAL\_GPIO\_WritePin(BEEP\_GPIO\_Port, BEEP\_Pin, GPIO\_PIN\_SET)) uint8\_t adc\_ch; //adc的个数uint32\_t adc\_buf\[4\]; //adc数值的存储数组uint16\_t temp,humi; //温湿度uint16\_t dl,dy,wdnum; //电流 电压 温度uint16\_t dlmin=50,dlmax=80,dymax=60,wdmax=300; //电流最小50 最大80 电压最大60 温度最大30uint8\_t flag\_led,flag\_beep; //灯、报警标志位uint16\_t time\_num;static int mode=0;
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/void SystemClock\_Config(void);uint16\_t dong\_get\_adc(){ //开启ADC1HAL\_ADC\_Start(&hadc1); //等待ADC转换完成,超时为100msHAL\_ADC\_PollForConversion(&hadc1,100); //判断ADC是否转换成功if(HAL\_IS\_BIT\_SET(HAL\_ADC\_GetState(&hadc1),HAL\_ADC\_STATE\_REG_EOC)){ //读取值return HAL\_ADC\_GetValue(&hadc1);} return 0;
}/****
*******监测函数
*****/void Monitor\_function(void){ DHT11\_Read\_TempAndHumidity(&DHT11\_Data);//调用获取温湿度、电流、电压temp = DHT11_Data.temperature; //获取温度humi = DHT11_Data.humidity; //获取湿度//将获取的值存储到adc_buf中for(adc\_ch=0;adc\_ch<4;adc_ch++){ //分别存放通道1、2、3、4的ADC值adc\_buf\[adc\_ch\]=dong\_get\_adc();}dl=adc_buf\[0\]/4096.00*100; //电流dy=adc_buf\[3\]/4096.00*100; //电压}/****
*******显示函数
*****/void Display_function(void){ //第一行Oled\_ShowCHinese(0,0,"电流"); Oled\_ShowString(32,0,":"); OLED\_ShowNum(40,0,dl,2,16); Oled\_ShowCHinese(64,0,"电压"); Oled\_ShowString(96,0,":"); OLED\_ShowNum(104,0,dy,2,16); //第二行Oled_ShowCHinese(0,2,"温度"); Oled\_ShowString(32,2,":"); OLED\_Show\_Temp(40,2,temp); //第三行// Oled\_ShowCHinese(0,4,"湿度");// Oled\_ShowString(32,4,":");// OLED\_Show_Humi(40,4,humi/10);}/****
*******处理函数
*****/void Manage_function(void){ if(dl > dlmax) //电流超过电流MAX {flag_led=0;flag_beep=1;} if(dy> dymax ) //电压大于电压MAX{flag_led=0;flag_beep=1;} if(temp>wdmax) //温度大于温度MAX{flag_led=0;flag_beep=1;} if(dl>dlmin && dl < dlmax && dy < dymax && temp < wdmax){flag_led=1;flag_beep=0;} if(dl<dlmin){mode = 1;} if(flag_beep==1) BEEP(1); elseBEEP(0); if(flag_led==1) LED(1); elseLED(0);
}/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/int main(void){ HAL\_Init(); SystemClock\_Config(); MX\_GPIO\_Init(); //GPIO口设置MX\_ADC1\_Init(); //ADC转换OLED_Init(); //OLED初始化OLED_Clear(); //OLED清屏flag_led = 0; while (1){ if(mode == 0){ Monitor_function(); //监测函数Display_function(); //显示函数Manage_function(); //处理函数} else{ OLED_Clear(); //OLED清屏} HAL_Delay(10);time\_num++; if(time\_num >= 5000)time_num = 0;}
}/*** @brief System Clock Configuration* @retval None*/void SystemClock_Config(void){RCC\_OscInitTypeDef RCC\_OscInitStruct = {0};RCC\_ClkInitTypeDef RCC\_ClkInitStruct = {0};RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC\_OscInitStruct.OscillatorType = RCC\_OSCILLATORTYPE_HSI;RCC\_OscInitStruct.HSIState = RCC\_HSI_ON;RCC\_OscInitStruct.HSICalibrationValue = RCC\_HSICALIBRATION_DEFAULT;RCC\_OscInitStruct.PLL.PLLState = RCC\_PLL_ON;RCC\_OscInitStruct.PLL.PLLSource = RCC\_PLLSOURCE\_HSI\_DIV2;RCC\_OscInitStruct.PLL.PLLMUL = RCC\_PLL\_MUL4; 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\_ClkInitStruct.SYSCLKSource = RCC\_SYSCLKSOURCE_PLLCLK;RCC\_ClkInitStruct.AHBCLKDivider = RCC\_SYSCLK_DIV1;RCC\_ClkInitStruct.APB1CLKDivider = RCC\_HCLK_DIV2;RCC\_ClkInitStruct.APB2CLKDivider = RCC\_HCLK\_DIV1; if (HAL\_RCC\_ClockConfig(&RCC\_ClkInitStruct, FLASH\_LATENCY\_0) != HAL_OK){ Error_Handler();}PeriphClkInit.PeriphClockSelection = RCC\_PERIPHCLK\_ADC;PeriphClkInit.AdcClockSelection = RCC\_ADCPCLK2\_DIV2; if (HAL\_RCCEx\_PeriphCLKConfig(&PeriphClkInit) != HAL_OK){ Error_Handler();}
}/* USER CODE BEGIN 4 *//* 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 */
四、实现现象
具体动态效果看B站演示视频:
基于STM32的温度、电流、电压检测系统(OLED、DHT11、继电器、电机)_哔哩哔哩_bilibili
全部资料(源程序、仿真文件、安装包、演示视频):
通过百度网盘分享的文件:基于STM32的温度、电流、电压检测系统(1).zip
链接:https://pan.baidu.com/s/1h93-TTKkkdf2hBryU9v55Q?pwd=p4sq
提取码:p4sq
–来自百度网盘超级会员V4的分享