<li id="8g3ty"><tbody id="8g3ty"><th id="8g3ty"></th></tbody></li>
    <label id="8g3ty"><samp id="8g3ty"></samp></label>
  • <span id="8g3ty"></span>

    1. <center id="8g3ty"><optgroup id="8g3ty"></optgroup></center>
    2. <bdo id="8g3ty"><meter id="8g3ty"><bdo id="8g3ty"></bdo></meter></bdo><center id="8g3ty"><optgroup id="8g3ty"></optgroup></center>
      <label id="8g3ty"><samp id="8g3ty"></samp></label>

    3. 電子開發(fā)網(wǎng)

      電子開發(fā)網(wǎng)電子設(shè)計(jì) | 電子開發(fā)網(wǎng)Rss 2.0 會(huì)員中心 會(huì)員注冊
      搜索: 您現(xiàn)在的位置: 電子開發(fā)網(wǎng) >> 電子開發(fā) >> 單片機(jī) >> 正文

      51單片機(jī)的液晶顯示溫度計(jì)程序

      作者:佚名    文章來源:本站原創(chuàng)    點(diǎn)擊數(shù):    更新時(shí)間:2011-5-22

      #include <reg51.h>
      #include <intrins.h>

      sbit  RST   = P2^0;
      sbit  CLK   = P2^1;
      sbit  DQ    = P2^2;
      sbit  TSOR  = P2^3;
      sbit  ALERT =P2^4;
      sbit  RS    = P2^7;
      sbit  RW    = P2^6;
      sbit  EN    = P2^5;
      /*------------------------------------------全局變量-------------------------------------------------------*/
      static unsigned char temp1,temp2;         //溫度值的整數(shù)部分、小數(shù)部分
      static unsigned char pos,posset;           //數(shù)字電位器電位值、設(shè)定值
      static unsigned char min,sec;              //分鐘、秒
      static unsigned char count;                //Timer0中斷計(jì)數(shù)
      static unsigned char minset;               //設(shè)定的分鐘數(shù)
      static unsigned char status1,status2;  //狀態(tài)標(biāo)志
      bit   stop,timeover;     //定時(shí)停止、結(jié)束
      static char line0[] ="     00:00      ";
      static char line1[] ="   . C         W";

      /*-------------------------------------------------------------------------------------------------------------*/
      void          InitInterupt();
      void          KeyboardDelay();
      /*-------------------------------------------LCD驅(qū)動(dòng)函數(shù)------------------------------------------------*/
      void          DelayL();
      void     DelayS();
      void     WriteCommand(unsigned char c);
      void     WriteData(unsigned char c);
      void     ShowChar(unsigned char pos,unsigned char c);
      void     ShowString(unsigned char line,char *ptr);
      void     InitLcd();
      /*----------------------------------------------鍵盤-程序--------------------------------------------------*/
      unsigned char GetKey();
      /*---------------------------------------------數(shù)字溫度計(jì)驅(qū)動(dòng)-------------------------------------------*/
      void          ChangePos(bit sel,unsigned char pos1,unsigned char pos2);
      /*------------------------------------------溫度傳感器驅(qū)動(dòng)----------------------------------------------*/
      void          Delay15();
      void          Delay60();
      void          Delay100ms();
      void          Write0TS();
      void          Write1TS();
      bit           ReadTS();
      void          ResetTS();
      void          WriteByteTS(unsigned char byte);
      unsigned char  ReadByteTS();
      void          InitTS();
      void          GetTempTS();
      /*-------------------------------------------------主程序---------------------------------------------------*/
      void main (void) {
           char code str1[]  ="  Hello World!  ";
           char code str2[]  ="   2002-10-20   ";
        unsigned char i;

           SP=0x50;
        ALERT=0;                 //報(bào)警燈滅
        TSOR=1;                 //1-wire總線釋放

           DelayL();
        InitLcd();               //初始化LCD
        DelayL();
           ShowString(0,str1);    //啟動(dòng)畫面
           ShowString(1,str2);
        for(i=0;i<15;i++)
             Delay100ms();
           InitInterupt();         //初始化中斷設(shè)置

           minset=10;               //缺省定時(shí)10分鐘
           posset=0;                //缺省電位器值0
        min=minset;     //初始化數(shù)據(jù)
        pos=posset;
        sec=0;           
           count=0;
           P1=0xF0;
        status1=0;
        status2=0;
        stop=1;
        timeover=0;

           ChangePos(0,255-pos,255-pos);     //設(shè)置電位器

        InitTS();                //初始化溫度計(jì)
        while(1)      //循環(huán)顯示溫度值
        {
             GetTempTS();
          line1[0]=0x20;
          i=temp1;
      if(i>39)       //超過40攝氏度,告警燈亮
       ALERT=1;
          if(i>99)      //超過100攝氏度,顯示溫度的百位
          {
            line1[0]=0x31;   
            i-=100;
             }
             line1[1]=i/10+0x30;     //顯示溫度的十位
          line1[2]=i%10+0x30;    //顯示個(gè)位
          line1[4]=temp2+0x30;   //顯示小數(shù)位
          if(timeover)               //若定時(shí)結(jié)束,則電位器緩慢復(fù)0
          {
               for(;pos>0;pos--)
            {
               ChangePos(0,255-pos,255-pos);   
               _nop_();
         _nop_();
            }
         timeover=0;
         posset=0;
          }
             if(pos>posset)       //若按鍵修改電位器位置
          {
            for(;pos>posset;pos--)   //則緩變到設(shè)定值
               {
               ChangePos(0,255-pos,255-pos);   
               _nop_();
            _nop_();
            }
            ChangePos(0,255-pos,255-pos);
          }
          else if(pos<posset)  
          {
            for(;pos<posset;pos++)
               {
               ChangePos(0,255-pos,255-pos);   
               _nop_();
            _nop_();
            }
            ChangePos(0,255-pos,255-pos);
          }
             i=pos; 
          line1[9]=0x20;     //顯示電位器等級值
          if(i>99)
          {
            line1[9]=i/100+0x30;
               i=i%100;
          }
             line1[10]=i/10+0x30;
             line1[11]=i%10+0x30;
          ShowString(1,line1);
          line0[5]=min/10+0x30;   //顯示時(shí)間
          line0[6]=min%10+0x30;
             line0[8]=sec/10+0x30;
          line0[9]=sec%10+0x30;
          ShowString(0,line0);   
             Delay100ms();
        }
      }

      void InitInterupt()
      {
        TMOD=0x21;               //初始化中斷設(shè)置
        TL1=0xFD;
        TH1=0xFD;
        PX0=1;
        EA=1;                
        ES=1;                
        PCON=0;   
        TR1=1;
        SCON=0x50;


        TL0=0x00;                 //定時(shí)0.05m
        TH0=0x4C; 
        ET0=1;

        EX0=1;               
        IT0=1;
      }

      void KeyboardDelay()        //按鍵中斷延時(shí)
      {
       unsigned char i,j;
       i=0x40;
       j=0xFF;
       while(i--)
        while(j--);
      }

      /*--------------------------------------------中斷處理-----------------------------------------------------*/
      Int0_process() interrupt 0 using 0
      {
        unsigned char key;
        unsigned char keycode[]= "TP";
        unsigned char step[3]={1,2,5};
        EA=0;
        key=GetKey();                //獲得按鍵值
        switch(key)
        {
         case 0:
           stop=!stop;
        min=minset;
           sec=0;
           break;
         case 1:
         case 2:
         case 3:
           if(stop)
        {
         minset+=step[key-1];
        if(minset>60)
          minset=0;
        min=minset;
        }
           break;
         case 5:
         case 6:
         case 7:
           if(stop)
        { 
         minset-=step[key-5];
        if(minset>60)
          minset=0;
        min=minset;
        }
           break;
         case 9:
         case 10:
         case 11:
         posset+=step[key-9];
           break;
         case 13:
         case 14:
         case 15:
         posset-=step[key-13];
           break;
         default:
           break;
        }
        TR0=!stop;
        KeyboardDelay();
        P1=0xF0;
        EA=1;
      }

      Timer0_process() interrupt 1 using 0
      {
        EA=0;
        TR0=0;
        TL0=0x00;
        TH0=0x4C;
        count++;
        if(count==20)      //如果到累計(jì)定時(shí)到達(dá)1s
        {
         if(sec==0)       //定時(shí)處理
         {
           if(min==0)      //總定時(shí)到,則置結(jié)束標(biāo)志
          timeover=1;
           else
        {
            min--;
            sec=59;
           } 
         }
         else
           sec--;
         count=0;
        } 
        TR0=1;
        EA=1;
      }
      /*--------------------------------------LCD驅(qū)動(dòng)子程序--------------------------------------------------*/
      void DelayL()                          
      {unsigned char i,j;
       i=0xF0;
       j=0xFF;
       while(i--)
        while(j--);
      }

      void DelayS()             
      {
       unsigned char i;
       i=0x1F;
       while(i--);
      }

      void WriteCommand(unsigned char c)      
      {
       DelayS();
       EN=0;
       RS=0;
       RW=0;
       _nop_();
       EN=1;
       P0=c;
       EN=0;
      }

      void WriteData(unsigned char c)         
      {
       DelayS();
       EN=0;
       RS=1;
       RW=0;
       _nop_();
       EN=1;
       P0=c;
       EN=0;
       RS=0;
      }

      void ShowChar(unsigned char pos,unsigned char c)
      {
       unsigned char p;
       if(pos>=0x10)
          p=pos+0xB0;
       else
          p=pos+0x80;
       WriteCommand(p);
       WriteData(c);
      }

      void ShowString(unsigned char line,char *ptr)   
      {
       unsigned char l,i;
       l=line<<4;
       for(i=0;i<16;i++)
         ShowChar(l++,*(ptr+i));
      }    

      void InitLcd()
      {
       DelayL();
       WriteCommand(0x38);
       WriteCommand(0x38);
       WriteCommand(0x06);
       WriteCommand(0x0C);
       WriteCommand(0x01);
       WriteCommand(0x80);
      }    
      /*---------------------------------------------鍵盤子程序-------------------------------------------------*/
      unsigned char GetKey()
      {
        unsigned k,t,i,j;
        k=P1;
        k=k&0xF0;
        i=0;
        while((k&0x10)&&i<4)
        {
       i++;
       k=k>>1;
        }  
        k=0x01;
        j=0;
        while(j<4)          
        {
          P1=0xFF^k;
       _nop_();
          t=P1;
          t=t^0xFF;
          t=t&0xF0;
          if(t)
         break;
          j++;
       k=k<<1;
        }
        k=j*4+i;
        return k;
      }
      /*-----------------------------------------數(shù)字溫度計(jì)驅(qū)動(dòng)子程序--------------------------------------*/
      void  ChangePos(bit sel,unsigned char pos1,unsigned char pos2)
      {  unsigned char i;
           RST=0;
           DQ=0;
           CLK=0;
        RST=1;
        DQ=sel;
           _nop_();
           CLK=1;
           _nop_();
        CLK=0;
           for(i=0;i<8;i++)
        {
        if(pos1&0x80)
             DQ=1;
        else
          DQ=0;
           _nop_();
           CLK=1;
           _nop_();
        CLK=0;
        pos1=pos1<<1;
       }
       for(i=0;i<8;i++)
       {
        if(pos2&0x80)
             DQ=1;
        else
          DQ=0;
           _nop_();
           CLK=1;
           _nop_();
           CLK=0;
        pos2=pos2<<1;
       }
          RST=0;
      }

      /*------------------------------------------溫度傳感器子程序-------------------------------------------*/
      void Delay100ms()              //延時(shí)100ms
      {
       unsigned  char i,j,k;
       for(i=0;i<8;i++)
         for(j=0;j<25;j++)
           for(k=0;k<250;k++);
      }

      void Delay15()                 //延時(shí)15us
      {
       unsigned  char i;
       for(i=0;i<8;i++);
      }

      void Delay60()                 //延時(shí)60us
      {
       unsigned  char i;
       for(i=0;i<30;i++);
      }

      void Write0TS()                //寫bit 0
      {
           TSOR=1;
           TSOR=0;
        Delay15();
        Delay15();
        Delay15();
        Delay15();
           TSOR=1;
        _nop_();
        _nop_();
      }

      void Write1TS()                 //寫bit 1
      {
           TSOR=1;
           TSOR=0;
           _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
           TSOR=1;
           _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
           Delay15();
           Delay15();
           Delay15();
      }

      bit ReadTS()
      {
       bit b;
           TSOR=1;
           TSOR=0;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
           TSOR=1;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        _nop_();
           b=TSOR;           
           Delay15();
           Delay15();
           Delay15();
        _nop_();
        _nop_();
        return b;
      }

      void ResetTS()                //復(fù)位
      {unsigned char i;

         TSOR=1;
         TSOR=0;
         for(i=0;i<8;i++)
           Delay60();
         TSOR=1;             
         while(TSOR);        
         for(i=0;i<8;i++)
           Delay60();
      }

      void WriteByteTS(unsigned char byte)   //寫一個(gè)字節(jié)(byte)
      {unsigned char i;
       for(i=0;i<8;i++)
       {
         if(byte&0x01)
           Write1TS();
         else
           Write0TS();
         byte=byte>>1;
       }
      }

      unsigned char ReadByteTS()          //讀一個(gè)字節(jié)(byte)
      {
       unsigned char i,j;
       bit           b;
       j=0;
       for(i=0;i<8;i++)
       {
         b=ReadTS();
         if(b)
           j+=1;
         j=_cror_(j,1);
       }
       return j;
      }
                
      void InitTS()                 //初始化溫度轉(zhuǎn)換
      {
           ResetTS();              
        WriteByteTS(0xCC);      
        WriteByteTS(0x4E);      
        WriteByteTS(0x64);      
           WriteByteTS(0x8A);     
           WriteByteTS(0x1F);     
      }

      void GetTempTS()                //獲取溫度
      {
           ResetTS();              
        WriteByteTS(0xCC);      
        WriteByteTS(0x44);      
        Delay100ms();          
           ResetTS();             
        WriteByteTS(0xCC);     
         WriteByteTS(0xBE);     
          
        temp2=ReadByteTS();
        temp1=ReadByteTS();
        ReadByteTS();
        ReadByteTS();
        ReadByteTS();
        ReadByteTS();
        ReadByteTS();
        ReadByteTS();
        ReadByteTS();
        temp1=temp1<<4;
        temp1+=(temp2&0xF0)>>4;
         temp2=(temp2&0x0F)?5:0;
      }
      液晶顯示溫度計(jì)程序

      Tags:51單片機(jī),c語言,電子溫度計(jì)  
      責(zé)任編輯:admin
      請文明參與討論,禁止漫罵攻擊,不要惡意評論、違禁詞語。 昵稱:
      1分 2分 3分 4分 5分

      還可以輸入 200 個(gè)字
      [ 查看全部 ] 網(wǎng)友評論
      關(guān)于我們 - 聯(lián)系我們 - 廣告服務(wù) - 友情鏈接 - 網(wǎng)站地圖 - 版權(quán)聲明 - 在線幫助 - 文章列表
      返回頂部
      刷新頁面
      下到頁底
      晶體管查詢
      主站蜘蛛池模板: 国产成人综合久久精品红| 天天色天天操综合网| 亚洲国产亚洲综合在线尤物| 五月婷婷久久综合| 亚洲人成网站999久久久综合| 久久丁香五月天综合网| 2020久久精品亚洲热综合一本| 亚洲国产欧洲综合997久久| 精品综合久久久久久99| 久久久久久久综合日本| 色诱久久久久综合网ywww| 婷婷色中文字幕综合在线| 亚洲精品综合久久| 色天使亚洲综合在线观看| 少妇人妻综合久久中文字幕| 伊人久久大香线蕉综合热线| 91精品国产综合久久精品| 狠狠色综合网久久久久久| 亚洲国产成人久久综合碰碰动漫3d| 亚洲AV综合永久无码精品天堂 | 国产成人综合日韩精品无码不卡| 国产精品国色综合久久| 精品综合久久久久久99| 色欲色香天天天综合网站免费| 国产婷婷综合在线视频中| 激情婷婷成人亚洲综合| 色综合网站国产麻豆| 六月婷婷国产精品综合| 色爱无码AV综合区| 久久综合香蕉国产蜜臀AV| 久久婷婷五月国产色综合| 久久综合精品不卡一区二区| 伊人色综合久久大香| 亚洲综合无码精品一区二区三区| 婷婷久久综合九色综合绿巨人| 在线精品国产成人综合| 精品国产第一国产综合精品| 亚洲国产品综合人成综合网站| 亚洲成AV人综合在线观看| 成人综合激情另类小说| 久久婷婷午色综合夜啪 |