一.系统概述
智能窗户系统的主要功能和特性:
1. 核心控制器及传感器选择:使用STC89C52RC单片机作为主控制器,集成温湿度传感器、光照传感器和MQ-2气体传感器,用于实时监测环境的温度、湿度、光照强度和烟雾浓度。
2. 数据显示与设定:使用LCD1602屏幕实时显示当前环境参数和设备状态。可以通过按键设定温度、烟雾浓度等阈值,当监测数值超过设定阈值时触发相应的控制动作。
3. 报警与控制功能:当烟雾浓度高于设定阈值时,系统会触发蜂鸣器警报。 温度过高时,系统会自动打开风扇和窗户(通过直流电机模拟窗户操作)进行通风降温;温度过低时会关闭窗户。当环境亮度不足时,系统会自动控制LED灯补光。
4. 远程控制与监控:数据可以通过蓝牙App实时显示和监控。用户可以通过手机App远程控制风扇、电机和LED灯的开关状态。
5. 定时功能与窗户控制:系统支持设定定时开窗和关窗的时间。用户也可以通过按键手动控制直流电机模拟的窗户的开关状态。
二.仿真设计
1. 进入仿真后显示屏会显示检测到的温湿度、光强、烟雾浓度以及开关窗的状态,需要通过页面切换按键去切换,此时LED灯亮起,代表光强低于阈值且红外传感器检测到人。
2. 调节温度超过设定阈值就会报警且启动风扇和打开窗户。
3. 此时光强高于阈值且检测不到人LED灯不亮。
4. 烟雾浓度超过阈值就会打开风扇报警且打开窗户,如果窗户是打开状态则不再次打开。
5. 设定开窗时间,设置完点击开始定时按键后开始计时,到达定时时间打开窗户。
6. 检测到的数据会通过上传到上位机端,这里采用虚拟串口和串口助手模拟上位机通信,虚拟串口助手链接是:波特律动 串口助手https://serial.keysking.com/
三.程序设计
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "Key.h"
#include <INTRINS.H>
#include <math.h>
#include "DTH11.h"
#include "Timer0.h"
#include <stdio.h>
sbit LED = P3^6;
sbit fs = P3^5;
sbit buzzer = P3^4;
unsigned char bdata Sdat;
sbit ADC_CS = P2^0;
sbit ADC_CLK = P2^1;
sbit ADC_DIO = P2^2;
sbit SD7 = Sdat^7;
sbit SD0 = Sdat^0;
sbit HW = P3^3;
sbit Continuous_Current_Dynamo1 = P2^7;
sbit Continuous_Current_Dynamo2 = P2^6;
unsigned char voltage[4]="0.00";
unsigned char uart_receive_buffer; //从串口接收的数据
unsigned char uart_receive_number=0,jia;
unsigned char Counter,Compare;
unsigned char rec_dat[6];
unsigned char KeyNum,Flag,KeyFlag = 0,djFLAG = 0;
unsigned char RValue,TValue,Guang,kFlag = 0,k1Flag = 0;
unsigned char TMAX,HMAX,TMIN;
unsigned char N_ADC,VCC,ZDFlag,HWFlag = 0;
unsigned int time,time1,GMAX,SMAX,sj,sj1,sj2;
unsigned char date,dataFlag;
void Com_Init(void);
void sendDate(unsigned char date);
void SendString(unsigned char *p);
void Delay_ms(unsigned int n);
void ADC0832(bit Chx);
void VCCjisuan();
void Count(void);
void main()
{
bit tmp;
unsigned char str[12];
unsigned char DHT[5];
Timer0_Init();
Com_Init();
Delay_ms(100);
LCD_Init();
LCD_ShowString(1,1,"T:00 ");
LCD_ShowString(2,1,"Tmax:00 ");
TMAX = 33;
TMIN = 27;
HMAX = 70;
GMAX = 2000;
SMAX = 2000;
Compare = 50;
fs = 0;
LED = 1;
buzzer = 1;
while(1)
{
KeyNum = Key();
if(KeyNum == 1)
{
if(KeyFlag == 4 && sj1 != 0)
{
KeyFlag--;
kFlag = 1;
}
ZDFlag = 0;
KeyFlag++;
if(KeyFlag>4){KeyFlag=0;}
}
if(KeyNum == 2)
{
if(KeyFlag == 0)
{
if(Flag == 0)
{
TMAX++;
if(TMAX > 50){TMAX = 50;}
if(TMAX < 10){TMAX = 10;}
}
if(Flag == 1)
{
TMIN++;
if(TMIN > 50){TMIN = 50;}
if(TMIN < 10){TMIN = 10;}
}
}
if(KeyFlag == 1)
{
HMAX += 10;
if(HMAX > 100){HMAX = 100;}
if(HMAX < 10){HMAX = 10;}
}
if(KeyFlag == 2)
{
GMAX += 100;
if(GMAX > 6000){GMAX = 6000;}
if(GMAX < 100){GMAX = 100;}
}
if(KeyFlag == 3)
{
SMAX += 100;
if(SMAX > 6000){SMAX = 6000;}
if(SMAX < 100){SMAX = 100;}
}
if(KeyFlag == 4)
{
if(Flag == 0)
{
sj1++;
}
if(Flag == 1)
{
HWFlag = 1;
}
if(Flag == 2)
{
sj2++;
}
}
}
if(KeyNum == 3)
{
if(KeyFlag == 0)
{
if(Flag == 0)
{
TMAX--;
if(TMAX > 50){TMAX = 50;}
if(TMAX < 10){TMAX = 10;}
}
if(Flag == 1)
{
TMIN--;
if(TMIN > 50){TMIN = 50;}
if(TMIN < 10){TMIN = 10;}
}
}
if(KeyFlag == 1)
{
HMAX -= 10;
if(HMAX > 100){HMAX = 100;}
if(HMAX < 10){HMAX = 10;}
}
if(KeyFlag == 2)
{
GMAX -= 100;
if(GMAX > 6000){GMAX = 6000;}
if(GMAX < 100){GMAX = 100;}
}
if(KeyFlag == 3)
{
SMAX -= 100;
if(SMAX > 6000){SMAX = 6000;}
if(SMAX < 100){SMAX = 100;}
}
if(KeyFlag == 4)
{
if(Flag == 0)
{
sj1--;
}
if(Flag == 1)
{
djFLAG = 0;
HWFlag = 0;
}
if(Flag == 2)
{
sj2--;
}
}
}
if(KeyNum == 4)
{
if(KeyFlag == 0)
{
Flag++;
if(Flag > 1)Flag = 0;
}
if(KeyFlag == 4)
{
Flag++;
if(Flag > 2)Flag = 0;
}
}
jia++;
if(jia == 2){jia = 0;
printf("T:%d\r\n",(int)DHT[0]);printf("H:%d\r\n",(int)DHT[2]);printf("G:%d\r\n",(int)time);printf("S:%d\r\n",(int)time1);Delay_ms(500);}
if(date == 1 && dataFlag == 0)
{
ZDFlag = 4;
LED = 0;
dataFlag++;
}
if(date == 2 && dataFlag == 0)
{
ZDFlag = 1;
LED = 1;
dataFlag++;
}
if(date == 5 && dataFlag == 0)
{
ZDFlag = 4;
fs = 1;
dataFlag++;
}
if(date == 6 && dataFlag == 0)
{
ZDFlag = 2;
fs = 0;
dataFlag++;
}
if(date == 3 && dataFlag == 0)
{
ZDFlag = 4;
djFLAG = 1;
dataFlag++;
}
if(date == 4 && dataFlag == 0)
{
ZDFlag = 3;
djFLAG = 0;
dataFlag++;
}
if(KeyFlag == 0)
{
TR0 = 0;
DHT_Start();
tmp=DHT_ByteRead(&DHT);
TR0 = 1;
if(tmp==1)
{
str[0] = (DHT[0]/10)%10 + '0'; //十位转为ASCII码
str[1] = (DHT[0]%10) + '0'; //个位转为ASCII
LCD_ShowString(1,3,str);
}
LCD_ShowString(1,1,"T:");
LCD_ShowString(1,5," ");
LCD_ShowString(2,1,"Tmax:");
LCD_ShowString(2,9,"Tmin:");
LCD_ShowNum(2,6,TMAX,2);
LCD_ShowNum(2,14,TMIN,2);
if(Flag == 0)
{
LCD_ShowString(2,8,"*");
LCD_ShowString(2,16," ");
}
if(Flag == 1)
{
LCD_ShowString(2,8," ");
LCD_ShowString(2,16,"*");
}
}
if(KeyFlag == 1)
{
TR0 = 0;
DHT_Start();
tmp=DHT_ByteRead(&DHT);
TR0 = 1;
if(tmp==1)
{
str[0] = (DHT[2]/10)%10 + '0'; //十位转为ASCII码
str[1] = (DHT[2]%10) + '0'; //个位转为ASCII
LCD_ShowString(1,3,str);
}
LCD_ShowString(1,1,"H:");
LCD_ShowString(1,5," ");
LCD_ShowString(2,1,"Hmax:");
LCD_ShowString(2,8," ");
LCD_ShowNum(2,6,HMAX,2);
}
ADC0832(1);
VCCjisuan();
time=VCC*3.3*10;
if(KeyFlag == 2)
{
LCD_ShowString(1,1,"G:");
LCD_ShowString(2,1,"Gmax:");
LCD_ShowString(1,7," ");
LCD_ShowNum(1,3,time,4);
LCD_ShowNum(2,6,GMAX,4);
}
ADC0832(0);
VCCjisuan();
time1=VCC*3.3*10;
if(KeyFlag == 3)
{
LCD_ShowString(1,1,"S:");
LCD_ShowString(2,1,"Smax:");
LCD_ShowString(1,7," ");
LCD_ShowNum(1,3,time1,4);
LCD_ShowNum(2,6,SMAX,4);
}
if(KeyFlag == 4)
{
LCD_ShowString(1,2,"OT:");
LCD_ShowString(1,9,"CT:");
LCD_ShowString(2,2,"key control");
LCD_ShowNum(1,5,sj1,4);
LCD_ShowNum(1,12,sj2,4);
if(Flag == 0)
{
LCD_ShowString(1,1,"*");
LCD_ShowString(2,1," ");
LCD_ShowString(1,16," ");
}
if(Flag == 1)
{
LCD_ShowString(1,1," ");
LCD_ShowString(2,1,"*");
LCD_ShowString(1,16," ");
}
if(Flag == 2)
{
LCD_ShowString(1,1," ");
LCD_ShowString(2,1," ");
LCD_ShowString(1,16,"*");
}
}
if(ZDFlag != 1 && ZDFlag < 4)
{
if(time <= GMAX){LED = 0;}
else {LED = 1;}
}
if(ZDFlag != 2 && ZDFlag < 4)
{
if(time1 > SMAX){fs = 1;}
else if(TMAX < DHT[0]){fs = 1;}
else if(time1 <= SMAX && TMAX >= DHT[0])fs = 0;
}
if(ZDFlag != 3 && ZDFlag < 4 && KeyFlag != 4)
{
if(TMAX < DHT[0]){djFLAG = 1;}
else if(TMAX >= DHT[0]){djFLAG = 0;}
}
if(time1 > SMAX){buzzer = 0;}
else {buzzer = 1;}
if(KeyFlag < 4)
{
if(djFLAG == 1)
{
LCD_ShowString(1,8,"open w ");
}
if(djFLAG == 0)
{
LCD_ShowString(1,8,"close w");
}
}
if(HWFlag == 1)
{
if(HW == 1){djFLAG = 1;buzzer = 1;}
else {djFLAG = 0;buzzer = 0;}
}
if(djFLAG == 1)
{
Continuous_Current_Dynamo1 = 1;
Continuous_Current_Dynamo2 = 0;
}
if(djFLAG == 0)
{
Continuous_Current_Dynamo1 = 1;
Continuous_Current_Dynamo2 = 1;
}
}
}