各位老师,我对温度湿度进行测量,使用数字温湿度传感器DHT11,显示测量值时发现一上电湿度数值就不停跳动,跳动数值幅度有十几,温度数值基本没变,感觉测量湿度数值不正确,请各位帮忙指导下,谢谢。
程序如下,MCU为16F873,晶振为10MHz
#include "pic.h"
#define uchar unsigned char
#define uint unsigned int
#define LCD_RS RC5
#define LCD_RW RC6
#define LCD_EN RC7
#define DH RC4 //定义DHT11数据端口
#define DH_HIGH() TRISC4=1 //设置数据口为输入
#define DH_LOW() TRISC4=0;DH = 0 //设置数据口为输出
uchar RH_data_H_temp,RH_data_L_temp,T_data_H_temp,T_data_L_temp,checkdata_temp;
uchar RH_data_H,RH_data_L,T_data_H,T_data_L,checkdata;
uchar flag,temp,comdata;
void delayus(uint x,uchar y)
{ uint i; uchar j;
for(i=x;i>0;i--);
for(j=y;j>0;j--);
}
void COM()
{ uchar i;
for(i=0;i<8;i++)
{ flag=2;
while((!(DH))&&flag++);//等待高电平
if(flag==1)break;
delayus(0,2);
delayus(0,2);
delayus(0,2);
//delayus(0,2);
temp=0;
if(DH)temp=1;
flag=2;
while((DH)&&flag++); //等待低电平
//超时则跳出for循环
if(flag==1)break;
//判断数据位是0还是1 // 如果高电平高过预定0高电平值则数据位为 1
comdata<<=1;
comdata|=temp; //0
}
}
//--------------------------------
//-----湿度读取子程序 ------------
//--------------------------------
//----以下变量均为全局变量--------
//----温度高8位== U8T_data_H------
//----温度低8位== U8T_data_L------
//----湿度高8位== U8RH_data_H-----
//----湿度低8位== U8RH_data_L-----
//----校验 8位 == U8checkdata-----
//--------------------------------
void RH(void)
{ //主机拉低18ms
DH_LOW();
delayus(5000,200);
DH_HIGH(); //总线由上拉电阻拉高 主机延时20-40us
delayus(0,2);
delayus(0,2);
delayus(0,2);
delayus(0,2);
//主机设为输入 判断从机响应信号
DH_HIGH();
//判断从机是否有低电平响应信号 如不响应则跳出,响应则向下运行
if(!DH)
{ flag=2;
//判断从机是否发出 80us 的低电平响应信号是否结束
while((!(DH))&&flag++);
flag=2;
//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
while((DH)&&flag++);
//数据接收状态
COM();
RH_data_H_temp=comdata;
COM();
RH_data_L_temp=comdata;
COM();
T_data_H_temp=comdata;
COM();
T_data_L_temp=comdata;
COM();
checkdata_temp=comdata;
DH_HIGH();
//数据校验
temp=(T_data_H_temp+T_data_L_temp+RH_data_H_temp+RH_data_L_temp);
if(temp==checkdata_temp)
{ RH_data_H=RH_data_H_temp;
RH_data_L=RH_data_L_temp;
T_data_H=T_data_H_temp;
T_dataL=T_data_L_temp;
checkdata=checkdata_temp;
}
} //fi
}
void Delayms(uint ms)
{ uchar i;
while(ms--)
{ for(i=0;i<120;i++);
}
}
--------------下面为液晶模块程序--------
uchar LCD_Busy_Check()
{ uchar result;
TRISB=0xFF;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
Delayms(1);
result=PORTB;
LCD_EN=0;
return result;
}
void Write_LCD_Command(uchar cmd)
{ while((LCD_Busy_Check()&0x80)==0x80);
TRISB=0x00;
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
PORTB=cmd;
LCD_EN = 1;
Delayms(1);
LCD_EN = 0;
}
void Write_LCD_Data(uchar dat)
{ while((LCD_Busy_Check()&0x80)==0x80);
TRISB=0x00;
LCD_RS" = 1;
LCD_RW = 0;
LCD_EN = 0;
PORTB = dat;
LCD_EN = 1;
Delayms(1);
LCD_EN = 0;
}
void LCD_Initialise()
{ Write_LCD_Command(0x01);
Delayms(1);
Write_LCD_Command(0x38);
Delayms(1);
Write_LCD_Command(0x0c);
Delayms(1);
Write_LCD_Command(0x06);
Delayms(1);
}
void Set_LCD_POS(uchar pos)
{
Write_LCD_Command(pos|0x80);
}
void init()
{ ADCON1=0x07;
TRISA=0x00;
TRISC=0x00;
}
void disp()
{ Set_LCD_POS(0x00);
Write_LCD_Data(RH_data_H/10+0x30);
Set_LCD_POS(0x01);
Write_LCD_Data(RH_data_H%10+0x30);
Set_LCD_POS(0x02);
Write_LCD_Data('%');
Set_LCD_POS(0x07);
Write_LCD_Data(T_data_H/10+0x30);
Set_LCD_POS(0x08);
Write_LCD_Data(T_data_H%10+0x30);
Set_LCD_POS(0x09);
Write_LCD_Data(0xdf);
Set_LCD_POS(0x0A);
Write_LCD_Data(0x43);
}
void main()
{ init();
LCD_Initialise();
DH_HIGH();
while(1)
{ RH();
disp();
}
}
DHT11说明书081206.pdf
(677.52 KB)
|