[51 microcontroller STC89C52] Use of DHT11 temperature and humidity sensor

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

1. Overview of DHT11

Data transfer logic

A complete data transmission of the module is 40 bits, with the high bit first


Data Format

8bit humidity integer data + 8bit humidity decimal data + 8bit temperature integer data + 8bit temperature decimal data + 8bit checksum


Communication process sequence diagram

2. Check whether the module exists

According to the following sequence diagram, perform communication initialization and check whether the module exists and whether the function is normal.

Sequential logic analysis

a : dht = 1


b :dht = 0


Delay at least 18ms


c: dht = 1


Stuck point d: while(dht)


Card e point: while(!dht)


Stuck point f: while(dht)


 Code

void DHT11_Start()

{

dht = 1;

dht = 0;

//Delay 30ms

Delay30ms();

dht = 1;

    //Stuck point d: while(dht); stuck point e: while(!dht); stuck point f: while(dht)

while(dht);

while(!dht);

while(dht);

}


3. Read temperature and humidity data

Timing analysis of DHT11 transmitting 0

Timing analysis of DHT11 transmission 1

Code

char dates[5];

 

void Read_Data_From_DHT()

{

int i;//wheel

int j;//How many times to read in each round

char tmp;

char flag;

DHT11_Start();

for(i=0;i<5;i++){      

for(j=0;j<8;j++){

while(!dht);//Waiting for stuck g point

Delay40us();//Valid data are all high level, the duration is different, 40us reading, low level 0, high level 1

if(dht == 1){

flag = 1;

while(dht);

}else{

flag = 0;

tmp = tmp << 1;

tmp |= flag;

}

datas[i] = tmp;

}

}


4. Serial port printing

#include "reg52.h"

#include "intrins.h"

 

sbit ledOne = P3^7;

sbit dht = P3^3;//The data of the module is inserted in p3.3

 

char dates[5];

 

sfr AUXR = 0x8E;

 

void UartInit(void) //9600bps@11.0592MHz

{

AUXR = 0x01;

SCON = 0x40; //Configure serial port working mode 1, REN disables reception

TMOD &= 0xF0;

TMOD |= 0x20; //Timer 1 working mode bit 8-bit automatic reloading

TH1 = 0xFD;

TL1 = 0xFD; //Initial value of 9600 baud rate

TR1 = 1;//Start the timer

}

void sendByte(char data_msg)

{

SBUF = data_msg;

while(!IF);

IF = 0;

}

 

void sendString(char* str)

{

while( *str != ''){

sendByte(*str);

str++;

}

}

 

void Delay30ms() //@11.0592MHz

{

unsigned char i, j;

 

i = 54;

j = 199;

do

{

while (--j);

} while (--i);

}

 

void Delay40us() //@11.0592MHz

{

unsigned char i;

 

_nop_();

i = 15;

while (--i);

}

 

void Delay1000ms() //@11.0592MHz

{

unsigned char i, j, k;

 

_nop_();

i = 8;

j = 1;

k = 243;

do

{

do

{

while (--k);

} while (--j);

} while (--i);

}

 

void DHT11_Start()

{

dht = 1;

dht = 0;

//Delay 30ms

Delay30ms();

dht = 1;

//Stuck point d: while(dht1); stuck point e: while(!dht); stuck point f: while(dht)

while(dht);

while(!dht);

while(dht);

}

 

void Read_Data_From_DHT()

{

int i;//wheel

int j;//How many times to read in each round

char tmp;

char flag;

DHT11_Start();

for(i= 0;i < 5;i++){

for(j=0;j<8;j++){

while(!dht);//Waiting for stuck g point

Delay40us();//Valid data are all high level, the duration is different, 40us reading, low level 0, high level 1

if(dht == 1){

flag = 1;

while(dht);

}else{

flag = 0;

tmp = tmp << 1;

tmp |= flag;

}

datas[i] = tmp;

}

}

 

void main()

{

ledOne = 1;

UartInit();

Delay1000ms();

Delay1000ms();

while(1){

Delay1000ms();

Read_Data_From_DHT();

sendString("H:"); //Humidity

sendByte(datas[0]/10 + 0x30);

sendByte(datas[0]%10 + 0x30);

sendByte('.');

sendByte(datas[1]/10 + 0x30);

sendByte(datas[1]%10 + 0x30);

sendString("rn");

sendString("T:"); //Temperature

sendByte(datas[2]/10 + 0x30);

sendByte(datas[2]%10 + 0x30);

sendByte('.');

sendByte(datas[3]/10 + 0x30);

sendByte(datas[3]%10 + 0x30);

sendString("rn");

        //The decimal part of the humidity is 0; the decimal part of the temperature can only display 1 decimal digit, and the code needs to be modified.

}

}


Reference address:[51 microcontroller STC89C52] Use of DHT11 temperature and humidity sensor

Previous article:[51 microcontroller STC89C52] IO port simulates PWM control of SG90 servo
Next article:[51 MCU STC89C52] Use of LCD1602 LCD screen

Recommended ReadingLatest update time:2024-11-16 22:40

STC89C52 MCU - Serial port sending program 1
/*-----------------------------------------------   Name: Serial communication   Written by: LZM   Date: 2015.05   Modification: None   Content: Connect the serial port and set the baud rate to 9600 without parity check.         Crystal oscillator 11.0592MHz Connect to the serial port and you can receive the characte
[Microcontroller]
STC89C52 MCU - Serial port sending program 1
51 single chip microcomputer STC89C52 drives a digital tube to display 0~9, A~F
Program source code /*-----------------------Include header file area-------------------------*/ #include reg52.h   //MCU header file  /*-----------------------Data type definition area-----------------------*/ typedef unsigned char u8; //define type unsigned char alias u8 typedef unsigned int u16; //define type u
[Microcontroller]
51 single chip microcomputer STC89C52 drives a digital tube to display 0~9, A~F
Program for 51 MCU to control DHT11 to display temperature and humidity on 1602
The microcontroller source program is as follows: #include reg52.h   #define uint unsigned int  #define uchar unsigned char  typedef bit BOOL;  sbit io = P2^0;  sbit rs = P2^6;   sbit rw = P2^5;  sbit ep = P2^7;  volatile data_byte;  uchar RH,RL,TH,TL; /********Delay module***********/  void delay(uchar ms)  { // Del
[Microcontroller]
[51 microcontroller STC89C52] Timer (interrupt) controls LED
1. Timer/Counter T0 1. Timer/counter related registers 2. Timer/counter control register TCON The TCON format is as follows: 3. Timer/counter operating mode register TMOD Mode selection:   2. Configure related registers 1. Use STC-ISP tool 2. Configuration register Set timer mode (16-bit timer) It is recom
[Microcontroller]
[51 microcontroller STC89C52] Timer (interrupt) controls LED
STC89C52 MCU PS2 keyboard decoding
///////////////////////////////////////////////////// /////////////////////////   Function: Realize the data collection, decoding and display of the P2 keyboard, that is,           collect the data value of the P2 keyboard through interruption, then decode it through software, and finally display the corresponding le
[Microcontroller]
STM32 drives DHT11 temperature and humidity sensor
DHT11 is a digital sensor that integrates humidity and temperature. The sensor includes a resistive humidity sensor and an NTC temperature sensor, and is connected to a high-performance 8-bit microcontroller. Through a simple circuit connection with a microcontroller or other microprocessor, local humidity and tempera
[Microcontroller]
STM32 drives DHT11 temperature and humidity sensor
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号