/********************DHT11.h********************/
#ifndef DHT11_H
#define DHT11_H
#include <reg52.h>
#include "common.h"
//溫濕度傳感器DHT11數(shù)據(jù)接口
sbit DHT11Dout = P2^0;
uchar DHT11ReceiveByte(void);
void DHT11ReadData(void);
#endif
/********************DHT11.c********************/
#include "DHT11.h"
#include "system.h"
/*uchar DHT11Data[6]
用來保存當(dāng)前的溫度、濕度
[0:1]濕度整數(shù)部分
[2]濕度小數(shù)部分
[3:4]溫度整數(shù)部分
[5]溫度小數(shù)部分
*/
uchar DHT11Data[6]={0, 0, 0, 0, 0, 0};
static void delay_25ms(void) //誤差 0us
{
unsigned char a,b,c;
for(c=7;c>0;c--)
for(b=16;b>0;b--)
for(a=110;a>0;a--);
}
static void delay_30us(void) //誤差 0us
{
unsigned char a,b;
for(b=3;b>0;b--)
for(a=3;a>0;a--);
}
uchar DHT11ReceiveByte(void)
{
uchar i;
uchar com_data, temp;
for(i=0; i<=7; i++)
{
while(!DHT11Dout);
delay_30us();
temp = 0;
if(DHT11Dout){temp = 1;}
while(DHT11Dout);
com_data <<= 1;
com_data |= temp;
}
return com_data;
}
void DHT11ReadData(void)
{
uint untemp=0;
uint TH_data=0, TL_data=0, RH_data=0, RL_data=0, CK_data=0;
uint TH_temp=0, TL_temp=0, RH_temp=0, RL_temp=0, CK_temp=0;
while(1){
//主機(jī)拉低必須大于18ms
DHT11Dout = 0;
delay_25ms();
DHT11Dout = 1;
//DATA總線由上拉電阻拉高主機(jī)延時(shí)30us
delay_30us();
//判斷DHT11是否有低電平響應(yīng)信號(hào)如不響應(yīng)則跳出,響應(yīng)則向下運(yùn)行
if(!DHT11Dout){
//判斷DHT11發(fā)出 80us 的低電平響應(yīng)信號(hào)是否結(jié)束
while(!DHT11Dout);
//判斷從機(jī)是否發(fā)出 80us 的高電平,如發(fā)出則進(jìn)入數(shù)據(jù)接收狀態(tài)
while(DHT11Dout);
//數(shù)據(jù)接收狀態(tài)
RH_temp = DHT11ReceiveByte(); //8bit溫度整數(shù)數(shù)據(jù)
RL_temp = DHT11ReceiveByte(); //8bit溫度小數(shù)數(shù)據(jù)
TH_temp = DHT11ReceiveByte(); //8bit濕度整數(shù)數(shù)據(jù)
TL_temp = DHT11ReceiveByte(); //8bit濕度小數(shù)數(shù)據(jù)
CK_temp = DHT11ReceiveByte(); //8bit校驗(yàn)位
delay_30us();
delay_30us();
DHT11Dout = 1;
//數(shù)據(jù)校驗(yàn)
untemp = RH_temp + RL_temp + TH_temp + TL_temp;
if(untemp==CK_temp){
RH_data = RH_temp;
RL_data = RL_temp;
TH_data = TH_temp;
TL_data = TL_temp;
CK_data = CK_temp;
break;//跳出while循環(huán)
}
}
}
//濕度整數(shù)部分
DHT11Data[0] = RH_data/10;
DHT11Data[1] = RH_data%10;
//濕度小數(shù)部分 不做處理
DHT11Data[2] = RL_data ;//轉(zhuǎn)為小數(shù)時(shí)除以256即可
//溫度整數(shù)部分
DHT11Data[3] = TH_data/10;
DHT11Data[4] = TH_data%10;
//溫度小數(shù)部分 不做處理
DHT11Data[5] = TL_data; //轉(zhuǎn)為小數(shù)時(shí)除以256即可
}
/*************************system.h*****************************/
#ifndef SYSTEM_H
#define SYSTEM_H
#include "common.h"
//蜂鳴器、LED報(bào)警接口
sbit AlarmBuzzer = P2^4; //響:聲報(bào)警
sbit LED4 = P3^5; //亮:光報(bào)警
//工作情況指示燈:
sbit LED3 = P3^6; //亮:啟動(dòng)制冷
sbit LED2 = P3^7; //亮:啟動(dòng)抽濕
void SystemInit(void);
void SystemControl(void);
void SystemSet(void);
void SystemInformationDisplay(void);
#endif
/***********************system.c************************/
#include "system.h"
#include "lcd1602.h"
#include "DHT11.h"
#include "key.h"
#include "eeprom.h"
uchar TemperatureLimit = 32; //溫度上限值
uchar GlobeTemperature = 30;//要設(shè)置的溫度
uchar TemperatureMin = 6; //溫度值下限值
uchar HumidityLimit = 80; //濕度上限值
uchar GlobeHumidity = 66; //要設(shè)置的濕度
uchar HumidityMin = 10; //濕度下限值
uchar SYSTEM_FLAGS = 0; //如果系統(tǒng)不是第一次啟動(dòng)過,則設(shè)置為1,否為0
uchar flg = 0;//按鍵控制
extern uchar DHT11Data[6];
//系統(tǒng)初始化
void SystemInit(void)
{
uchar *Project = "Granary Control";
uchar *Designer = "Designer: HongDe";
AlarmBuzzer = 1;
LED4 = 0;
LED3 = 0;
LED2 = 0;
LcdInit(); //初始化LCD
DelayMs(10);
LcdSetPosition(1); //設(shè)置顯示位置
while(*Project != '\0'){//在LCD1602第1行顯示“Granary Control”
LcdWriteData(*Project);
Project++;
}
LcdSetPosition(0x40); //設(shè)置顯示位置
while(*Designer != '\0'){//在LCD1602第1行顯示“Granary Control”
LcdWriteData(*Designer);
Designer++;
}
SYSTEM_FLAGS = eepromReadByte(0x2016);//從第一扇區(qū)讀系統(tǒng)啟動(dòng)參數(shù)
if(SYSTEM_FLAGS==1){//如果系統(tǒng)不是第一次啟動(dòng)過,則讀取先前存儲(chǔ)的參數(shù)值
GlobeTemperature = eepromReadByte(0x2000);//讀取存放在eeprom中的溫度節(jié)點(diǎn)值
TemperatureLimit = eepromReadByte(0x2004); //讀取存放在eeprom中的溫度安全節(jié)點(diǎn)值
GlobeHumidity = eepromReadByte(0x2008); //讀取存放在eeprom中的濕度節(jié)點(diǎn)值
HumidityLimit = eepromReadByte(0x2012); //讀取存放在eeprom中的濕度安全節(jié)點(diǎn)值
}
}
void SystemControl(void)
{
uchar temp=0;
uchar rh=0;
DHT11ReadData();//獲取溫度、濕度
temp = DHT11Data[3]*10 + DHT11Data[4]; //溫度整數(shù)部分
rh = DHT11Data[0]*10 + DHT11Data[1];//濕度整數(shù)部分,小數(shù)部分未處理
if((rh>HumidityLimit)||(temp>TemperatureLimit)){//當(dāng)前溫度高于安全點(diǎn)溫度或者當(dāng)前濕度高于安全點(diǎn)濕度
while(1){
//更新當(dāng)前環(huán)境溫度
DHT11ReadData();//獲取溫度、濕度
rh= DHT11Data[0]*10 + DHT11Data[1];//濕度整數(shù)部分,小數(shù)部分未處理
temp = DHT11Data[3]*10+DHT11Data[4];
if(temp>TemperatureLimit)LED3 = 1; //啟動(dòng)制冷
if(rh>HumidityLimit)LED2 = 1; //啟動(dòng)抽濕
LED4 = ~LED4; //燈亮閃爍報(bào)警
AlarmBuzzer = ~AlarmBuzzer; //蜂鳴器鳴叫
DelayMs(5);
if((temp<TemperatureLimit)&&(temp<GlobeTemperature)){LED3 = 0; LED4 = 0; AlarmBuzzer = 1;}//停止制冷
if((rh<HumidityLimit)&&(rh<GlobeHumidity)){LED2 = 0; LED4 = 0; AlarmBuzzer = 1;}//停止抽濕
if((rh<HumidityLimit)&&(temp<TemperatureLimit)){LED4 = 0; break;}//停止報(bào)警
SystemSet();//設(shè)置
SystemInformationDisplay();//溫濕度顯示
}
}
if(temp>GlobeTemperature){
LED3 = 1; //啟動(dòng)制冷
}else{LED3 = 0;}//關(guān)閉制冷
if(rh>GlobeHumidity){
LED2 = 1; //啟動(dòng)抽濕
}else{LED2 = 0;}//停止抽濕
AlarmBuzzer = 1;
}
void SystemSet(void)
{
uchar key;
uchar eepromFlags = 0;
key = ScanKey();
if(key==SW2){//按下按鍵SW2
flg++;
if(flg==5){flg = 1;}
}
switch(flg){
case 1:{//溫度節(jié)點(diǎn)調(diào)節(jié)(即溫度節(jié)點(diǎn))
if(key==SW1){ --GlobeTemperature; eepromFlags = 1;}//減操作
if(key==SW3){ ++GlobeTemperature; eepromFlags = 1;}//加操作
if(GlobeTemperature==TemperatureMin-1)GlobeTemperature = TemperatureLimit;
if(GlobeTemperature==TemperatureLimit+1)GlobeTemperature = TemperatureMin;
break;
}
case 2:{//溫度安全節(jié)點(diǎn)調(diào)節(jié)
if(key==SW1){ --TemperatureLimit; eepromFlags = 1;}//減操作
if(key==SW3){ ++TemperatureLimit; eepromFlags = 1;}//加操作
break;
}
case 3:{//濕度節(jié)點(diǎn)調(diào)節(jié)
if(key==SW1){ --GlobeHumidity; eepromFlags = 1;}//減操作
if(key==SW3){ ++GlobeHumidity; eepromFlags = 1;}//加操作
if(GlobeHumidity==HumidityMin-1)GlobeHumidity = HumidityLimit;
if(GlobeHumidity==HumidityLimit+1)GlobeHumidity = HumidityMin;
break;
}
case 4:{ //濕度安全節(jié)點(diǎn)調(diào)節(jié)
if(key==SW1){--HumidityLimit; eepromFlags = 1;}//減操作
if(key==SW3){++HumidityLimit; eepromFlags = 1;}//加操作
break;
}
default: eepromFlags = 0; break;
}
if(eepromFlags){//系統(tǒng)工作參數(shù)有變動(dòng),參數(shù)存eeprom
eepromErase(0x2000);//擦除第一個(gè)扇區(qū)
eepromWriteByte(0x2000, GlobeTemperature);//存放溫度節(jié)點(diǎn)值
eepromWriteByte(0x2004, TemperatureLimit); //存放溫度安全節(jié)點(diǎn)值
eepromWriteByte(0x2008, GlobeHumidity); //存放濕度節(jié)點(diǎn)值
eepromWriteByte(0x2012, HumidityLimit); //存放濕度安全節(jié)點(diǎn)值
SYSTEM_FLAGS = 1;
eepromWriteByte(0x2016, SYSTEM_FLAGS); //存放系統(tǒng)啟動(dòng)過的標(biāo)志
}
}
//顯示溫濕度信息
//顯示設(shè)置的溫濕度值,和當(dāng)前環(huán)境的溫濕度值
void SystemInformationDisplay(void)
{
char *TempStr = "t/tS/TH:";
char *RHStr = "r/rS/RH:";
uchar i=0;
LCDClear();
LcdSetPosition(0x40); //設(shè)置濕度顯示在中間位置,第2行
while(*RHStr != '\0'){
LcdWriteData(*RHStr);
RHStr++;
}
LcdWriteData(DHT11Data[0]+'0');
LcdWriteData(DHT11Data[1]+'0');
LcdWriteData('/');
LcdWriteData(GlobeHumidity/10+'0');
LcdWriteData(GlobeHumidity%10+'0');
LcdWriteData('/');
LcdWriteData(HumidityLimit/10+'0');
LcdWriteData(HumidityLimit%10+'0');
LcdSetPosition(0x00); //設(shè)置溫度顯示在中間位置,第1行
while(*TempStr != '\0'){
LcdWriteData(*TempStr);
TempStr++;
}
LcdWriteData(DHT11Data[3]+'0');
LcdWriteData(DHT11Data[4]+'0');
LcdWriteData('/');
LcdWriteData(GlobeTemperature/10+'0');
LcdWriteData(GlobeTemperature%10+'0');
LcdWriteData('/');
LcdWriteData(TemperatureLimit/10+'0');
LcdWriteData(TemperatureLimit%10+'0');
}