STM32F407 study notes-DHT11 module (temperature and humidity sensor)

STM32F407 study notes-DHT11 module (temperature and humidity sensor)

1. Basic principle:
Control the DHT11 sensor and read data by controlling the timing of the DHT11 module. The idle state of the bus is high. The microcontroller pulls the bus low and waits for DHT11 to respond. The pull-down time is greater than 18ms to ensure that DHT11 can detect it. Start signal. When DHT11 receives the start signal of the microcontroller, it waits for the end of the start signal of the microcontroller, and then sends an 80us low-level response signal. After the MCU sends the start signal, wait for a delay of 20-40us, switch to the input state, wait for the end of the 80us low level signal of DHT11, and then judge whether DHT11 sends a high level of 80us; if it is, start collecting data .

Sequence of digital 0: When DHT11 outputs digital 0, the signal read by the single-chip microcomputer is a low level of 50 us, and then a high level of 26-28 us.

Sequence of number 1: When DHT11 outputs number 0, the signal read by the single-chip microcomputer is a low level of 50 us, and then a high level of 70 us.

Data format: 8bit humidity integer data + 8bit humidity decimal data + 8bi temperature integer data + 8bit temperature decimal data + 8bit checksum

8-bit checksum: 8bit humidity integer data + 8bit humidity decimal data + 8bi temperature integer data + 8bit temperature decimal data" to add the last 8 bits of the result.

2. Code function: The
DHT11 module is controlled by the single-chip microcomputer to measure temperature and humidity and feedback on the serial port.

3. Wiring:
VCC——VCC, GND——GND, DATA——PF0

Fourth, the code part:
dht.h

#ifndef __DHT_H
#define	__DHT_H

#include "sys.h"
#include "delay.h"
#include "usart.h"

#define IO_DHT11 GPIO_Pin_0
#define GPIO_DHT11 GPIOF

#define DHT11_DQ_High GPIO_SetBits(GPIO_DHT11,IO_DHT11)
#define DHT11_DQ_Low GPIO_ResetBits(GPIO_DHT11,IO_DHT11)

void DHT11_IO_OUT(void);
void DHT11_IO_IN (void);
void DHT11_Init(void);
void DHT11_Rst(void);
u8 DHT11_Read_Data(u8 *temp,u8 *humi);
u8 DHT11_Read_Byte(void);
u8 DHT11_Read_Bit(void);
u8 DHT11_Check(void);

#endif

dht.c

#include "dht.h"



void DHT11_IO_IN (void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOF,GPIO_Pin_0);
	
}

void DHT11_IO_OUT (void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOF, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOF,GPIO_Pin_0);

}

void DHT11_Rst(void)
{
    
    
	DHT11_IO_OUT();
	DHT11_DQ_Low;
	delay_ms(20);
	DHT11_DQ_High;
	delay_us(30);

}

u8 DHT11_Check(void)
{
    
    
	u8 retry=0;
	DHT11_IO_IN();
	
	while ((GPIO_ReadInputDataBit(GPIO_DHT11,IO_DHT11)==1)&&retry<100)
	{
    
    
		retry++;
		delay_us(1);
	};
	
	if(retry>=100)
		return 1;
	else retry=0;
	
	while ((GPIO_ReadInputDataBit(GPIO_DHT11,IO_DHT11)==0)&&retry<100)
	{
    
    
		retry++;
		delay_us(1);
	};	
	if(retry>=100)
		return 1;
		return 0;
	
}

u8 DHT11_Read_Bit(void)
{
    
    
	u8 retry=0;
	while ((GPIO_ReadInputDataBit(GPIO_DHT11,IO_DHT11)==1)&&retry<100)
	{
    
    
		retry++;
		delay_us(1);
	};
	retry=0;
	while ((GPIO_ReadInputDataBit(GPIO_DHT11,IO_DHT11)==0)&&retry<100)
	{
    
    
		retry++;
		delay_us(1);
	};	
	delay_us(50);
	if(GPIO_ReadInputDataBit(GPIO_DHT11,IO_DHT11)==1)
	return 1;
	else 
	return 0;

}

u8 DHT11_Read_Byte(void)
{
    
    
	u8 i,dat;
	dat=0;
	for (i=0;i<8;i++)
	{
    
    
		dat<<=1;
		dat|=DHT11_Read_Bit();
	}
	return dat;
}

u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
    
    
	u8 buf[5];
	u8 i;
	DHT11_Rst();
	if(DHT11_Check()==0)
	{
    
    
		for (i=0;i<5;i++)
		{
    
    
			buf[i]=DHT11_Read_Byte();		
		}
		if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
		{
    
    
			*temp=buf[2];
			*humi=buf[0];
		}		
	}
	else return 1;
	return 0;
}

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


mian.c

#include "sys.h"
#include "delay.h"
#include "usart.h"
#include	"led.h"
#include	"beep.h"
#include	"key.h"
#include "dht.h"

int main(void)
{
    
    
	u8 wd=0;
	u8 sd=0;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init(168);
	uart_init(115200);
	DHT11_Init();

	while(1)
	{
    
    
		DHT11_Read_Data(&wd,&sd);
		printf("ζȣº%d ¡¯C\r\n",wd);
		printf("滦飼%d %%r\n",sd);
		delay_ms(1000);
		delay_ms(1000);	
	}
}

PS: Successfully tested based on stm32f407zgt6

Reference for this article: Detailed explanation of the principle and driver of DHT11 temperature and humidity sensor

Only for beginners to learn and use

Guess you like

Origin blog.csdn.net/weixin_54121461/article/details/114118934