基于51的DHT11驱动

1.传感器性能说明

   测湿范围:20%90%  测温范围:050C  测温精度:±2C 测湿精度: ±5%RH

2.接口说明

   长度大于20m时采用5K上拉电阻

3.电源引脚

    电源供电电压:35.5V ,电源引脚(VCCGND)可增加一个100nF的电容用以去耦滤波

4.串行接口

    单线双向,采取但总线方式,一次通信时间为4ms,数据格式如下:

湿度整数部分+湿度小数部分+温度整数部分+温度小数部分+温湿度校验和

5.数据读取时序分析

         图一  单总线读取时序(完整过程)

  图2     单总线读取时序(初始部分)

     图三       数据表示方法

     图四     数据表示方法

总线空闲状态为高电平,总线把电平拉低等待DHT11响应,拉低时间必须大于18msDHT11接到主机起始信号后,等待主机信号结束,然后发送80us电平表示响应。主机发送起始信号后延时30-40us,读取响应信号。(主机发送起始信号后可切换为输入模式,高电平亦可)

7.基于51驱动的源码分析

/*
*  基于51单片机的DHT11驱动程序
*  其中假定51单片机的时钟频率为12M,并且单指令单周期,可得每个机器周期的时间为1/12M  × 12 = 1us
*  
*/

#include "dht11.h"
#include "reg52.h"

#define size  5             //设置数组的大小
#define  NUMBLE_50us   25  //设置延时的计数值
#define  NUMBLE_40us   20
#define  NUMBLE_80us   40

#define ERROR -1
#define OK    0

sbit DHT11 = P1^0;     //DHT11的单总线
unsigned int status;   //DHT11的数据读取状态
unsigned char humi_value,temp_value; //温湿度值
unsigned char arry_value[size];    //定义存取数组

static void delay_ms(int x)    //ms延时函数
{
    unsigned int i,j;
   for(i = 100*x;i > 0 ;i--)
   {
       delay_10us();
   }
   return ;
}



static void delay_10us()    //10us延时函数
{
{
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    return ;
}

static unsigned char  DHT11_readvalue()
{
    char i;
    unsigned char value = 0,count;
    for(i = 0; i < 8;i ++)
    {
        value<<=1;
        count = 0;
        status = OK;   
        while(DHT11 == 0 && count ++ < NUMBLE_50us);  //读取每一数据位初始低电平为50us
        if(count >= NUMBE_50us)
        {
            status = ERROR;
            return 0;
        }
        delay_10us;
        delay_10us;
        delay_10us;
        if(DHT11 != 0)
        {
            value ++;
            count = 0;
            while(DHT11 != 0 && count++ < NUMBLE_40us); //读取一数据位为1,高电平为(40 + 30)us
            if(count >= NUMBLE_40us)
           {
                status = ERROR;      //读取数据位失败
                return 0;
            }
        }
    }
    return value;
}
static int  DHT11_Readhum_temp(void)
{
    unsigned char count = 0;
    unsigned char i;
    unsigned int checkvalue = 0;
    
    DHT11 = 1;
    DHT11 = 0; 
    delayms(20);    //主机拉低18ms设置起始信号     
    DHT11 = 1;
    delay_10us;     //主机拉高并延时等待
    delay_10us;
    delay_10us;
    delay_10us;  
    if(DHT11 != 0)
    {
        return ERROR;
    }
    else
    {
        while(DHT11 == 0 && count ++ < NUMBLE_80us);   //从机拉高80us响应
        if(count >= NUMBLE_80us)
        {
            return ERROR;
        }
        DHT11 = 1;
        count = 0;
        while(DHT11 !=0 && count ++ < NUMBLE_80us);   //从机拉高延时准备输出
        if(count >= NUMBLE_80us)
        {
            return ERROR;
        }
        
        for(i = 0; i < size -1 ; i ++)      //读取温湿度值
        {
            arry_value[i] = DHT11_readvalue();
            if(i != size -1)
            {
             checkvalue + = arry_value[i];  //计算校验和
            }
        }
        if(arry_value[size -1] != checkvalue)    //比较读取结果
        {
            return ERROR;
        }
        else
        {
            humi_value = arry_value[0]*10 + arry_value[1];  //读取湿度值
            temp_value = arry_value[2]*10 + arry_value[3];  //读取温度值
        }
    }
    return  0;
}


 

  • 1
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值