STM32- digital temperature and humidity sensor drive DHT11

DHT11 Module Introduction

DHT11 digital temperature and humidity sensor for measuring the ambient temperature and humidity, and the data transmission is a digital signal, which analog data acquisition and transmission DS18B20 not the same, in terms of handling compared DS18B20 DHT11 more accurate data collection, and driving more convenient.
DHT11 sensor comprises a resistive sensing element and a wet NTC temperature measurement element, and a sensor embedded in an 8-bit microcontroller. Embedded MCU after treatment, the processed data can be output directly after the sensing element to the measured data.
DHT11 single bus communication between the microcontroller and, initialized only one I / O port to achieve real-time measurement of temperature and humidity.
The relevant parameters are as follows:
Here Insert Picture Description

DHT11 data transmission

DHT11 single data transfer bus communication, i.e., data is completed by a two-way input-output port IO, DHT11 each time data is transmitted to the microcontroller 40Bit transmission data packet, the data packet 40Bit contains the following:

  • The integer part (32-39) and humidity
  • Fractional part (24-31) and humidity
  • (16-23) of the integer part of the temperature
  • (8-15) the temperature of the fractional part of
  • (0-7) parity data portion, and the first four parts

For example:
Here Insert Picture Description
humidity: 45.0
temperature: 28.0
verified as: 45 + 73 = 28 (the data is correct)

DHT11 communication sequence

Since DHT11 and MCU are single bus communication, so that two-way transmission of data to rely on strict timing, the timing of the communication microcontroller and DHT11 three parts:

  • Sending a reset signal to the microcontroller DHT11
  • SCM response signal DHT11
  • Transmitting data packets to the microcontroller DHT11

The overall timing chart as shown:
Here Insert Picture Description
The first step: sending a reset signal, then the pin pattern to be push-pull output . The data line low for t1 (at least 18ms) times; and then pulled data line for t2 (20 ~ 40us) time.
Step Two: read the corresponding, mode The pin should be floating input . DHT11 data line will be pulled low for t3 (40 ~ 50us) time, as the respective signal; data line is then pulled DHT11 for t4 (40 ~ 50us) time, begin transmitting packets.
The third step: transmission of data packets. Transfer "1", 12 ~ 14us low +116 ~ 118us high; transmission "0", 12 ~ 14us low +26 ~ 28us high.
Here Insert Picture Description

These three steps to complete a full data transmission, and then processed on display like a microcontroller is received packets.

Code

The main driver code to achieve the following modules:

  • The associated pin initialization
  • Reset module
  • Analyzing response module
  • Reading the data packet module
  • Display Module

The associated pin initialization

Because it is a IO port, to meet the output and the detection level, so I wrote two functions to switch the mode pin, the pin can be switched between a push-pull output and the floating inputs:

/* 相关引脚的配置,DHT是单通道通信,所以一个引脚就够了 */
void DHT_GPIO_Config_Output( void )
{
	GPIO_InitTypeDef GPIO_InitStruct;	
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStruct.GPIO_Pin = DHT_PIN;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(DHT_PORT, &GPIO_InitStruct);
}

/* 同一个引脚 要在输入和输出的时候进行切换引脚模式 */
void DHT_GPIO_Config_Input( void )
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStruct.GPIO_Pin = DHT_PIN;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(DHT_PORT, &GPIO_InitStruct);
}

In the initialization will start the clock, so maybe you do not start the clock module

Reset module

void DHT11_Rst( void )
{
	DHT_GPIO_Config_Output();
	DHT11_L;
	Delay_ms( 20 );
	DHT11_H;
	Delay_us( 30 );
}

Analyzing response module

uint8_t DHT11_Check( void )
{
	uint8_t t;
	/* 浮空输入,判断引脚输入电平 */
	DHT_GPIO_Config_Input();
	while( DHT11_Value_L && t<100 )
	{
		t++;
		Delay_us(1);
	}
	if( t>100 ) return 1;
	t=0;
	while( DHT11_Value_H && t<100 )
	{
		t++;
		Delay_us(1);
	}
	if( t>100 ) return 1;
	return 0;
}

Reading the data packet module

void DHT11_Read_Data( uint8_t *temp, uint8_t *humi )
{
	uint8_t i,t;
	uint9_t data[5];
	DHT_GPIO_Config_Input();
	/* 执行40次读取位数据,data指向数据包 */
	for( i=0;i<40;i++ )
	{
		/* 开始低电平 */
		while( DHT11_Value_L && t<100 )
		{
			t++;
			Delay_us(1);
		}
		t=0;
		/* 开始高电平 */
		while( DHT11_Value_H && t<100 )
		{
			t++;
			Delay_us(1);
		}
		Delay_us(40);
		/* 判断高电平持续时间 */
		if( DHT11_Value_H )
		{
			data[i/8] |= 0x01;
			data[i/8] = data[i/8]<<(i%8);
		}
		else
			data[i/8] = data[i/8]<<(i%8);
			
	}
	/* 将数据地址传出去 */
	*humi = data[0];
	*temp = data[2];
}

But generally summed up what inadequacies also hope our friends pointed out, what problem can communicate with me, we progress together!
q: 2723808286

Published 62 original articles · won praise 188 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43743762/article/details/103222095