專注電子技術學習與研究
當前位置:單片機教程網(wǎng) >> MCU設計實例 >> 瀏覽文章

51單片機+DHT11溫控程序-可設置溫度值

作者:huqin   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2014年01月26日   【字體:

基于51單片機的溫濕度測量控制程序,使用DHT11數(shù)字傳感器,用1602液晶屏顯示,按鍵定義如下:
sbit key_1=P0^1;  //確定按鍵
sbit key_2=P0^2;  //按鍵加
sbit key_3=P0^3;  // 按鍵減
本程序中用到的頭文件 12c5a.h下載: http://www.torrancerestoration.com/f/12c5a.rar
#include "12c5a.H"
#include "intrins.h"

unsigned char code num[]={"0123456789"};//顯示字符存儲
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;  //數(shù)據(jù)命令選擇端
sbit RW=P3^5;  //讀寫控制端
sbit E=P3^6;   //使能信號控制端
//sbit key_0=P0^0;  //設置按鍵
sbit key_1=P0^1;  //確定按鍵
sbit key_2=P0^2;  //按鍵加
sbit key_3=P0^3;  // 按鍵減
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);   //顯示在第一行
 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);   //顯示在第一行
 for(i=0;i<11;i++)  
  WR_data_1602(t_char[i],1);  
}


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


void key_temphigh()    // 溫度上限判斷
 { if(key_2==0)  //進行加判斷
    { Delay(3);
        if(key_2==0)
       { while(!key_2);
      temp_high+=5;
      if(temp_high==995)
      {temp_high=5;}
       }
    }
   if(key_3==0)  //進行減判斷
      { Delay(3);
        if(key_3==0)
       { while(!key_3);  //等待松手
          temp_high-=5;  //上限報警溫度每次減0。5
       if(temp_high==0)
        {temp_high=990;}
       }
      }
    }

void key_templow()    // 溫度下限判斷
 { if(key_2==0)  //進行加判斷
    { Delay(3);
        if(key_2==0)
       { while(!key_2);
      temp_low+=5;
      if(temp_low==995)
      {temp_low=5;}
       }
    }
   if(key_3==0)  //進行減判斷
      { Delay(3);
        if(key_3==0)
       { while(!key_3);  //等待松手
          temp_low-=5;  // 上限溫度每次減0。5
       if(temp_low== 0)
        {temp_low=990;}
       }
      }
    }

void key_scan()
  { unsigned char nm=0,k;
     if(key_1==0)   //檢測設置按鍵,判斷是否進行設置
      { if(key_1==0)
    { Delay(100) ;
     while(!key_1);
       k=1;nm++;
     }
    }
   while(k)      //進入調(diào)整界面,
   {  if(key_1==0)
       { Delay(3);
       if(key_1==0)
     { while(!key_1);
        nm++;  //調(diào)整哪個的參數(shù)
     if(nm==4){nm=1;}
      }
     }
    switch(nm)   //選擇調(diào)整的對象
      { 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)       //確定并退出
     {Delay(3);
       if(key_1==0)
      {while(!key_1);
        nm=0;k=0;WR_data_1602(0xc1,0);process(temp);
       }
    } */
       if(k==0)
   {continue;}//結束循環(huán)
    }
  }

 void baojing()
   {
   if(temp>temp_high-5) {P04=1;P05=0;P06=1;}     // 上限25
      else if(temp<temp_low+5) {P04=0;P05=1;P06=1;}    // 下限20
         else  {P04=1;P05=1;P06=0;}
      } 

void init_1602(void)  //初始化
{
 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) //寫數(shù)據(jù)i=1,寫指令i=0
{
 P2=adata;
 RS=i;
 RW=0;
 E=1;
 delay_1602(1);
 E=0;
 RW=1;
 RS=0;  
}


void  process(unsigned int shuju) //顯示數(shù)據(jù)處理
{
 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);    //顯示在第二行
 //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()   //主函數(shù)
{
 unsigned int mm;
 init_1602();  //初始化
 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); 
 }
}

 

關閉窗口

相關文章