目录
主控芯片:STM32F103C8T6
产品概述
DHT11数字温湿度传感器是一款含有已校准数字信号输出的温湿度复合传感器。它应用专用的数 字模块采集技术和温湿度传感技术,确保产品具有枀高的可靠性与卓越的长期稳定性。传感器包括一 个电容式感湿元件和一个NTC 测温元件,并与一个高性能8位单片机相连接。
模块接线
1.典型应用电路中建议连接线长度短于5m时用4.7K上拉电阻,大于5m时根据实际情况降低上拉电 阻的阻值。
2. 使用3.3V电压供电时连接线尽量短,接线过长会导致传感器供电不足,造成测量偏差。
3. 每次读出的温湿度数值是上一次测量的结果,欲获取实时数据,需连续读取2次,但不建议连续多次 读取传感器,每次读取传感器间隔大于2秒即可获得准确的数据。
4. 电源部分如有波动,会影响到温度。如使用开关电源纹波过大,温度会出现跳动。
测量范围
相对湿度:5%~95%RH
温度:-20~60℃
模块代码
DTH11.h
#ifndef _DHT11_H_
#define _DHT11_H_
#include "stm32f10x.h" // Device header
//上电后等待1秒才调用函数
char DHT11_GetData(uint8_t *Humi,uint8_t* Temp);
char DHT11_GetRealData(uint8_t *Humi,uint8_t* Temp);//实时温湿度
#endif
DHT11.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "DHT11.h"
#define DHT11_IO GPIOB
#define DHT11_Pin GPIO_Pin_12
#define DHT11_RCC RCC_APB2Periph_GPIOB
void DHT11_MOSI_Init(void)
{
RCC_APB2PeriphClockCmd(DHT11_RCC,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin=DHT11_Pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(DHT11_IO,&GPIO_InitStruct);
GPIO_SetBits(DHT11_IO,DHT11_Pin);
}
void DHT11_MISO_Init(void)
{
RCC_APB2PeriphClockCmd(DHT11_RCC,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin=DHT11_Pin;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(DHT11_IO,&GPIO_InitStruct);
}
void DHT11_Start(void)
{
DHT11_MOSI_Init();
GPIO_ResetBits(DHT11_IO,DHT11_Pin);
Delay_ms(25);
GPIO_SetBits(DHT11_IO,DHT11_Pin);
Delay_us(13);
DHT11_MISO_Init();
}
uint8_t DHT11_ReceiveByte(void)
{
uint8_t Byte=0x00;
for(int i=0;i<8;i++)
{
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==0);
Delay_us(40);
if(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==1)
{
Byte|=(0x80>>i);
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==1);
}
}
return Byte;
}
//该函数每次读出的温湿度数值是上一次读取测量的结果
char DHT11_GetData(uint8_t *Humi,uint8_t* Temp)
{
char Mark='+';
uint8_t Humi_H,Humi_L,Temp_H,Temp_L,Check;
DHT11_Start();
if(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==0)
{
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==0);
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==1);
Humi_H=DHT11_ReceiveByte();
Humi_L=DHT11_ReceiveByte();//等于0
Temp_H=DHT11_ReceiveByte();
Temp_L=DHT11_ReceiveByte();//温度低位Bit8为1则表示负温度,否则为正温度,后7位为小数部分
Check=DHT11_ReceiveByte();
if(Humi_H+Humi_L+Temp_H+Temp_L==Check)
{
*Humi=Humi_H;
*Temp=Temp_H;//小数部分不做处理
if((Temp_L&0x80)==0x80)
{
Mark='-';
}
}
while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_Pin)==0);
GPIO_SetBits(DHT11_IO,DHT11_Pin);
}
return Mark;
}
//获取实时温湿度
//连续获取两次数据,DHT11模块会在上一次结束信号时重测温湿度数据
char DHT11_GetRealData(uint8_t *Humi,uint8_t* Temp)
{
char Mark='+';
DHT11_GetData(Humi,Temp);
Delay_ms(1000);
Delay_ms(1000);
Delay_ms(100); //读取传感器数据大于2s
Mark=DHT11_GetData(Humi,Temp);
return Mark;
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "DHT11.H"
uint8_t Humi,Temp;
int main(void)
{
OLED_Init();
OLED_ShowString(1, 1, "Humi:");
OLED_ShowString(2, 1, "Temp:");
Delay_ms(1000);
while (1)
{
DHT11_GetData(&Humi,&Temp);
DHT11_GetRealData(&Humi,&Temp);
OLED_ShowNum(1,6,Humi,2);
OLED_ShowNum(2,6,Temp,2);
}
}
详细信息需要阅读对应手册。