找回密码
 立即注册

QQ登录

只需一步,快速开始

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

仿真时LCD一直显示DHT11采集的数据为0,求大神解决一下

[复制链接]
跳转到指定楼层
楼主
请问是因为没采集到数据吗,还是时序问题呀  纯小白QAQ

DHT11代码:
#include"reg52.h"
#include"DHT11.h"
#include"delay.h"
#include <intrins.h>
sbit  DHT11_DQ=P3^7;

bit status;  

void delay_10us(unsigned int nus)
{
        unsigned char i;

        i = 2*nus;
        while (--i);
}

void DHT11_Rst(void)  
{
        DHT11_DQ=0;      
        delay_ms(20);   
        DHT11_DQ=1;      
        delay_10us(4);  
}

bit DHT11_Check(void)
{
        unsigned char retry=0;

        while(DHT11_DQ&&retry<100)
        {
                retry++;
                delay_10us(1);
        };
        if(retry>=100)return 1;   
        else retry=0;              
        while(!DHT11_DQ&&retry<100)
        {
                retry++;
                delay_10us(1);
        };
        if(retry>=100)return 1;
                 return 0;
        
}

bit DHT11_Init(void)  
{
        DHT11_Rst() ;         
        return DHT11_Check();
}

static unsigned char DHT11_Read_Byte(void)
{
        unsigned char retry, dat = 0, i;
        status = OK;  
        for(i = 8; i > 0; i--)
        {
        
                dat <<= 1;
                retry = 0;
        
                whileT11_DQ == 0 && retry++ < NUMBER);
                if(retry >= NUMBER)
                {
                        status = ERROR;  
                        return 0;   
                }

                delay_10us(2);
        
                if(DHT11_DQ != 0)
                {
               
                        dat++;
               
                        while(DHT11_DQ != 0 && retry++ < NUMBER)
                        {
                                DHT11_DQ = 1;
                        }
                        if(retry >= NUMBER)
                        {
                                status = ERROR;  
                                return 0;
                        }  
                }
        }  
        return (dat);

bit DHT11_Read_Date(unsigned char *temp,unsigned char *humi)
{  
        unsigned char i = 0,retry = 0;
        unsigned char buf[SIZE];
        DHT11_DQ = 0;      
        delay_ms(30);
        DHT11_DQ = 1;   

        delay_10us(3);
        if(DHT11_DQ != 0)  
        {
                //??ó|′eD?o?  
                return ERROR;
        }
        else
        {
        
                while(DHT11_DQ == 0 && retry++ < NUMBER);  
                if(retry >= NUMBER)
                {
                        DHT11_DQ = 1;
                        return ERROR;  
                }
                retry = 0;
                DHT11_DQ = 1;        
        
                while(DHT11_DQ != 0 && retry++ < NUMBER);
                if(retry >= NUMBER)
                {
                        DHT11_DQ = 1;
                        return ERROR;
                }
        

            for(i = 0; i < SIZE; i++)  
                {
                        buf[ i] = DHT11_Read_Byte();
                        if(status == ERROR)        
                        {
                                DHT11_DQ = 1;   
                                return ERROR;  
                        }  
                }
                if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
                {
                        *humi = buf[0];
                        *temp = buf[2];
                        DHT11_DQ = 1;
                        return OK;                                                   
                }
                else                        
                {
                        
                        return ERROR;
                }
        }         
}



主函数
void main()
{
        unsigned char temperature;              
        unsigned char humidity;
        Time0_Time1_init();
        lcd1602_init();
        led= 1;
        delay_ms(1000);
        L1602_string(1,1,"WD:");
        L1602_string(1,8,"SD:");
        L1602_string(1,6,"C");
        L1602_string(1,13,"%");
        L1602_string(2,3,":");
        L1602_string(2,6,":");
        L1602_string(2,12,":");
        while(1)
        {
                DHT11_Read_Date(&temperature,&humidity);
                delay_ms(50);
               
                L1602_int(1,4,temperature);
                L1602_int(1,11,humidity);
               
                if(key1==0
                {
                        buf++;
                        if(buf>1) buf=0;
                }
               
                if(buf==0
                {
                        L1602_string(1,15,"SD");
                        if(key2==0) led=~led;
                        if(key3==0) q1=~q1;
                        if(key4==0) q2=~q2;
                        if(temperature>40) q2=1;
                }
               
               
                if(buf==1
                {
                        L1602_string(1,15,"ZD");
                        if(key4==0) miao++;
                        if(key5==0) fen++;
                        if(key6==0) shi++;
                        if(key3==0) shi1++;  if(shi1>23) shi1=0;
                        if(key2==0) fen1++;  if(fen1>59) fen1=0;
                        
                        if((shi==shi1)&(fen==fen1)
                        {
                                if(humidity>70) led=q1=q2=0;  
                        
                        }
                        if(temperature>40|humidity<35
                        led=q1=q2=1;        
                }
                L1602_int(2,1,shi);
                L1602_int(2,4,fen);
                L1602_int(2,7,miao);
               
                L1602_int(2,10,shi1);
                L1602_int(2,13,fen1);
               
                delay_ms(150);
        }        
}

void Timer0() interrupt 1
{
   TH0=(65536-50000)/256;
   TL0=(65536-50000)%256;
   count++;
   if(count==20
        {
    count=0
                miao++;
                if(miao>59)
                {
                        miao=0;
                        fen++;
                }
                if(fen>59)
                {
                        fen=0;
                        shi++;
                }
                if(shi>23) shi=0;
  }
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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