本帖最后由 Ganger929 于 2009-8-18 17:42 编辑
按DHT11的时序图读,发现读出的数据始终是 42 170 170 170,即00101010 10101010 10101010 10101010,这是怎么回事呢
程序如下:
INT8U DHT11Reset(void)
{
INT8U presence;
INT16U U8FLAG;
DHT11_OUT();
DHT11_SET();
//_delay_us(6);
DHT11_CLR();
_delay_ms(20);
DHT11_IN();
DHT11_SET();
asm("nop");
while(((DHT11_READ()))&&(U8FLAG++));
if(U8FLAG==1) presence = 1;
else presence = 0;
// _delay_us(30);
// DHT11_IN();
// presence = DHT11_READ();
return presence;
}
/*读数据程序*/
INT8U DHT11Read(void)
{
INT8U U8comdata,i,U8temp;
INT16U U8FLAG;
for(i=0;i<8;i++)
{
DHT11_IN();
DHT11_CLR();
asm("nop");
U8FLAG=2;
while(((DHT11_READ()==0))&&(U8FLAG++));
U8temp=0;
_delay_us(30);
if(DHT11_READ()){U8temp=1;}
//if(DHT11_READ()){U8comdata|=0x01;}
//else{U8comdata&=0xFE;}
U8FLAG=2;
while((DHT11_READ())&&(U8FLAG++));
//超时则跳出for循环
if(U8FLAG==1)break;
//判断数据位是0还是1
// 如果高电平高过预定0高电平值则数据位为 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
return U8comdata;
}
INT16U DHT11GetHum(void)
{
INT16_TO_INT8 value, v2;
INT16U U8FLAG;
cli();
if(DHT11Reset())
{
sei();
return(0xFFFF);
}
U8FLAG=2;
//判断从机是否发出 80us 的低电平响应信号是否结束
while((DHT11_READ()==0)&&(U8FLAG++));
U8FLAG=2;
//判断从机是否发出 80us 的高电平,如发出则进入数据接收状态
while((DHT11_READ())&&(U8FLAG++));
value.i8[1]=DHT11Read();
value.i8[0]=DHT11Read();
/*
v2.i8[1]=DHT11Read();
v2.i8[0]=DHT11Read();
U8FLAG = DHT11Read();
printf("%d %d %d %d %d\n", value.i8[1], value.i8[0], v2.i8[1], v2.i8[0], U8FLAG);
*/
sei();
return value.i16;
} |