DHT11温湿度模块的简单使用与代码-STM32F103C8T6

目录

产品概述

模块接线

测量范围

模块代码

DTH11.h

DHT11.c

main.c


主控芯片: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);
	}
}

详细信息需要阅读对应手册。

DHT11-V1_3说明书(详细版).pdf (aosong.com)icon-default.png?t=N7T8http://www.aosong.com/userfiles/files/media/DHT11-V1_3%E8%AF%B4%E6%98%8E%E4%B9%A6%EF%BC%88%E8%AF%A6%E7%BB%86%E7%89%88%EF%BC%89.pdf

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是基于STM32F103C8T6使用DHT11温湿度模块检测的步骤: 1. 硬件连接 将DHT11模块的VCC引脚连接到STM32F103C8T6的5V引脚,GND引脚连接到STM32F103C8T6的GND引脚,DATA引脚连接到STM32F103C8T6的PB0引脚。 2. 软件开发 在Keil MDK中,新建一个工程,然后按照以下步骤进行配置: (1)在工程中添加头文件dht11.h和dht11.c。 (2)在main函数中初始化PB0引脚为输入模式。 (3)在main函数中循环调用dht11_read函数,读取DHT11模块的温湿度值,并通过串口输出。 以下是示例代码: ```c #include "stm32f10x.h" #include "dht11.h" #include "stdio.h" int main(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; // 初始化PB0引脚为输入模式 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); // 初始化USART1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); while (1) { // 读取温湿度值 DHT11_Data_TypeDef dht11_data = dht11_read(); // 输出温湿度值 char str[128]; sprintf(str, "Temperature: %d.%d C, Humidity: %d.%d %%\r\n", dht11_data.temperature_integral, dht11_data.temperature_decimal, dht11_data.humidity_integral, dht11_data.humidity_decimal); USART_SendString(USART1, str); Delay_ms(2000); } } // 串口发送字符串 void USART_SendString(USART_TypeDef* USARTx, char* str) { while(*str) { USART_SendData(USARTx, (uint8_t)(*str++)); while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET); } } // 延时函数 void Delay_ms(uint32_t nTime) { uint32_t TimingDelay = 0; TimingDelay = nTime; while(TimingDelay != 0) { TimingDelay--; } } // SysTick中断处理函数 void SysTick_Handler(void) { static uint32_t counter = 0; if(counter != 0) { counter--; } } // 获取系统时钟 uint32_t GetSysClock(void) { RCC_ClocksTypeDef RCC_ClocksStatus; RCC_GetClocksFreq(&RCC_ClocksStatus); return RCC_ClocksStatus.HCLK_Frequency; } ``` 3. 编译下载 在Keil MDK中编译工程,然后下载到STM32F103C8T6开发板中进行测试。 注意:由于DHT11模块的数据线是单向的,需要使用上拉电阻,所以在初始化PB0引脚时,需要将它设置为上拉输入模式(GPIO_Mode_IPU)。而且DHT11模块的温湿度值读取需要一定的时间,建议采用定时器或者延时函数等方式进行等待。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值