STC15 single chip microcomputer DHT11 temperature and humidity detection (detailed comments) oled display program

Publisher:SereneWandererLatest update time:2019-10-24 Source: 51heiKeywords:STC15 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I added oled here to display temperature and humidity, using STC15, dht11 modules


#include

#include "intrins.h" // used to provide _nop_(); function

#include "OLED_Contrul_Hygrothermograph.c" //OLED control function


sbit YLED=P3^1;

sbit RLED=P3^0;

sbit DHT=P3^5; //DHT11 module


unsigned int Temperature[3]={1,1,1}; //The first two digits are the integer part, and the last digit is the decimal part

unsigned int Humidity[2]={1,1}; //The returned humidity data only has the integer part


unsigned char Data_Receive[5];

/*##############################################################################################

Design name: Hygrothermograph based on DHT11

Designer: Qinghe


Platform: STC15F104W

Hardware parts

Display: OLED12864 is used to display temperature and humidity

The measurement uses the DHT11 module, and the module information is as follows:

ASATR

-----

NA:DHT11

OC:3.3-5.5V

U1:5-95%RH

U2:-20-60℃

U3:±5%RH±2℃

SN:A1712882A


To ensure accurate delay, the crystal oscillator is set to 12MHz


I borrowed some programs from http://www.51hei.com/bbs/dpj-38067-1.html, and I would like to express my gratitude

##############################################################################################*/


void Delay30us()//@12.000MHz

{

        unsigned char i;

        i=87;

        while(--i);

}


void Delay20ms()//@12.000MHz

{

        unsigned char i,j,k;

        i=1;

        j=234;

        k=113;

        do

        {

                do

                {

                        while(--k);

                }while(--j);

        }while(--i);

}


void Delay1s()//@12.000MHz

{

        unsigned char i,j,k;

        i=46;

        j=153;

        k=245;

        do

        {

                do

                {

                        while(--k);

                }while(--j);

        }while(--i);

}


void OLED_Display() //OLED display driver function

{

        OLED_DataDisplay_T(56,4,Temperature); //Display temperature at the specified position

        OLED_DataDisplay_H(56,6,Humidity); //Display humidity at the specified location

}


void DHT_Collect()//Temperature collection function

{

        unsigned char i,j;//for loop variable

        unsigned char t; //Timeout judgment

        unsigned char dat8=0; //8 bits of data read at a time, need to be read 5 times

        unsigned int CheckValue=0; //Checksum

        

        DHT=0; //Host sends start signal

        Delay20ms(); //Host pulls the bus down for at least 18ms

        DHT=1; //Host pulls up the bus for 20~40us

        Delay30us();

        

        t=80; //Set the timeout waiting time

        while(DHT&&t--); //Wait for DHT11 to pull down the bus

        if(t==0)//timeout

        {

                DHT=1;

        }

        t=250; //Set the timeout waiting time

        while(!DHT&&t--); //Wait for DHT11 to pull up the bus

        if(t==0)//timeout

        {

                DHT=1;

        }

        t=250; //Set the timeout waiting time

        while(DHT&&t--); //Wait for DHT11 to pull down the bus

        if(t==0)//timeout

        {

                DHT=1;

        }


        for(j=0;j<5;j++) //5 times reading

        {

                for(i=0;i<8;i++) // 1 time 8 bits

                {

        //Wait for 50us to start the time slot

                        t=150; //Set the timeout waiting time

                        while(!DHT&&t--); //Wait for DHT11 to pull up the bus

                        if(t==0)//timeout

                        {

                                DHT=1;

                        }

                        t=0; //Record time clear

                        while(DHT&&++t); //Wait and record the duration of high level

                        dat8<<=1;

                        if(t>30) //High level lasts longer (70us)

                        dat8+=1; //transmission value is 1

                }

                Data_Receive[j]=dat8;

        }

        

        Delay30us(); //Wait for DHT11 to pull down for 50us

        Delay30us();

        DHT=1;


        for(i=0;i<4;i++)

        {

                CheckValue+=Data_Receive[i]; //Calculate the check value

        }

        if((unsigned char)CheckValue==Data_Receive[4])//比对

        {

                Temperature[0]=Data_Receive[2]/10;

                Temperature[1]=Data_Receive[2]%10;

                Temperature[2]=Data_Receive[3]%10;

                Humidity[0]=Data_Receive[0]/10;

                Humidity[1]=Data_Receive[0]%10;

        }

}


void sys_init()//system initialization function

{

        YLED=1;

        RLED=1;

        OLED_Init(); //OLED initialization

        OLED_CLS(); //Clear screen

        

        OLED_FormTitle(0,0); //Display title text Linyar Lab.

        OLED_FormName(0,2); //Display Hygrothermograph name

        OLED_FormLine(0,3); //Display a horizontal line

        OLED_FormTemp(8,4); //Display temperature text

        OLED_FormHumi(16,6); //Display humidity text

        OLED_AnyChar_16X16(96,4,TempChar); //Display the Celsius degree ℃ character at row 4 (each row is 16 pixels high) and column 96 pixel position

        OLED_AnyChar_8X16(88,6,HumiChar); //Display the percent sign % at row 6 (each row is 16 pixels high) and column 88 pixels

        OLED_AnyChar_8X16(72,4,PointChar); //Display the colon character at row 4 (each row is 16 pixels high) and column 72 pixels

}


void main()

{

        P3M0=0X00;

        P3M1=0X00;

        sys_init();

        

        while(1)

        {

                DHT_Collect(); //DHT11 data collection

                YLED=1;RLED=0;//Light state conversion

                OLED_Display(); //OLED display function

                RLED=1;YLED=0;//Light state conversion

                Delay1s();

                Delay1s(); //Note: A long delay is required, otherwise it will read 0

        }

}



Keywords:STC15 Reference address:STC15 single chip microcomputer DHT11 temperature and humidity detection (detailed comments) oled display program

Previous article:51 single chip microcomputer intelligent car ultrasonic obstacle avoidance experiment
Next article:51 MCU RS485 program source code and proteus simulation diagram

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号