找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3280|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

ds18b20寫的溫度控制器,設(shè)定溫度只能到個(gè)位數(shù),怎么改能控制到小數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

單片機(jī)源程序如下:
  1. void key_pros()  //按鍵處理函數(shù)
  2. {
  3.         if(k1==0)
  4.         {
  5.                 delay(1000); //按鍵消抖
  6.                 if(k1==0)                           //按鍵1菜單
  7.                 {
  8.                         mode++;
  9.                         if(mode==5)mode=0;
  10.                         wrc(0x01);
  11.                 }
  12.                 while(!k1);
  13.         }
  14.         if(mode==1)                  //對(duì)溫度上限設(shè)定
  15.         {
  16.                 if(k2==0)                  //加        //按鍵2加
  17.                 {
  18.                         delay(1000);   //按鍵消抖
  19.                         if(k2==0)
  20.                         {
  21.                                 temph++;
  22.                                 if(temph>=80)temph=80;
  23.                         }
  24.                         while(!k2);
  25.                 }
  26.                 if(k3==0)           //減
  27.                 {
  28.                         delay(1000);
  29.                         if(k3==0)                  //按鍵3減
  30.                         {
  31.                                 temph--;
  32.                                 if(temph<=0)temph=0;
  33.                         }
  34.                         while(!k3);
  35.                 }
  36.         }
  37.         if(mode==2)                  //對(duì)溫度下限設(shè)定
  38.         {
  39.                 if(k2==0)                  //加
  40.                 {
  41.                         delay(1000);
  42.                         if(k2==0)
  43.                         {
  44.                                 templ++;
  45.                                 if(templ>=80)templ=80;
  46.                         }
  47.                         while(!k2);
  48.                 }
  49.                 if(k3==0)           //減
  50.                 {
  51.                         delay(1000);
  52.                         if(k3==0)
  53.                         {
  54.                                 templ--;
  55.                                 if(templ<=0)templ=0;
  56.                         }
  57.                         while(!k3);
  58.                 }
  59.         }
  60.         if(mode==3)                  //對(duì)濕度上限設(shè)定
  61.         {
  62.                 if(k2==0)                  //加
  63.                 {
  64.                         delay(1000);
  65.                         if(k2==0)
  66.                         {
  67.                                 humih++;
  68.                                 if(humih>=80)humih=80;
  69.                         }
  70.                         while(!k2);
  71.                 }
  72.                 if(k3==0)           //減
  73.                 {
  74.                         delay(1000);
  75.                         if(k3==0)
  76.                         {
  77.                                 humih--;
  78.                                 if(humih<=0)humih=0;
  79.                         }
  80.                         while(!k3);
  81.                 }
  82.         }
  83.         if(mode==4)                  //對(duì)濕度下限設(shè)定
  84.         {
  85.                 if(k2==0)                  //加
  86.                 {
  87.                         delay(1000);
  88.                         if(k2==0)
  89.                         {
  90.                                 humil++;
  91.                                 if(humil>=80)humil=80;
  92.                         }
  93.                         while(!k2);
  94.                 }
  95.                 if(k3==0)           //減
  96.                 {
  97.                         delay(1000);
  98.                         if(k3==0)
  99.                         {
  100.                                 humil--;
  101.                                 if(humil<=0)humil=0;
  102.                         }
  103.                         while(!k3);
  104.                 }
  105.         }
  106. }

  107. void lcd_init_display()           //LCD初始化顯示
  108. {
  109.         uint8 i;
  110.         for(i=0;i<5;i++)
  111.         {
  112.                 wrc(0x80+i);
  113.                 wrd(str1[i]);        
  114.         }

  115.         for(i=0;i<5;i++)
  116.         {
  117.                 wrc(0xc0+i);
  118.                 wrd(str2[i]);        
  119.         }
  120. }

  121. void data_pros()        //數(shù)據(jù)處理函數(shù)
  122. {
  123.         uint8 i;              
  124.         uint8 temp_buf[2],humi_buf[2];
  125.         uint8 temphbuf[2],templbuf[2],humihbuf[2],humilbuf[2];
  126.         float dio;        //測量溫度值
  127.         uint16 k;  //取小數(shù)點(diǎn)后兩位有效數(shù)字

  128.         tempvalue=readtemp();        //讀取溫度
  129.         DHT11_Read_Data(&temp,&humi);
  130.         temp_buf[0]=temp/10+0x30;          //LCD顯示溫度做準(zhǔn)備
  131.         temp_buf[1]=temp%10+0x30;

  132.         humi_buf[0]=humi/10+0x30;          //LCD顯示濕度做準(zhǔn)備
  133.         humi_buf[1]=humi%10+0x30;
  134.         
  135.         dio=a*0.0625;
  136.         k=dio*10000;//取小數(shù)點(diǎn)后兩位有效數(shù)字

  137.         temphbuf[0]=temph/10+0x30;           //LCD顯示上下限溫度做準(zhǔn)備
  138.         temphbuf[1]=temph%10+0x30;
  139.         templbuf[0]=templ/10+0x30;
  140.         templbuf[1]=templ%10+0x30;

  141.         humihbuf[0]=humih/10+0x30;           //LCD顯示上下限濕度做準(zhǔn)備
  142.         humihbuf[1]=humih%10+0x30;
  143.         humilbuf[0]=humil/10+0x30;
  144.         humilbuf[1]=humil%10+0x30;

  145.         if(mode==0)                   //測量溫度,濕度顯示
  146.         {
  147.                 lcd_init_display();                          //LCD顯示函數(shù)
  148.                 wrc(0x85);                                         //LCD顯示溫度位置
  149.                 wrd(num[tempvalue%100/10]);         //測量溫度十位顯示
  150.                 wrd(num[tempvalue%100%10]);         //測量溫度個(gè)位顯示
  151.                 wrd('.');
  152.                 wrd(num[k/1000]);  //取小數(shù)點(diǎn)后兩位有效數(shù)字
  153.                 wrd(0xdf);
  154.                 wrd('C');                 //測量溫度符號(hào)
  155.         
  156.                 for(i=0;i<2;i++)
  157.                 {
  158.                         wrc(0Xc5+i);           //LCD顯示濕度位置
  159.                         wrd(humi_buf[i]); //測量濕度十個(gè)位顯示                  
  160.                 }        
  161.                 for(i=0;i<3;i++)
  162.                 {
  163.                         wrc(0Xc7+i);         //LCD顯示符號(hào)位置
  164.                         wrd(str5[i]);        //測量濕度符號(hào)        %RH  
  165.                 }        
  166.         }
  167.         if(mode==1)                          //溫度上限顯示
  168.         {
  169.                 wrc(0x80);
  170.                 for(i=0;i<6;i++)
  171.                 {
  172.                         wrd(str6[i]);                  
  173.                 }
  174.                 wrd(temphbuf[0]);
  175.                 wrd(temphbuf[1]);                        
  176.         }
  177.         if(mode==2)                          //溫度下限顯示
  178.         {
  179.                 wrc(0x80);
  180.                 for(i=0;i<6;i++)
  181.                 {
  182.                         wrd(str7[i]);                  
  183.                 }
  184.                 wrd(templbuf[0]);
  185.                 wrd(templbuf[1]);                        
  186.         }
  187.         if(mode==3)                          //濕度上限顯示
  188.         {
  189.                 wrc(0x80);
  190.                 for(i=0;i<6;i++)
  191.                 {
  192.                         wrd(str8[i]);                  
  193.                 }
  194.                 wrd(humihbuf[0]);
  195.                 wrd(humihbuf[1]);                        
  196.         }
  197.         if(mode==4)                          //濕度下限顯示
  198.         {
  199.                 wrc(0x80);
  200.                 for(i=0;i<6;i++)
  201.                 {
  202.                         wrd(str9[i]);                  
  203.                 }
  204.                 wrd(humilbuf[0]);
  205.                 wrd(humilbuf[1]);                        
  206.         }
  207. }

復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:7485 發(fā)表于 2018-4-23 09:30 | 只看該作者
控制部分的溫度設(shè)定值擴(kuò)大10倍(假設(shè)你僅使用1位小數(shù)),實(shí)測溫度值也擴(kuò)大10倍然后進(jìn)行比較。顯示時(shí)在個(gè)位加上小數(shù)點(diǎn)即可。

如果有負(fù)數(shù)(零下)可以把設(shè)定值(上限制和下限值)加上50(假設(shè)你最低到零下50度)實(shí)測值也加上50進(jìn)行比較。
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表