51 MCU based DHT11 temperature and humidity sensor

Publisher:stampieLatest update time:2022-10-11 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

wiring: 

Code:


main.c file

 

/*

Use the DHT11 module to detect temperature and humidity and display them on the LCD1602. Because the 1602 display does not always work, serial port printing is added to make it more intuitive.

*/

#include

#include "lcd.h"

#include

#include

 

sbit Temp_data=P3^6; //Signal pin

 

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_delay_us(unsigned char n);

void DHT11_delay_ms(unsigned int z);

void DHT11_start();

unsigned char DHT11_rec_byte();

void DHT11_receive();

void InitUART(void);

 

//Main function

void main()

{

InitUART(); //Serial port interrupt initialization

P1=0xf0;

InitLcd1602();

LcdShowStr(0,0,"Shidu:");

LcdShowStr(0,1,"Wendu:");

 

while(1)

{

DHT11_delay_ms(150);

DHT11_receive();

 

//This function is a print character function, which should play the role of numerical conversion.

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);

//humidity

LcdShowStr(7,0,rec_dat_lcd0);

LcdShowStr(9,0,".");

LcdShowStr(10,0,rec_dat_lcd1);

LcdShowStr(11,0," %");

//temperature

LcdShowStr(7,1,rec_dat_lcd2);

LcdShowStr(9,1,".");

LcdShowStr(10,1,rec_dat_lcd3);

LcdShowStr(11,1,"C");

//The following prints the temperature through the serial port assistant

//I also wonder why it can print without using the interrupt service function. Why do we need this interrupt? Can't we print directly? This is my question.

printf("Humi:%d.%d n",rec_dat[0],rec_dat[1]);

printf("Temp:%d.%d °Cn",rec_dat[2],rec_dat[3]);

}

}

 

//DHT11 start signal

void DHT11_start()

{

Temp_data=1;

DHT11_delay_us(2);

Temp_data=0;

DHT11_delay_ms(20);

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);

  DHT11_delay_us(8);

dat<<=1;

if(Temp_data==1)

{

dat+=1;

}

while(Temp_data);

}

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 high     

        DHT11_delay_us(40); //Delay 80us after pulling high

        R_H=DHT11_rec_byte(); //Receive high eight bits of humidity  

        R_L=DHT11_rec_byte(); //Receive the lower eight bits of humidity  

        T_H=DHT11_rec_byte(); //Receive high eight bits of temperature  

        T_L=DHT11_rec_byte(); //Receive the lower eight bits of temperature

        revise=DHT11_rec_byte(); //Receive correction bit

 

        DHT11_delay_us(25); //End

 

        if((R_H+R_L+T_H+T_L)==revise) //correction

        {

            RH=R_H;

            RL=R_L;

            TH=T_H;

            TL=T_L;

        } 

        /*Data processing, convenient display*/

        rec_dat[0]=RH;

        rec_dat[1]=RL;

        rec_dat[2]=TH;

        rec_dat[3]=TL;

}

}

//Delay us --2*n+5us

void DHT11_delay_us(unsigned char n)

{

    while(--n);

}

 

//Delay in ms

void DHT11_delay_ms(unsigned int z)

{

   unsigned int i,j;

   for(i=z;i>0;i--)

      for(j=110;j>0;j--);

}

 

//Use timer 1 as the serial port baud rate generator

void InitUART(void)

{

TMOD=0x20; //Timer 1 working mode 2

SCON=0x40; //Serial communication working mode 1

REN=1; //Allow receiving

TH1=0xFD,TL1=0xFD; //Baud rate=9600

TI=1; // Pay attention here

TR1=1;

EA = 1; // Enable general interrupt

}


Lcd.c file

 

#include "lcd.h"

 

void Lcd1602_Write_Cmd(unsigned char cmd) //write command

{

    //Read_Busy();

    LCD1602_RS = 0;

    LCD1602_RW = 0;

    LCD1602_DB = cmd;

LCD_Delay10ms(1);

    LCD1602_EN = 1;

LCD_Delay10ms(1);

    LCD1602_EN = 0;    

}

 

void Lcd1602_Write_Data(unsigned char dat) //write data

{

      //Read_Busy();

      LCD1602_RS = 1;

      LCD1602_RW = 0;

      LCD1602_DB = dat;

LCD_Delay10ms(1);

      LCD1602_EN = 1;

LCD_Delay10ms(1);

      LCD1602_EN = 0;

}

//Start displaying data at the specified position!

void LcdSetCursor(unsigned char x,unsigned char y) //Coordinate display

{

    unsigned char addr;

    if(y == 0)

        addr = 0x00 + x; //Starting from the first line, x represents the xth number in a line

    else

        addr = 0x40 + x; //Starting from the second line, x represents the xth number in a line

    

    Lcd1602_Write_Cmd(addr|0x80);

}

 

void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) //display string

{

    LcdSetCursor(x,y); //The coordinates of the current character

    while(*str != '')

    {

        Lcd1602_Write_Data(*str++);

    }

}

 

void InitLcd1602() //1602 initialization

{

    Lcd1602_Write_Cmd(0x38); //Open, 5*8, 8-bit data

    Lcd1602_Write_Cmd(0x0c);

    Lcd1602_Write_Cmd(0x06);

    Lcd1602_Write_Cmd(0x01); //Clear screen   

}

 

void LCD_Delay10ms(unsigned int c) //Error 0us

{

    unsigned char a,b;

    for(;c>0;c--)

        for(b=38;b>0;b--)

            for(a=130;a>0;a--);

}


Lcd.h file

 

#ifndef __LCD_H_

#define __LCD_H_

/**********************************

Defined when 4-bit data transmission is used,

Use 8 bits to unset this definition

**********************************/

//#define LCD1602_4PINS

/**********************************

Include header files

**********************************/

#include

//---Redefine keywords---//

#ifndef uchar

#define uchar unsigned char

#endif

#ifndef uint 

#define uint unsigned int

#endif

/**********************************

PIN port definition

**********************************/

#define LCD1602_DB P0 //data bus data bus

sbit LCD1602_RS = P2^6;

sbit LCD1602_RW = P2^5;

sbit LCD1602_EN = P2^7;  

/**********************************

Function declaration

**********************************/

/*Delay function under 12MHZ clock of 51 MCU*/

//void Lcd1602_Delay1ms(uint c); //error 0usvo

void LCD_Delay10ms(unsigned int c);

//void Read_Busy(); //Busy detection function, if bit7 is 0, execution is allowed; if bit7 is 1, execution is prohibited

void Lcd1602_Write_Cmd(unsigned char cmd); //write command

void Lcd1602_Write_Data(unsigned char dat); //write data

void LcdSetCursor(unsigned char x,unsigned char y); //Coordinate display

void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str); //Display string

void InitLcd1602(); //1602 initialization

  

#endif


operation result:

 

Reference address:51 MCU based DHT11 temperature and humidity sensor

Previous article:51 MCU Basics DS1302
Next article:51 MCU Basics - Serial Communication

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号