/**********************DHT11与12864LCD************************/
/* DHT11总线接P1^0 */
/* */
/*************************************************************/#include <reg52.h>
#define uchar unsigned char
#define uint unsigned intuchar lcd_x,lcd_y,data_byte=0,count;
uint TH_data,TL_data,RH_data,RL_data,CK_data;
uint TH_temp,TL_temp,RH_temp,RL_temp,CK_temp;
uchar num;sbit RS = P2^0;//并行指令,数据选择信号
sbit RW = P2^1;//读写信号
sbit EN = P2^2;//并行使能信号
sbit PSB = P2^3;//并串口选择信号
sbit RST = P2^5;//复位信号
sbit io = P1^0;//DHT11总线void read_io();//开始信号,读数据并校验
void delay(uint a);//延时函数
void LCD_init();//LCD初始化程序
void write_cmd(uchar cmd);//写指令
void write_data(uchar dat);//写数据
bit check_busy();//检测LCD忙信号
//void clear_gcrom();//清除GCROM
void delay1();//10us延时
void lcd_dis_code(uint cmd_addr,uchar num,uchar dis[]);//显示函数
void lcd_pos(unsigned char X,unsigned char Y);//LCD显示位置计算uchar code lcd[]={"温湿度检测系统 "};
uchar code lcd1[]={" 温度:"};
uchar code lcd2[]={" 湿度:"};
uchar code lcd3[]={"℃"};
uchar code lcd4[]={"%RH"};uchar str1[]={" "};
uchar str2[]={" "};
void main()
{
uchar k,j;
LCD_init();
lcd_dis_code(0x80,16,lcd);
lcd_dis_code(0x88,9,lcd1);
lcd_dis_code(0x98,9,lcd2);
lcd_dis_code(0x8e,2,lcd3);
lcd_dis_code(0x9e,5,lcd4);
while(1)
{
read_io();//读取温湿度数据
//clear_gcrom();
//湿度
for(k=0; k<2; k++)
{
lcd_pos(4,k+4);
write_data(str1[k]);
}
//clear_gcrom();
//温度
for(j=0; j<2; j++)
{
lcd_pos(3,j+4);
write_data(str2[j]);
} delay(500);
}
}
void delay1()//延时10us
{
unsigned char i;
for(i=0; i<3; i++);
}void lcd_pos(unsigned char X,unsigned char Y)
{
unsigned char pos;
if ( X == 1 )
{ pos = 0x80 ; }
else if ( X == 2 )
{ pos = 0x90 ; }
else if ( X == 3 )
{ pos = 0x88 ; }
else
{ pos = 0x98 ; }
write_cmd(Y | pos); //数据指针=80+地址变量
}/**********************DHT11模块**********************/
uchar receive_byte()//接收一个字节
{
uchar i,temp;
for(i=0;i<8; i++)
{
count = 2;
while((!io) && count++)//等待50us低电平结束,并防止死循环
temp = 0;
delay1();
delay1();
delay1();
if (io==1)
{
temp = 1;
count = 2;
while(io && count++);
}
else
temp = 0;
data_byte<<=1;
data_byte|=temp;
}
return (data_byte);
}void read_io()//开始信号,读数据并校验
{
io = 0;
delay(18);//主机拉低18ms
io = 1;//DATA总线由上拉电阻拉高 主机延时20us
delay1();
delay1();
delay1();
delay1();
io = 1;//主机设置为输入高电平,判断从机响应信号
if(!io)
{
count = 2;
while((!io)&&count++);//判断DHT11发出80us低电平响应信号是否结束
count = 2;
while(io && count++);//判断DHT11拉高总线80us高电平是否结束
RH_temp = receive_byte();
RL_temp = receive_byte();
TH_temp = receive_byte();
TL_temp = receive_byte();
CK_temp = receive_byte();
io = 1;
num = (RH_temp + RL_temp + TH_temp + TL_temp);//数据校验 if(num == CK_temp)
{
RH_data = RH_temp;
RL_data = RL_temp;
TH_data = TH_temp;
TL_data = TL_temp;
CK_data = CK_temp;
}
}
//湿度整数部分
str1[0] = (char)(0x30 + RH_data/10);
str1[1] = (char)(0x30 + RH_data%10);
//温度整数部分
str2[0] = (char)(0x30 + TH_data/10);
str2[1] = (char)(0x30 + TH_data%10);
}
/**********************E N D**********************//**********************12864模块程序**********************/
/*void clear_gcrom()
{
uchar i,j,k;
lcd_x = 0x80;
lcd_y = 0x80;
write_cmd(0x34); for(i = 0 ; i < 2 ; i++) //上下两屏幕
{
for(j = 0 ; j < 32 ; j++)
{
write_cmd(lcd_y+j);
write_cmd(lcd_x);
for(k = 0 ; k < 16 ; k++)
{
write_data(0x00);
}
}
lcd_x = 0x88;
}
write_cmd(0x36);
write_cmd(0x30);
}*/ void lcd_dis_code(uint cmd_addr,uchar num,uchar dis[])
{
uint i;
write_cmd(cmd_addr);
for(i=0; i<num; i++)
write_data(dis[i]);
}void LCD_init()
{
PSB = 1;
RST = 0;
delay(5);
RST = 1;
delay(5);
write_cmd(0x34);//扩充指令
delay(5);
write_cmd(0x30);//基本指令
delay(5);
write_cmd(0x0c);//显示开关光标
delay(5);
write_cmd(0x01);//清屏
delay(5);}bit check_busy()
{
bit temp;
RS = 0;
RW = 1;
EN = 1;
delay(5);
temp = (bit)(P0&0x80); //按位与操作
EN = 0;
return (temp);
}void write_cmd(uchar cmd)
{
while(check_busy());
RS = 0;
RW = 0;
EN = 0;
delay(5);
P0 = cmd;
delay(5);
EN = 1;
delay(5);
EN = 0;
}void write_data(uchar dat)
{
while(check_busy());
RS = 1;
RW = 0;
EN = 0;
P0 = dat;
delay(5);
EN = 1;
delay(5);
EN = 0;
}void delay(uint a)
{
uchar i;
while(a--)
{
for(i=0; i<113; i++);
}
}
/**********************E N D**********************/