微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

可能无法读取 dht11 数据

如何解决可能无法读取 dht11 数据

我正在为 ATmega32 编写 DHT11 接口,但由于某种原因,在这部分的某个 while 循环中收到响应后,它保持读取逻辑高

if(dio_GetPinValue(DHT11_PORT,DHT11_PIN)==1)
            {
                SET_BIT(ReceivedData[Loc_u8ByteCount],Loc_u8ReceivedBitCount);  /* then its logic HIGH */
                /*wait until low again for next bit */

                while(dio_GetPinValue(DHT11_PORT,DHT11_PIN)); //IT GET STUCK HERE 

            }

为什么会这样? dio 驱动程序和 LCD 库已检查并正常工作,因此没有问题 而且我还没有写校验和函数 我还检查了硬件并对 Arduino UNO DHT11 库进行了测试,它运行良好所以问题出在代码上。这是完整的代码

u8* pu8InitAndReceiveData(void)
{
/* 0-Decale Some variables*/
    u8 ReceivedData[5]={0};



/* 1-Start THE DHT11 From Low-power-consumption Mode to Running Mode*/
    /*Make the Pin OUTPUT to send start signal*/

    dio_SetPinDirection(DHT11_PORT,DHT11_PIN,OUTPUT);
    /*send high to make the DHT11 Ready to receive LOW (Start Signal starts with LOW)*/

    dio_SetPinValue(DHT11_PORT,HIGH);
    _delay_ms(10);

    /*Send Start Signal*/

    dio_SetPinValue(DHT11_PORT,LOW);
    _delay_ms(18); // Waiting time
    dio_SetPinValue(DHT11_PORT,HIGH); //after about 20-40 us the DHT11 Should response
    _delay_us(35); //wait until the DHT11 detects the start signal and starts to responce

/* 2-receive response from DHT11 to make sure that it is converted into Running-mode */
    /*Make the Pin INPUT to receive response signal*/

    dio_SetPinDirection(DHT11_PORT,INPUT);
    while(dio_GetPinValue(DHT11_PORT,DHT11_PIN)==1); //make sure DHT11 send LOW
    while(dio_GetPinValue(DHT11_PORT,DHT11_PIN)==0); //DHT11 will send low for about 80us
    while(dio_GetPinValue(DHT11_PORT,DHT11_PIN)==1); //then  will send high for about 80us


/* 3-receive 40bit Data From the DHT11*/

    for (u8 Loc_u8ByteCount=0; Loc_u8ByteCount<5; Loc_u8ByteCount++)
    {
        for (u8 Loc_u8ReceivedBitCount=7; Loc_u8ReceivedBitCount>=0; Loc_u8ReceivedBitCount--)
        {
            /*Each bit Starts with 50us LOW*/

            while(dio_GetPinValue(DHT11_PORT,DHT11_PIN)==0);
            /*if Zero bit: it will be high for 26-28us,else if One Bit: it will be high for 40us*/

            _delay_us(40);

            if(dio_GetPinValue(DHT11_PORT,DHT11_PIN)); //IT GET STUCK HERE 

            }
            else
            {
                CLR_BIT(ReceivedData[Loc_u8ByteCount],Loc_u8ReceivedBitCount);
            }

        } //END Inner for
    } //END Outer For

/* 4-Back To default for the next readings*/
    /*Make the Pin OUTPUT*/
    dio_SetPinDirection(DHT11_PORT,OUTPUT);
    /*send high until the next Reading operation*/
    dio_SetPinValue(DHT11_PORT,HIGH);






return ReceivedData;

} //END Func








void DHT11_GetValues(void)
{
    u8 * pu8OinterToReceivedArray=0;
    pu8OinterToReceivedArray = pu8InitAndReceiveData();

        Integer_Humidty=pu8OinterToReceivedArray[0] ;   /* store first eight bit as Integer humidity*/
        Decimal_Humidty=pu8OinterToReceivedArray[1] ;   /* store next eight bit as Decimal humidity */
        Integer_Temp=pu8OinterToReceivedArray[2] ;  /* store next eight bit in Integer Temperature*/
        Decimal_Temp=pu8OinterToReceivedArray[3] ;  /* store next eight bit in Decimal Temperature */
        Check_Sum=pu8OinterToReceivedArray[4] ;/* store next eight bit in CheckSum */

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。