调试DHT11程序两天了 仍然觉得不对劲......代码如下
DHT11.C
#include
#include
#include "DHT11.H"
unsigned char DHT11_init(void){
unsigned char i=16;
DHT11_IO_OUT;
DHT11_IO_SET;
_delay_ms(1);
DHT11_IO_CLR;
_delay_ms(20); //主机拉低IO,并至少保持18ms
DHT11_IO_SET;
_delay_us(10); // 主机拉高IO 20到40us;其后 器件响应 40-50us电平
DHT11_IO_IN;
//DHT11_IO_SET; //带有上拉 暂时注释,因为电路中已有4.7K上接电阻,以免上拉太强
while(i--){ //每5us采样一次 若采到0 说明器件响应正常,若16次以后仍未采到 说明100us时间已过 器件仍未应答 有问题
_delay_us(5);
if (DHT11_IO_READ_L){
while (DHT11_IO_READ_L){asm("nop");} //本人程序中 读到响应0即完成器件存在的判断,故器件响应的0有可能尚未结束,nop若干进行渡过
while (DHT11_IO_READ_H){asm("nop");} //器件响应0后 会拉高IO 40-50us 后以低电平开始发数据,故渡过
return 1;
}
}
return 0;
//器件应答正常则返回1 否则返回0 符合日常习惯
}
unsigned char DHT11_READ_BYTE(void){
unsigned char i,dat=0; //DHT11_INIT时 已将口线设为输入
for (i=0;i<8;i++){
while (DHT11_IO_READ_L){asm("nop");} //渡过每个数据开始位时的低电平12-14us 其后,器件将拉高数据线,发0时 保持26--28us 发1时 保持116-118us
//故在渡过起始0电平后的50us时进行采样 若为高 则是1,若为低,实际是下一位的起始0电平,仍能说明当前所发的是0
_delay_us(50);
if (DHT11_IO_READ_H)
{
dat |= 0x01; //手册写明,数据格式是高位先出
while (DHT11_IO_READ_H)
{
asm("nop"); //因发1时,高电平较长,nop渡过
}
}
dat <<= 1; //读到数据,左移1位,预备接收下一位
}
return dat;
}
主程序片断
while(1)
{
_delay_ms(255);
_delay_ms(255);
_delay_ms(255);
_delay_ms(255);
_delay_ms(255);
_delay_ms(255);
_delay_ms(255);
LCD12864_WRITE_BYTE_S(0,0x01);
DHT11_init();
RH[0] = DHT11_READ_BYTE();
RH[1] = DHT11_READ_BYTE();
RH[2] = DHT11_READ_BYTE();
RH[3] = DHT11_READ_BYTE();
RH[4] = DHT11_READ_BYTE();
LCD12864_WRITE_BYTE_S(0,0x88);
LCD12864_WRITE_BYTE_S(1,RH[0]/100+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[0]%100)/10+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[0]%100)%10+0x30);
LCD12864_WRITE_BYTE_S(1,' ');
LCD12864_WRITE_BYTE_S(1,RH[1]/100+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[1]%100)/10+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[1]%100)%10+0x30);
LCD12864_WRITE_BYTE_S(0,0x90);
LCD12864_WRITE_BYTE_S(1,RH[2]/100+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[2]%100)/10+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[2]%100)%10+0x30);
LCD12864_WRITE_BYTE_S(1,' ');
LCD12864_WRITE_BYTE_S(1,RH[3]/100+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[3]%100)/10+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[3]%100)%10+0x30);
LCD12864_WRITE_BYTE_S(0,0x98);
LCD12864_WRITE_BYTE_S(1,RH[4]/100+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[4]%100)/10+0x30);
LCD12864_WRITE_BYTE_S(1,(RH[4]%100)%10+0x30);
if ((RH[0]+RH[1]+RH[2]+RH[3])==RH[4]){
LCD12864_WRITE_STR_S("check OK");
}
else
LCD12864_WRITE_STR_S("Error");
程序运行结果如图
现在问题是 温度数据明显太高了 ,北方冬天室内 怎么就有40多度
请各位指点一下,谢谢
[ 本帖最后由 whg0109 于 2013-12-17 12:08 编辑 ]