系统包括温湿度监测模块、烟雾监测模块、PM2.5监测模块、甲醛监测模块及显示报警模块,主要实现了对温湿度、PM2.5、甲醛和烟雾的实时监测。系统采用LCD液晶屏将各个传感器监测到的数据分别进行实时显示。当传感器监测值超出设定值范围时进行声光报警,并通过控制继电器来模拟加湿器、风扇、灭火装置的开关,对室内环境进行调节。注释有点乱码查看工程文件即可
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "dht11.h"
- #include "adc.h"
- #include "beep.h"
- #include "jdq.h"
- #include "math.h"
- int main(void)
- {
- u8 t=0;
- int b;
- u8 temperature;
- u8 humidity;
- u16 adcx1;
- u16 adcx;
- u16 pm;
- u16 ms;
- float msk;
- float temp;
- double x,y;
- delay_init(); //延时函数初始化
- uart_init(115200); //串口初始化为115200
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- Adc_Init1();
- Adc_Init2();
- Adc_Init3();
- BEEP_Init();
- JDQ1_Init();
- JDQ2_Init();
- JDQ3_Init();
- JDQ4_Init();
- POINT_COLOR=RED; //设置字体为红色
- LCD_ShowString(30,50,200,16,16,"DHT11");
- LCD_ShowString(30,130,200,16,16,"MQ2");
- LCD_ShowString(30,220,200,16,16,"JQ: ");
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(30,150,200,16,16,"ADC_CH0_VAL:");
- LCD_ShowString(30,170,200,16,16,"ADC_CH0_VOL:0.000V");
-
- while(DHT11_Init()) //DHT11初始化
- {
- LCD_ShowString(30,70,200,16,16,"DHT11 Error");
- delay_ms(200);
- LCD_Fill(30,70,239,130+16,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(30,70,200,16,16,"DHT11 OK");
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(30,90,200,16,16,"Temp: C");
- LCD_ShowString(30,110,200,16,16,"Humi: %");
-
- while(1)
- {
- if(t%10==0) //每100ms读取一次
- {
- DHT11_Read_Data(&temperature,&humidity); //读取温湿度值
- temperature=temperature*0.9;
- humidity=humidity*0.9;
- LCD_ShowNum(30+40,90,temperature,2,16); //显示温度
- LCD_ShowNum(30+40,110,humidity,2,16); //显示湿度
- }
- delay_ms(10);
- t++;
- if(t==20)
- {
- t=0;
-
- }
- adcx1=Get_Adc1_Average(ADC_Channel_10,10);
- adcx1=adcx1*0.75;
- LCD_ShowxNum(126,150,adcx1,4,16,0);//显示ADC的值
-
- temp=(float)adcx1*(3.3/4096);
-
- adcx=temp;
- LCD_ShowxNum(126,170,adcx,1,16,0);//显示电压值
- temp-=adcx;
- temp*=1000;
- LCD_ShowxNum(142,170,temp,3,16,0X80); //减去整数部分,放大一千倍。显示小数部分
-
- delay_ms(250);
-
- pm=Get_GP2Y_Average(20);
- LCD_ShowString(30,200,200,16,16,"PM2.5:");
- LCD_ShowxNum(80,200,pm,4,16,0);//显示ADC的值
-
- delay_ms(250);
- LCD_ShowString(70,220,200,16,16,"0.000V");
- ms=Get_Adc3_Average(ADC_Channel_13,5);
- //LCD_ShowxNum(80,220,ms,4,16,0);
- msk=(float)ms*(3.3/4096);
- ms=msk;
- x=(2.852*msk)-(0.210*msk*msk)-7.071;
- LCD_ShowxNum(70,220,ms,1,16,0);//显示电压值
- msk-=ms;
- msk*=1000;
- LCD_ShowxNum(86,220,msk,3,16,0X80);
-
- y=exp(x);
- y=0.76*y;
- LCD_ShowString(80,240,200,16,16,"0.000");
- ms=y;
- LCD_ShowxNum(80,240,ms,1,16,0);
- y-=ms;
- y*=1000;
- LCD_ShowxNum(96,240,y,3,16,0X80);
-
- for (b=0;b<3;b++)
- {
- if (pm>125)
- {
- BEEP=0;
- LED0=!LED0;
- delay_ms(300);//延时300ms
- BEEP=1;
- LED0=!LED0;
- delay_ms(300);
- }
- }
- for (b=0;b<3;b++)
- {
- if (adcx1>1200)
- {
- BEEP=0;
- LED0=!LED0;
- delay_ms(200);//延时300ms
- BEEP=1;
- LED0=!LED0;
- delay_ms(200);
- }
- }
-
- if (adcx1>=1200)
- {
- JDQ1=1;
-
- }
- if (adcx1<1200)
- {
- JDQ1=0;
- }
- if (temperature>26||ms>2000)
- {
- JDQ2=1;
-
- }
-
- if (temperature<= 26&&ms<2000)
- {
- JDQ2=0;
- }
-
- if (humidity<40)
- {
- JDQ3=1;
-
- }
- if (humidity>50)
- {
- JDQ3=0;
- }
- if (pm>125)
- {
- JDQ4=1;
-
- }
- if (pm<100)
- {
- JDQ4=0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
基于单片机的智能家用空气质量监测系统.7z
(241.62 KB, 下载次数: 55)
|