#include<reg51.h>
#include<intrins.h> //包含所需要的机械延时
#define uint unsigned int
#define uchar unsigned char
/*******************************
*********dht22相关配置**********
***只需要直接调用数据接收函数****
*******************************/
sbit DA=P2^4; //数据接受地址
unsigned char DHT22_init();
unsigned char ReadTH();
unsigned char ReadTL();
unsigned char ReadHH();
unsigned char ReadHL();
static int THmax_val=35;
static int THmin_val=10;
static int RHmax_val=70;
static int RHmin_val=30;
int T;
int H;
int temp1;
int temp2;
unsigned char TL; //储存暂存器的温度低位
unsigned char TH; //储存暂存器的温度高位
unsigned char HL;
unsigned char HH;
sbit LED1=P1^1;
sbit LED2=P1^2;
sbit LED3=P1^3;
/*****LCD1602相关函数*********
*RW接地即可,也可以配置为低电平*
*只需简单延时即可,无需复杂操作*
*****************************/
void write_com(uchar com); //写命令
void write_data(uchar date);//写数据
void lcd1602init();
void delay(unsigned int x_ms) ;
void delayms(uint z);
void delayus(uint n);
sbit lcdrs=P2^5;
sbit lcden=P2^7;
sbit lcdrw=P2^6;
int abc = 0;
int bcd=0;
/*********按键相关函数******/
sbit K1 = P1^7;
sbit K2 = P1^6;
sbit K3 = P1^5;
sbit K4 = P1^4;
void THmax();
void THmin();
void RHmax();
void RHmin();
/******* 定时器相关函数****/
void timer_init(); //定时器初始化
int aaa;
/***********主函数**********/
void main()
{
delayms(500);
timer_init();
lcd1602init(); //1602初始化
delayms(500);
while(1)
{
if(K1 ==0)
{
while(1)
{
write_com(0x01);
write_com(0x80); //第一行从开头写
write_data('T');
write_data('h');
write_data('r');
write_data('e');
write_data('s');
write_data('h');
write_data('o');
write_data('l');
write_data('d');
write_com(0x80+0x40);
write_data('2');
write_data('.');
write_data('T');
write_data('H');
write_data(' ');
write_data('3');
write_data('.');
write_data('R');
write_data('H');
write_data(' ');
write_data('4');
write_data('.');
write_data('b');
write_data('a');
write_data('c');
write_data('k');
while((K2==1)&&(K3==1)&&(K4==1));
//如果是温度调节
if((K2==0)&&(K3==1))
{
while(K2==0);
while(1)
{
write_com(0x01);
write_com(0x80); //第一行从开头写
write_data('2');
write_data('.');
write_data('T');
write_data('H');
write_data('m');
write_data('a');
write_data('x');
write_com(0x80+0x40);
write_data('3');
write_data('.');
write_data('T');
write_data('H');
write_data('m');
write_data('i');
write_data('n');
write_com(0x80+0x40+9);
write_data('4');
write_data('.');
write_data('b');
write_data('a');
write_data('c');
write_data('k');
while((K2==1)&&(K3==1)&&(K4==1));
//如果是温度最大值
if((K2==0)&&(K3==1))
{
while(K2==0);
THmax();
}
//如果是温度最小值
if((K2==1)&&(K3==0))
{
while(K3==0);
THmin();
}
//如果是退出
if((K4==0))
{
while((K4==0));
break;
}
}
}
//如果是湿度调节
if((K2==1)&&(K3==0))
{
while(K3==0);
while(1)
{
write_com(0x01);
write_com(0x80); //第一行从开头写
write_data('2');
write_data('.');
write_data('R');
write_data('H');
write_data('m');
write_data('a');
write_data('x');
write_com(0x80+0x40);
write_data('3');
write_data('.');
write_data('R');
write_data('H');
write_data('m');
write_data('i');
write_data('n');
write_com(0x80+0x40+9);
write_data('4');
write_data('.');
write_data('b');
write_data('a');
write_data('c');
write_data('k');
while((K2==1)&&(K3==1)&&(K4==1));
//如果是湿度阈值最大值
if((K2==0)&&(K3==1))
{
while(K2==0);
RHmax();
}
// 如果是湿度阈值最小值
if((K2==1)&&(K3==0))
{
while(K3==0);
RHmin();
}
// 如果是退出
if((K4==0))
{
while((K4==0));
break;
}
}
}
//如果是退出
if(K4==0)
{
while(K4==0);
write_com(0x01);
break;
}
}
}
else if(K1!=0)
{
write_com(0x80);
write_data('T');
write_data('H');
write_data(' ');
write_data('0'+temp1/10/10);
write_data('0'+temp1/10%10);
write_data('.');
write_data('0'+temp1%10);
write_data(' ');
write_data(' ');
write_data('R');
write_data('H');
write_data(' ');
write_data('0'+temp2/10/10);
write_data('0'+temp2/10%10);
write_data('.');
write_data('0'+temp2%10);
write_com(0x80+0x40);
write_data('1');
write_data('.');
write_data('T');
write_data('h');
write_data('r');
write_data('e');
write_data('s');
write_data('h');
write_data('o');
write_data('l');
write_data('d');
if ((temp1>= (THmax_val*10))||(temp1< (THmin_val*10)))
LED2=0;
else LED2=1;
if (((temp2)>=( RHmax_val*10))||(temp2< (RHmin_val*10)))
LED3=0;
else LED3=1;
if(LED2==0||LED3==0)
LED1=1;
else LED1=0;
}
}
}
/*开始*/
/***********dht22相关子函数*************/
/*********LCD1602相关子函数**********/
void write_com(uchar com)
{
lcdrs=0;
P3=com;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1;
P3=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void lcd1602init()
{
lcdrw=0;
lcden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
/*************简单延时函数************/
void delay(unsigned int x_ms)
{
unsigned char i, j;
while(x_ms)
{ i = 2;
j = 239;
do
{
while (--j);
} while (--i);
x_ms--;
}
}
void delayms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/************定时器相关函数***********/
void timer_init()
{
TMOD=0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
ET0 = 1 ;
TR0 = 1 ;
EA = 1 ;
}
void timer0()interrupt 1{
aaa+=1;
TH0 = 15536/256;
TL0 = 15536%256;
if(aaa==10)
{
aaa=0;
DHT22_init();
HH=ReadHH();
HL=ReadHL();
TH=ReadTH()