DTH11温湿度传感器学习笔记

   DTH11基本参数       

工作电压范围:3.5V-5.5V
工作电流 :平均 0.5mA
湿度测量范围:20-90%RH
温度测量范围:0-50℃
湿度分辨率 :1%RH 8 位
温度分辨率 :1℃ 8 位
采样周期 :1S
单总线结构
与 TTL 兼容(5V)

引脚定义                

VCC                电源正极

GND                接地

Dout               输出口,接单片机IO口

NC                空脚       

数据传输

DTH11采用单总线传输数据,一次传40Bit数据,也就是5个Byte数据。数据分为小数和整数(由于DTH11的原因,后面读书时可以不用管小数部分),具体的数据格式如下。

一次性传40bit,高位在前,

                                        8bit湿度整数数据+8bit湿度小数数据+

                                        8bit温度整数数据+8bit温度小数数据

                                        8bit校验和

校验和是前4个byte的和:  8bit温度整数数据+8bit温度小数数据+8bit湿度整数数据+8bit湿度小数数据=8bit校验和。

//DTH11.c文件


#include <REGX52.H>
#include "intrins.h"

sbit DTH_IO = P1^2;

/**
  * @brief 延迟函数 1ms
  * @param ms 延迟多少ms
  * @retval 无
  */
void DTH_Delay1ms(unsigned int ms)		//@11.0592MHz
{
	unsigned char data i, j;

    while(ms)
	{
        _nop_();
        i = 2;
        j = 199;
        do
        {
            while (--j);
        } while (--i);
        ms--;
    }
}


/**
  * @brief 延迟函数 10us
  * @param ms 延迟多少us
  * @retval 无
  */
void DTH_Delay10us(unsigned int us)		//@11.0592MHz
{
	unsigned char data i;
    while(us)
	{
        i = 2;
        while (--i);
        us--;
    }
}


/**
  * @brief DTH11初始化
  * @param 无
  * @retval Ack 应答信号 0为应答 1为没有应答
  */
unsigned char DTH_Init()
{
    unsigned char data i, j;
    unsigned char Ack = 1;  
    
    DTH_IO = 1;
    DTH_IO = 0;                     //主机拉低至少18ms
    DTH_Delay1ms(30);               
    DTH_IO = 1;
    DTH_Delay10us(5);
    
    if(DTH_IO == 0)                 //检测应答信号
    {
        
        while(!DTH_IO);
        while(DTH_IO);
        Ack = 0;
    }
        
    return Ack;
}


/**
  * @brief DTH11读取1Byte数据
  * @param 无
  * @retval Byte 读取的数据
  */
unsigned char DTH_ReadByte()
{
    unsigned char i = 0;
    unsigned char Byte = 0x00;
    
    for(i=0;i<8;i++)
    {
        while(!DTH_IO);                 
        DTH_Delay10us(3);
        if(DTH_IO==1)                   //检测读取1还是0
        {
            while(DTH_IO);
            Byte |= (0x80>>i);
        }        
    }
    
    return Byte;
}


/**
  * @brief DTH11读取5byte
  * @param Com 选择读取的数据 1读取湿度 2读取温度
  * @retval RH 读取湿度的值 (20~90%RH)
  * @retval TH 读取温度的值 (0~50)
  */
unsigned char DTH_Read(unsigned char Com)
{
    unsigned char RH,RL,TH,TL,Revise;
    
    DTH_Init();
    RH = DTH_ReadByte();                //读取湿度的整数数据
    RL = DTH_ReadByte();                //读取湿度的小数数据
    TH = DTH_ReadByte();                //读取温度的整数数据
    TL = DTH_ReadByte();                //读取温度的小数数据
    Revise = DTH_ReadByte();            //读取校验数据
    if(RH+RL+TH+TL == Revise)
    {
        if(Com == 1)
        {
            return RH;
        }
        
        if(Com == 2)
        {
            return TH;
        }
    } 
}

//DTH11.h文件

#ifndef __DTH11_H__
#define __DTH11_H__

#define RH  1        //读取湿度
#define TH  2        //读取温度

unsigned char DTH_Init();
void DTH_Delay1ms(unsigned int ms);
unsigned char DTH_Read(unsigned char Com);

#endif

 

 

                       

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值