51 MCU + DHT11 temperature control program - can set temperature value

Publisher:SparklingStar22Latest update time:2015-05-13 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The temperature and humidity measurement control program based on 51 single chip microcomputer uses DHT11 digital sensor and 1602 LCD screen. The button definitions are as follows:

sbit key_1=P0^1; //Confirm key
sbit key_2=P0^2; //Key addition
sbit key_3=P0^3; //Key subtraction
Download the header file 12c5a.h used in this program: http://www.51hei.com/f/12c5a.rar
#include "12c5a.H"
#include "intrins.h"

unsigned char code num[]={"0123456789"};//Display character storage
unsigned char code start_char[]={"Loading...."};
unsigned char code t_char[]={"NOW TEMP IS"};
unsigned char *pnum=num;
unsigned int bbb,ccc,temp,temp_high,temp_low;

 
#define ADC_POWER 0x80 //ADC power control bit 
#define ADC_FLAG 0x10 //ADC complete flag 
#define ADC_START 0x08 //ADC start control bit 
#define ADC_SPEEDLL 0x00 //540 clocks 
#define ADC_SPEEDL 0x20 //360 clocks 
#define ADC_SPEEDH 0x40 / /180 clocks 
#define ADC_SPEEDHH 0x60 //90 clocks

 

sbit RS=P3^4; //Data command selection terminal
sbit RW=P3^5; //Read and write control terminal
sbit E=P3^6; //Enable signal control terminal
//sbit key_0=P0^0; //Set button
sbit key_1=P0^1; //Confirm button
sbit key_2=P0^2; //Key addition
sbit key_3=P0^3; //Key subtraction
sbit P04=P0^4;
sbit P05=P0^5;
sbit P06=P0^6;

void key_temphigh();
void key_templow();
void key_scan();
void delay_1602(unsigned int a);
void init_1602(void);
void WR_data_1602(unsigned char adata,unsigned char i);
void process(unsigned int shuju);


 void Delay(unsigned char n)                           
{                                                         
     int x;
  while (n--)                                  
     {
   x = 500;
   while (x--);
   }
}

void init_ADC(void)
{
 P1ASF=0xFF;
 ADC_RES = 0;
 ADC_RESL = 0;                                       
 ADC_CONTR = 0x88;
 Delay(2);  
}


unsigned int GetResult(void) 

 unsigned char m,n;
 unsigned int aaa;
    init_ADC();
    while (!(ADC_CONTR & ADC_FLAG)); //Wait complete flag 
    ADC_CONTR =0x80; //Close ADC 
 m=ADC_RES;
 n= ADC_RESL;                    
    aaa=m*4+n;
 return aaa;
}     
void start_init(void)
{
 unsigned char i;
  WR_data_1602(0x80,0); //Displayed in the first line
 for(i=0;i<11;i++)  
  WR_data_1602(start_char[i],1);
 delay_1602(20);
 init_ADC();
 temp=GetResult()*5.0;
 WR_data_1602(0x80,0); //Displayed in the first line
 for(i=0;i<11 ;i++)  
  WR_data_1602(t_char[i],1);  
}


void delay_1602(unsigned int a) //Delay
{
 unsigned int i,j,k;
 for(i=0;i   for(j=0;j<106;j++)
   for(k=0; k<100;k++);
}

[page]
void key_temphigh() // Temperature upper limit judgment
 { if(key_2==0) //Add judgment
    { Delay(3);
        if(key_2==0)
       { while(!key_2);
      temp_high+=5;
      if(temp_high==995)
      {temp_high=5;}
       }
    }
   if(key_3==0) //Subtract judgment
      { Delay(3);
        if(key_3==0)
       { while(!key_3); //Wait for
          releasetemp_high-=5; //The upper limit alarm temperature decreases by 0.5 each time
       if(temp_high==0)
        {temp_high=990;}
       }
      }
    }

void key_templow() // Temperature lower limit judgment
 { if(key_2==0) //Add judgment
    { Delay(3);
        if(key_2==0)
       { while(!key_2);
      temp_low+=5;
      if(temp_low==995)
      {temp_low=5;}
       }
    }
   if(key_3==0) //Subtract judgment
      { Delay(3);
        if(key_3==0)
       { while(!key_3); //Wait for release
          temp_low-=5; // The upper limit temperature decreases by 0.5 each time
       if(temp_low== 0)
        {temp_low=990;}
       }
      }
    }

void key_scan()
  { unsigned char nm=0,k;
     if(key_1==0) //Detect the setting button and determine whether to set it
      { if(key_1==0)
    { Delay(100);
     while(!key_1);
       k=1;nm++;
     }
    } 
   while(k) //Enter the adjustment interface,
   { if(key_1==0)
       { Delay(3);
       if(key_1==0)
     { while(!key_1);
        nm++; //Which parameter to adjustif
     (nm==4){nm=1;}
      }
     }
    switch(nm) //Select the object to be adjusted
      { case 1:WR_data_1602(0xc0,0);WR_data_1602('H',1);process(temp_high);key_temphigh();break;
     case 2:WR_data_1602(0xc0,0);WR_data_1602('L',1);process(temp_low);key_templow();break;
     case 3:WR_data_1602(0xc0,0);WR_data_1602(' ',1);process(temp);nm=0;k=0;break;
     default :break;
     }
  
 /* if(key_1==0) //OK and Exit
     {Delay(3);
       if(key_1==0)
      {while(!key_1);
        nm=0;k=0;WR_data_1602(0xc1,0);process(temp);
       }
    } */
       if(k==0)
   {continue;}//End loop 
    }
  }

 void baojing()
   {
   if(temp>temp_high-5) {P04=1;P05=0;P06=1;} // Upper limit 25
      else if(temp          else {P04=1;P05=1;P06=0;}
      } 

void init_1602(void) //Initialization
{
 WR_data_1602(0x38,0);
 WR_data_1602(0x01,0);
 WR_data_1602(0x06,0);
 WR_data_1602(0x0c,0);
}


void WR_data_1602(unsigned char adata,unsigned char i) //write data i=1,write instruction i=0
{
 P2=adata;
 RS=i;
 RW=0;
 E=1;
 delay_1602(1);
 E=0;
 RW=1;
 RS=0;  
}


void process(unsigned int shuju) //Display data processing
{
 unsigned int a,b,c,d;
 a=shuju/1000;
 b=(shuju%1000)/100;
 c=(shuju%100)/10;
 d =shuju%10;
 if(a==0&&b!=0){WR_data_1602(0xc2,0);
 WR_data_1602(*(pnum+b),1);
 WR_data_1602(*(pnum+c),1);
 WR_data_1602(' .',1); 
 WR_data_1602(*(pnum+d),1);
 WR_data_1602('C',1);}
 if(a==0&&b==0){WR_data_1602(0xc2,0); 
 WR_data_1602(*(pnum+c),1);
 WR_data_1602('.',1); WR_data_1602 
 (*(pnum+d),1);
 WR_data_1602('C',1);
 WR_data_1602(' ',1); }
 //WR_data_1602(0xc1,0); //Displayed in the second line
 //WR_data_1602(' ',1);
 //WR_data_1602(*(pnum+a),1);
// WR_data_1602(*(pnum+b ),1);
 //WR_data_1602(*(pnum+c),1);
// WR_data_1602('.',1); 
// WR_data_1602(*(pnum+d),1);
// WR_data_1602('C ',1);
 
 delay_1602(1);  
}

 

void main() //Main function
{
 unsigned int mm;
 init_1602(); //Initialization
 start_init();
 mm=GetResult();
 temp_high=250;
 temp_low=90;
 while(1)
 { 
  key_scan();
     baojing() ;
  ccc=GetResult();
  temp=ccc*5.0;
  process(temp);
  delay_1602(10); 
 }
}

Reference address:51 MCU + DHT11 temperature control program - can set temperature value

Previous article:STC89C51RC 40C-PDIP name meaning of each part
Next article:51 single chip microcomputer frequency meter Proteus simulation design (with program circuit)

Recommended ReadingLatest update time:2024-11-17 06:46

Based on 51 single chip microcomputer + DHT11 temperature and humidity + LCD1602 display
DHT11 temperature and humidity related introduction DHT11 Product Overview The DHT11 digital temperature and humidity sensor is a temperature and humidity composite sensor with calibrated digital signal output. It uses dedicated digital module acquisition technology and temperature and humidity sensing technology to e
[Microcontroller]
Based on 51 single chip microcomputer + DHT11 temperature and humidity + LCD1602 display
51 MCU based DHT11 temperature and humidity sensor
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 reg51.h #include "lcd.h" #include intrins.h #include stdio.h   sbit Temp_d
[Microcontroller]
51 MCU based 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号