#include <REGX52.H>#include<intrins.h>#include<stdio.h>#include "Delay.h"#include "LCD1602.h"#define uchar unsigned char#define uint unsigned intsbit beep=P3^4;
sbit key1=P3^6; // sbit key2=P3^5; // 减少sbit key3=P1^0; // 增加sbit key4=P1^1; // 增加sbit jdq = P3^3;//继电器sbit Temp\_data=P2^6; //DHT11unsigned char rec\_dat\_lcd0\[6\];unsigned char rec\_dat\_lcd1\[6\];unsigned char rec\_dat\_lcd2\[6\];unsigned char rec\_dat\_lcd3\[6\];unsigned int rec\_dat\[4\];static uchar wd,sd;static int wdyz=40,sdyz=80;void DHT11\_delay\_us(unsigned char n);void DHT11\_delay\_ms(unsigned int z);void DHT11\_start();unsigned char DHT11\_rec\_byte();void DHT11\_receive();void beep\_warning();void cshq();void xxpxs();void ajjc();//延时msvoid DHT11\_delay_ms(unsigned int z){ unsigned int i,j; for(i=z; i>0; i--) for(j=110; j>0; j--);
}//延时us --2*n+5usvoid DHT11\_delay\_us(unsigned char n){ while(--n);
}//DHT11起始信号void DHT11_start(){
Temp\_data=1; DHT11\_delay_us(10);
Temp\_data=0; DHT11\_delay_ms(50);//这个延时不能过短,18ms以上,实际在仿真当中要想读到数据延时要在延时参数要在40以上才能出数据
Temp\_data=1; DHT11\_delay\_us(30);//这个延时不能过短}//接收一个字节unsigned char DHT11\_rec_byte(){ unsigned char i,dat=0; for(i=0; i<8; i++)
{ while(!Temp\_data); DHT11\_delay_us(8);
dat <<=1; if(Temp_data==1)
{
dat +=1;
} while(Temp_data);
} return dat;
}//接收温湿度数据void DHT11\_receive(){ unsigned int R\_H,R\_L,T\_H,T\_L; unsigned char RH,RL,TH,TL,revise; DHT11\_start();
Temp\_data=1; if(Temp\_data==0)
{ while(Temp_data==0); //等待拉高
DHT11\_delay\_us(40); //拉高后延时80us
R\_H=DHT11\_rec_byte(); //接收湿度高八位
R\_L=DHT11\_rec_byte(); //接收湿度低八位
T\_H=DHT11\_rec_byte(); //接收温度高八位
T\_L=DHT11\_rec_byte(); //接收温度低八位
revise=DHT11\_rec\_byte(); //接收校正位
DHT11\_delay\_us(25); //结束
if((R\_H+R\_L+T\_H+T\_L)==revise) //校正
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
} /*数据处理,方便显示*/
rec_dat\[0\]=RH;
rec_dat\[1\]=RL;
rec_dat\[2\]=TH;
rec_dat\[3\]=TL;
}
}void dht11(){ DHT11\_delay\_ms(150); DHT11\_receive(); sprintf(rec\_dat\_lcd0,"%d",rec\_dat\[0\]); sprintf(rec\_dat\_lcd1,"%d",rec\_dat\[1\]); sprintf(rec\_dat\_lcd2,"%d",rec\_dat\[2\]); sprintf(rec\_dat\_lcd3,"%d",rec\_dat\[3\]); DHT11\_delay_ms(100);
wd = rec\_dat\[3\]*10 + rec\_dat\[2\];
sd = rec\_dat\[1\]*10 + rec\_dat\[0\];
}void beep_warning()//蜂鸣器警报并且电机转动{ if(wd>wdyz)
{
beep = 1;
jdq = 0;
}
if(sd>sdyz)
{
beep = 1;
jdq = 0;
}
if(wd<=wdyz && sd<=sdyz )
{
beep = 0;
jdq = 1;
}
}void main() //主函数{
LCD_Init(); //显示屏初始化
beep = 0;
jdq = 1; do
{ dht11(); //温湿度获取
ajjc(); //按键检测
xxpxs(); //显示屏显示
beep_warning(); //状态判断
} while(1);
}void ajjc() //按键检测{ if(!key1)
{
wdyz++; while(!key1);
}
if(!key2)
{
wdyz--; while(!key2);
}
if(!key3)
{
sdyz++; while(!key3);
}
if(!key4)
{
sdyz--; while(!key4);
}
}void xxpxs() //显示屏显示{
LCD_ShowString(1,1,"wd:");
LCD_ShowString(1,8,"sd:");
LCD_ShowNum(1,4,wdyz,3); //
LCD_ShowNum(1,11,sdyz,3);//
LCD\_ShowString(2,1,"wd:"); LCD\_ShowNum(2,4,wd,3);//
LCD_ShowString(2,8,"sd:");
LCD_ShowNum(2,11,sd,3);//
}