找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 650|回复: 4
打印 上一主题 下一主题
收起左侧

为什么我的DHT11一直读出来是0

[复制链接]
跳转到指定楼层
楼主
ID:1082695 发表于 2023-6-7 23:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void DHT11_int(void)
{        

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //使能GPIOE时钟
        
                DHT11_output() ;


}
void DHT11_input(void)
{
                GPIO_InitTypeDef  GPIO_InitStructure;
        
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA0 通道0
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//模拟输入
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;//不带上下拉
    GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化  

}
void DHT11_output(void)
{
                GPIO_InitTypeDef  GPIO_InitStructure;
        
                GPIO_SetBits(GPIOE, GPIO_Pin_6);

              GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//PA0 通道0
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//模拟输入
                GPIO_InitStructure.GPIO_OType=GPIO_OType_OD;
                GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
            GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;//不带上下拉
           GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化  
        
}
uint8_t DHT11_read_bitdat(void)
{
        uint8_t i;  
        uint8_t dat;
        for(i=0;i<8;i++)
        {
                        dat<<=1;
                        while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6))
                                
                        while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6)==0)
                                
                        if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6))
                        {
                                delay_us(30);               
                        }
                        if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6))
                                dat|=1;
        }
                return dat;
}

uint8_t DHT11_read(uint8_t *humi,uint8_t *temp)
{
                        int retry=0;
                        uint8_t i;
                        uint8_t buf[5];
                        GPIO_ResetBits(GPIOE, GPIO_Pin_6);
                        delay_ms(30);
        
                        GPIO_SetBits(GPIOE, GPIO_Pin_6);
                        delay_us(40);
        
                        DHT11_input();
                        while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6))
                {
                                delay_us(80);
                                retry++;
                                if(retry>10)
                                {
                                        GPIO_SetBits(GPIOC,GPIO_Pin_13);
                                                return 1;
                                }        
                }
                        retry=0;
                        while(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6)==0)
                {
                        delay_us(80);
                        retry++;
                        if(retry>10)
                         {
                        GPIO_SetBits(GPIOC,GPIO_Pin_13);
                        return 1;
                         }
                }
                for(i=0;i<5;i++)
                {
                        buf[ i]=DHT11_read_bitdat();
                }        
               
                DHT11_output();
               
                if((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
                {        
                         *humi = buf[0];   
                         *temp = buf[2];     
                        
                                return 0;
               
                }               
        
                return 1;
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:123289 发表于 2023-6-8 08:17 | 只看该作者
看IC手册,再写程序。
回复

使用道具 举报

板凳
ID:584814 发表于 2023-6-12 14:43 | 只看该作者
你认为呢 ?
首先提出自己的观点供讨论,而不是责问大家。
回复

使用道具 举报

地板
ID:1083588 发表于 2023-6-12 17:17 | 只看该作者
有可能地址错了
回复

使用道具 举报

5#
ID:403121 发表于 2023-6-12 19:46 | 只看该作者
代码不全啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表