51 MCU OLED+dht11 source program to display temperature

Publisher:CyborgDreamerLatest update time:2020-11-11 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have been learning 51 MCU for more than a month, and stayed up all night to write the OLED+DHT11 program for a competition.


The microcontroller source program is as follows:

#include "REG51.h"

#include "oled.h"

#include

typedef unsigned char BYTE;

typedef unsigned int WORD;

#define uint unsigned int

#define uchar unsigned char

sbit io=P1^2; //dht11data is connected to the P1^0 port of the microcontroller //

sbit IN1 = P1^0;                        

sbit IN2 = P1^1; //Input motor interface

sbit K1 = P3^0;

sbit K2 = P3^1; //button interface

uchar data_byte;


uchar RH,RL,TH,TL;


int count1,a;

void DelayUs2x(unsigned char t)


{


        while(--t);


}


void DelayMs(unsigned char t)


{


        while(t--)


        {


//About 1mS delay


        DelayUs2x(245);


        DelayUs2x(245);


        }


}


void delay(uchar ms) //delay module//

{


  uchar i;


  while(ms--)                 


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


}


void delay1() //A for loop takes about 8 machine cycles. One machine cycle is 1us, and the crystal oscillator is 12MHz. This means that this function delays more than 8us. This delay function must be a little more precise.


{


  uchar i;


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


}


//Motor program

void ZHUAN(){

       

          IN2=0;

   }



void speed(int cnt,int sd)

{  

if(cnt        {

        IN1=1;

        }

else  

        {

        IN1=0;

        }


}  

void turn(int cnt,int sd)                                   

{  

    ZHUAN();          

        speed(cnt,sd);

}  




void start()//start signal


{


  io=1;


  delay1();


  io=0;


  delay(25); // The host must pull the bus low for more than 18ms to ensure that DHT11 can detect the start signal


  io=1; //After sending the start signal, pull the level high and delay for 20-40us


  delay1(); //The following three delay functions are about 24us, which meets the requirements


  delay1();


  delay1();


}

uchar receive_byte() //Receive a byte //


{


  uchar i,temp;


  for(i=0;i<8;i++) //Receive 8-bit data


  {


        while(!io); //Wait for 50us low level start signal to end


        delay1(); //After the start signal ends, delay 26us-28us, the following three delay functions


         delay1();


        delay1();


        temp=0; //The time is 26us-28us, indicating that the received data is '0'


        if(io==1)


         temp=1; //If it is still high level after 26us-28us, it means the received data is '1'


    while(io); //Wait for data signal high level (26us-28us for '0' and 70us for '1')


        data_byte<<=1; //The received data has the high bit first and is shifted right


         data_byte|=temp;


  }


  return data_byte;


}

void receive()//Receive data//

{


  uchar T_H,T_L,R_H,R_L,check,num_check,i;


  start(); //Start signal//


  io=1; // Host is set as input to judge the response signal of slave DHT11


  if(!io)//Judge whether the slave has a low level response signal//


  {  

        while(!io); //Judge whether the slave has finished sending out the 80us low-level response signal //


        while(io); //Judge whether the slave has finished sending out a high level of 80us. If so, the host enters the data receiving state.


        R_H=receive_byte(); //High humidity


        R_L=receive_byte(); // Humidity low bit


        T_H=receive_byte(); // high temperature


        T_L=receive_byte(); // low temperature


        check=receive_byte(); //check bit


        io=0; //After the last bit of data is received, the slave device pulls the level down for 50us//


        for(i=0;i<7;i++)//About 50us delay


        delay1();


        io=1; //The bus is pulled high by the pull-up resistor and enters the idle state


        num_check=R_H+R_L+T_H+T_L;


        if(num_check==check)//Judge whether the sum of the four data read is the same as the check bit


        {


   RH=R_H;


   RL=R_L;


   TH=T_H;


   TL=T_L;


   check=num_check;

        }

  }

}


int main(void)

{       

        TMOD=0x01;

        EA=1;  

        TH0=(65536-1000)/256;

        TL0=(65536-1000)%256;

        ET0=1;

        TR0=1;

       

        a = 100;

                OLED_Init(); //Initialize OLED  

                OLED_Clear();

                OLED_ShowCHinese(36,0,0);

                OLED_ShowCHinese(54,0,1);

                OLED_ShowCHinese(72,0,2);

                OLED_ShowCHinese(0,2,3);

                OLED_ShowCHinese(18,2,5);

                OLED_ShowCHinese(0,4,4);

                OLED_ShowCHinese(18,4,5);

                OLED_ShowString(37,4,":",16);

                  OLED_ShowString(37,2,":",16);

        while(1)

        {

            turn(count1,a);


                if(K1==0)


                {


                        DelayMs(100);


                        if(K1==0)// debounce


                        {

                           if(a>=0)

                           a=a-10;

                        }


                        }

                if(K2==0)


……………………


Reference address:51 MCU OLED+dht11 source program to display temperature

Previous article:Detailed explanation of the single chip infrared sensor experiment
Next article:Graduation Project of Pulse Measurement Instrument Based on Single Chip Microcomputer

Recommended ReadingLatest update time:2024-11-15 15:31

5G calls for high refresh rate OLED, Samsung Display obtains two SGS certifications
Samsung Display’s official Weibo account recently announced a good news, that is, with its clear, fast-response and ghosting-free technology, Samsung Display has obtained the “Seamless Display” certification from a global technology certification company. In the 5G era, video and game consumption is showing a rapid
[Mobile phone portable]
5G calls for high refresh rate OLED, Samsung Display obtains two SGS certifications
Samsung is going to dominate the OLED screen market, big and small? Will LGD panic?
We all know that Samsung is the absolute leader in small-screen OLED, while LGD occupies the vast majority of the large-screen OLED market. However, a recent piece of news may overturn this situation.    Recently, there is news that Samsung Display will restart investment in large-size OLEDs and will build a QD-OLED
[Home Electronics]
Samsung is going to dominate the OLED screen market, big and small? Will LGD panic?
OLED display of picture and text based on 51 microcontroller
I. Introduction In terms of performance, OLED screens have a relatively higher resolution, fewer pins, and occupy much fewer I/O ports. In terms of power consumption, first of all, OLED displays do not require backlights. When current passes through, these organic materials can emit light, and they can be used between
[Microcontroller]
OLED display of picture and text based on 51 microcontroller
Korean media: Korean OLED products are accelerating their "expansion" in the Japanese market
Despite Japan's strong preference for domestic brands, Korean OLED panels and OLED TVs still occupy a high market share in the Japanese market. South Korean media businesskorea reported that LG Electronics' market share in Japan's OLED TV market in the second quarter was 7.4%, lower than Japanese brands Sony (35.2%)
[Mobile phone portable]
Apple suffered a setback in OLED panel production and had to pay Samsung approximately $950 million in compensation
According to foreign media reports, Apple may have paid $950 million in compensation to Samsung Display because it failed to purchase enough OLED panels from the latter to meet the minimum requirements stipulated in the contract.   Like many companies in the iPhone supply chain, Apple typically strikes deals with supp
[Power Management]
Apple suffered a setback in OLED panel production and had to pay Samsung approximately $950 million in compensation
Apple adds BOE to standard iPhone 14 OLED panel supply chain
Apple has reportedly signed an OLED display agreement with BOE, enabling the screen producer to supply components for the upcoming "iPhone 14". The contract is worth 50 million yuan ($7.75 million) and is said to stipulate that BOE will provide displays for the "iPhone 14" mobile phone products. This supply only inv
[Mobile phone portable]
The "five major" types of materials for making OLEDs
        There are many types of materials for preparing OLEDs, which are mainly divided into several categories such as anode materials, cathode materials, buffer layer materials, carrier transport materials and light-emitting materials.    1. Anode material The anode material of OLED is mainly used as the anode of t
[Power Management]
OLED display implementation method based on single chip microcomputer
    1 Introduction      Organic electroluminescent display, also known as organic light emitting diode (OrganICLightEMIttingDiode, OLED) or organic light emitting display OrganicLightEmittingDisplay (OLED), has obvious advantages over the popular liquid crystal display (LCD) on the market, mainly manifested in: self-l
[Microcontroller]
OLED display implementation method based on single chip microcomputer
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号