目录
一、主要功能
二、硬件资源
三、程序编程
四、实现现象
一、主要功能
检测 分别是温湿度 光照 PM2.5、烟雾、红外,然后用OLED屏幕显示,
红外超过阈值则蜂鸣器报警,这是防盗报警;温度或烟雾超过阈值,则蜂鸣器也报警,
并且继电器驱动电机转动进行撒水,这是火灾预警;电压超过阈值,则所有模块包括显示屏全部熄灭,系统不起作用,这是过载保护。
二、硬件资源
基于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 LED1(a) (a?HAL\_GPIO\_WritePin(LED1\_GPIO\_Port, LED1\_Pin, GPIO\_PIN\_RESET):HAL\_GPIO\_WritePin(LED1\_GPIO\_Port, LED1\_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\[6\]; //adc数值的存储数组uint16\_t temp,humi; //温湿度uint16\_t pm2,gz,hw,yw,dy;
uint16_t pwyz=60,gzyz=60,hwyz=60,ywyz=60,dyyz=60,WDYZ=300,SDYZ = 700;
uint8\_t flag\_led,flag\_beep; //灯、报警标志位uint16\_t time\_num;static int flag = 0;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<5;adc_ch++){ //分别存放通道1、2、3、4、5的ADC值adc\_buf\[adc\_ch\]=dong\_get\_adc();}pm2=adc_buf\[0\]/4096.00*100;gz=adc_buf\[1\]/4096.00*100;hw=adc_buf\[2\]/4096.00*100;yw=adc_buf\[3\]/4096.00*100;dy=adc_buf\[4\]/4096.00*100;}/****
*******显示函数
*****/void Display_function(void){ //第一行Oled\_ShowString(0,0,"PM2.5:"); OLED\_ShowNum(48,0,pm2,2,16); Oled\_ShowCHinese(64,0,"光照:"); Oled\_ShowString(96,0,":"); OLED_ShowNum(104,0,gz,2,16); //第二行Oled\_ShowCHinese(0,2,"红外"); Oled\_ShowString(32,2,":"); OLED_ShowNum(40,2,hw,2,16); Oled\_ShowCHinese(64,2,"烟雾:"); Oled\_ShowString(96,2,":"); OLED_ShowNum(104,2,yw,2,16); //第三行Oled\_ShowCHinese(0,4,"电压"); Oled\_ShowString(32,4,":"); OLED_ShowNum(40,4,dy,2,16); //第四行OLED\_Show\_Temp(0,6,temp); Oled\_ShowCHinese(64,6,"湿度"); Oled\_ShowString(96,6,":"); OLED\_Show\_Humi(96,6,humi/10);}/****
*******处理函数
*****/void Manage_function(void){ if(hw > hwyz) //温度大于温度MAX{flag\_beep=1; Oled\_ShowCHinese(64,4,"有人");} else{ Oled_ShowCHinese(64,4,"无人");} if(yw > ywyz) //温度大于温度MAX{flag_beep=1;flag_led = 1;} if(temp > WDYZ) //温度大于温度MAX{flag_led=1;flag_beep=1;} if(dy>dyyz){flag = 1;} if(humi > SDYZ) //温度大于温度MAX{flag_led=1;flag_beep=1;} if(hw < hwyz && yw < ywyz && dy < dyyz && temp < WDYZ ){flag_led=0;flag_beep=0;} if(gz < gzyz){ LED1(1);} else{ LED1(0);} if(flag_beep==1) BEEP(1); elseBEEP(0); if(flag_led==1) LED(1); elseLED(0);
}int main(void){ HAL\_Init(); SystemClock\_Config(); MX\_GPIO\_Init(); MX\_ADC1\_Init(); OLED_Init(); //OLED初始化OLED_Clear(); //OLED清屏while (1){ if(flag == 0){ Monitor_function(); //监测函数Display_function(); //显示函数Manage_function(); //处理函数HAL_Delay(10);} else if(flag == 1){ OLED_Clear(); //OLED清屏}}
}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站演示视频:
B站演示视频
基于单片机的博物馆安全监控系统设计
全部资料(源程序、仿真文件、安装包、原理图、演示视频):