DHT11 temperature and humidity LCD display

Keywords: IoT C51

1, Introduction to temperature and humidity module sensor

DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It applies special digital module acquisition technology and temperature and humidity sensing technology to ensure that the product has high reliability and excellent long-term stability. The sensor includes a resistive humidity sensing element and an NTC temperature measuring element, and is connected with a high-performance 8-bit single chip microcomputer.


Power supply voltage: 3.3~5.5V DC
The output is a single bus digital signal
Temperature measurement range: 0 ~ 50 degrees (accuracy: plus or minus 2 degrees, resolution: 1 degree)
The humidity measurement range is 20 ~ 90% RH (the accuracy is plus or minus 5% and the resolution is 1%)

Using the single bus two-way serial communication protocol, each acquisition should be initiated by the single chip microcomputer, and then DHT11 will send a response to the single chip microcomputer and start transmitting 40 bit data frames, with the high bit in front.

The data format is:
The first two bytes: 8bit humidity integer data + 8bit humidity decimal data
The second and third bytes: 8bit temperature integer data + 8bit temperature decimal data
The fifth byte: 8bit check bit (it is the value of eight bits after the first four data are added)
The decimal part of temperature and humidity is 0 by default, that is, the data collected by the single chip microcomputer are integers, and the data with the check bit of 4 bytes are added, and the lower 8 bits of the result are taken as the check sum.

2, Relevant sequence diagram

Communication process:

The bus idle state is high level. The host pulls down the bus and waits for the response of DHT11. The host must pull down the bus for more than 18 milliseconds to ensure that DHT11 can detect the start signal. After receiving the start signal from the host, DHT11 waits for the end of the host start signal, and then sends an 80us low-level response signal. After the host sends the start signal, after a delay of 20-40us, it reads the response signal of DHT11. After the host sends the start signal, it can switch to the input mode or output high-power average. The bus can be pulled up by the pull-up resistance.

Output digital 0 signal

Output digital 1 signal

3, Sample code

Display temperature and humidity data on LCD1602 screen:

DHT11.c

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

sbit Temp_data = P1^0;
unsigned int rec_dat[4];

unsigned char rec_dat_lcd0[6];
unsigned char rec_dat_lcd1[6];
unsigned char rec_dat_lcd2[6];
unsigned char rec_dat_lcd3[6];


void DHT11_start()	
{
	Temp_data=1;
	DHT11_delay_us(2);
	
	Temp_data=0;
	DHT11_delay_ms(20);   // Pull down for at least 18ms	
	
	Temp_data=1;
	DHT11_delay_us(13);
}

// Receive a byte
unsigned char DHT11_rec_byte()
{
	
	unsigned char i,dat;

	
	for(i=0;i<8;i++)
	{
			while(!Temp_data);   // Wait for the low level to pass
		  DHT11_delay_us(8);	 // Delay 8us if high
			dat<<=1;
			if(Temp_data==1)
			{
				dat+=1;
			}
			while(Temp_data);		// Wait to pull down, ready for the next one
	}

	return dat;

	
}

// Receive temperature and humidity data
void DHT11_receive()
{
	unsigned int R_H,R_L,T_H,T_L;
	unsigned char RH,RL,TH,TL,revise;
	
	DHT11_start();
	Temp_data=1;
	
	if(Temp_data==0)
	{
		while(Temp_data==0);      // Wait for the low level to pass
        DHT11_delay_us(40); // Delay after pulling up
		
        R_H=DHT11_rec_byte();    // Humidity high eight
        R_L=DHT11_rec_byte();    // Humidity low eight
        T_H=DHT11_rec_byte();    // Eight digit high temperature
        T_L=DHT11_rec_byte();    // Low temperature octave
        revise=DHT11_rec_byte(); 

        DHT11_delay_us(25);   

        if((R_H+R_L+T_H+T_L)==revise)      
        {
            RH=R_H;
            RL=R_L;
            TH=T_H;
            TL=T_L;
	
        } 
        
        rec_dat[0]=RH;
        rec_dat[1]=RL;
        rec_dat[2]=TH;
        rec_dat[3]=TL;

	}
	
}


void DHT11_delay_us(unsigned char n)
{
    while(--n);
}


void DHT11_delay_ms(unsigned int z)
{
   unsigned int i,j;
   for(i=z;i>0;i--)
      for(j=110;j>0;j--);
}


DHT11.h

#ifndef _DHT11_H_

#define _DHT11_H_
	void DHT11_start();
	unsigned char DHT11_rec_byte();
	void DHT11_receive();
	void DHT11_delay_us(unsigned char n);
	void DHT11_delay_ms(unsigned int z);
#endif

LCD.c

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

sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_E = P2^2;

#define LCD_data P0

void Delay1ms()		//@11.0592MHz
{
	unsigned char i, j;

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


void LCD_Command(unsigned char command)
{
	LCD_RS = 0;			
	LCD_RW = 0;		
	LCD_data = command;
	LCD_E = 1;			
	Delay1ms();
	LCD_E = 0;
	Delay1ms();
}

void LCD_WriteData(unsigned char datas)
{
	LCD_RS = 1;			
	LCD_RW = 0;			
	LCD_data = datas;
	LCD_E = 1;			
	Delay1ms();
	LCD_E = 0;
	Delay1ms();
}

void LCD_init()
{
	LCD_Command(0x38);   
	LCD_Command(0x0c);	
	LCD_Command(0x06);  
	LCD_Command(0x01);	
}

void setCursor(unsigned char row,unsigned char column)
{
		if(row == 1){
		LCD_Command(0x80|(column-1));   // ¹â±êµØÖ·
	}else{
		LCD_Command(0x80|((column-1)+0x40));
	}
}


// display string
void LCD_show_string(unsigned char row,unsigned char column,unsigned char *string)
{	
	unsigned int i;
	setCursor(row,column);
	for(i=0;string[i]!='\0';i++){
			LCD_WriteData(string[i]);
		}
}

LCD.h

#ifndef _LCD_H_
#define _LCD_H_
	void Delay1ms();
	void LCD_Command(unsigned char command);
	void LCD_WriteData(unsigned char datas);
	void LCD_init();
	void setCursor(unsigned char row,unsigned char column);
	void LCD_show_char(unsigned char row,unsigned char column,unsigned char Char);
	void LCD_show_string(unsigned char row,unsigned char column,unsigned char *string);
	int pow(unsigned int x,unsigned int y);
	void LCD_show_num(unsigned char row,unsigned char column,unsigned int num,unsigned int length);
#endif

main.c

#include <REGX52.H>
#include "LCD.h"
#include "DHT11.h"
#include <stdio.h>

extern unsigned int rec_dat[4];

extern unsigned char rec_dat_lcd0[6];
extern unsigned char rec_dat_lcd1[6];
extern unsigned char rec_dat_lcd2[6];
extern unsigned char rec_dat_lcd3[6];


void main()
{	
	LCD_init();
	LCD_show_string(1,1,"Humi:");
	LCD_show_string(2,1,"Temp:");
		
	while(1){
		
			DHT11_delay_ms(150);
			DHT11_receive();

			sprintf(rec_dat_lcd0,"%d",rec_dat[0]);
			sprintf(rec_dat_lcd1,"%d",rec_dat[1]);
			sprintf(rec_dat_lcd2,"%d",rec_dat[2]);
			sprintf(rec_dat_lcd3,"%d",rec_dat[3]);
			DHT11_delay_ms(100);
		
			LCD_show_string(1,6,rec_dat_lcd0);
			LCD_show_string(1,8,".");
			LCD_show_string(1,9,rec_dat_lcd1);
			LCD_show_string(1,11,"%");
		
			LCD_show_string(2,6,rec_dat_lcd2);
			LCD_show_string(2,8,".");
			LCD_show_string(2,9,rec_dat_lcd3);
			LCD_show_string(2,11,"C");
			
	}
	
}

Posted by watson100 on Thu, 23 Sep 2021 06:39:18 -0700