DHT11数字温湿度传感器附1602显示程序已验证okWord格式.docx
《DHT11数字温湿度传感器附1602显示程序已验证okWord格式.docx》由会员分享,可在线阅读,更多相关《DHT11数字温湿度传感器附1602显示程序已验证okWord格式.docx(8页珍藏版)》请在冰豆网上搜索。
U8U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8U8comdata;
sbitP2_0=P2^0;
sbitP2_1=P2^1;
sbitP2_2=P2^2;
sbitP2_3=P2^3;
unsignedcharstr1[]="
ShiDu:
"
;
unsignedcharstr2[]="
WenDu:
voidDelay(U16j)
{
U8i;
for(;
j>
0;
j--)
{
for(i=0;
i<
27;
i++);
}
voidDelay_10us(void)
{
i--;
}
voidCOM(void)
8;
i++)
U8FLAG=2;
//----------------------
P2_1=0;
//T
P2_1=1;
//----------------------
while((!
P2_0)&
&
U8FLAG++);
Delay_10us();
//Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
while((P2_0)&
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是0还是1
//如果高电平高过预定0高电平值则数据位为1
U8comdata<
<
=1;
U8comdata|=U8temp;
//0
}//rof
}
//--------------------------------
//-----湿度读取子程序------------
//----以下变量均为全局变量--------
//----温度高8位==U8T_data_H------
//----温度低8位==U8T_data_L------
//----湿度高8位==U8RH_data_H-----
//----湿度低8位==U8RH_data_L-----
//----校验8位==U8checkdata-----
//----调用相关子程序如下----------
//----Delay();
Delay_10us();
COM();
voidRH(void)
{
//主机拉低18ms
P2_0=0;
Delay(180);
P2_0=1;
//总线由上拉电阻拉高主机延时20us
//主机设为输入判断从机响应信号
//判断从机是否有低电平响应信号如不响应则跳出,响应则向下运行
if(!
P2_0)
//T!
//判断从机是否发出80us的低电平响应信号是否结束
//判断从机是否发出80us的高电平,如发出则进入数据接收状态
//数据接收状态
COM();
U8RH_data_H_temp=U8comdata;
U8RH_data_L_temp=U8comdata;
U8T_data_H_temp=U8comdata;
U8T_data_L_temp=U8comdata;
U8checkdata_temp=U8comdata;
//数据校验
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}//fi
voidmain()
U8shidu_shi,shidu_ge,wendu_shi,wendu_ge;
LCD_init();
delay_nms
(2);
LCD_write_string(0,LINE1,str1);
LCD_write_string(0,LINE2,str2);
while
(1)
{
RH();
shidu_shi=0x30+U8RH_data_H/10;
shidu_ge=0x30+U8RH_data_H%10;
//shidu_xs=0x30+U8RH_data_L/10;
wendu_shi=0x30+U8T_data_H/10;
wendu_ge=0x30+U8T_data_H%10;
//wendu_xs=0x30+U8T_data_L/10;
LCD_write_char(6,0,shidu_shi);
LCD_write_char(7,0,shidu_ge);
//LCD_write_char(9,0,shidu_xs);
LCD_write_char(6,1,wendu_shi);
LCD_write_char(7,1,wendu_ge);
//LCD_write_char(9,1,wendu_xs);
H文件
ifndeflcd_H
#definelcd_H
/************************************/
//commonpart
#define
HIGH
1
LOW
0
TRUE
FALSE
ZERO
0
MSB
0x80
LSB
0x01
//lcdpart
#defineLINE1
#defineLINE2
#defineLINE1_HEAD
#defineLINE2_HEAD
0xC0
#defineDATA_MODE
0x38
#defineOPEN_SCREEN
0x0C
#defineDISPLAY_ADDRESS
#defineCLEARSCREEN
LCD_en_com(0x01)
/*************************************/
//changethispartatdifferentboard
#defineLCDIO
P1
sbitLCD1602_RS=P2^4;
sbitLCD1602_RW=P2^5;
sbitLCD1602_EN=P2^6;
/********************************************************************/
voidLCD_Read_BF(void)
unsignedcharread=0;
LCD1602_RS=LOW;
//RS0
LCD1602_RW=HIGH;
//RW1
LCD1602_EN=HIGH;
//EN1ReadBF
LCDIO=0xFF;
do{
read=LCDIO;
}while(read&
MSB);
voidLCD_en_com(unsignedcharcommand)
//写指令
LCD_Read_BF();
//RS0
LCD1602_RW=LOW;
//RW0
//EN--\|/__Writecommand
LCDIO=command;
LCD1602_EN=LOW;
voidLCD_en_dat(unsignedcha