欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3991|回復: 4
打印 上一主題 下一主題
收起左側

DS18b20 PID水溫控制的單片機程序源碼

[復制鏈接]
跳轉到指定樓層
樓主
ID:201463 發表于 2017-5-16 18:01 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
DS18B20+51單片機+PID

單片機源程序如下:
  1. /************* main.c ******************/
  2. #include <reg51.h>
  3. #define uchar unsigned char
  4. #define uint  unsigned int
  5. #include "18b20.c"
  6. #include<pid.c>


  7. uchar count,high_time;
  8. uchar set;
  9. uchar code dis_7[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
  10. main()
  11. {
  12.   uint t;
  13.   TMOD=0x01;        
  14.   TH0 =0x20;
  15.   TL0 =0x00;
  16.   EA  = 1;
  17.   ET0 = 1;
  18.   TR0 = 1;   
  19.   set=32;  

  20.   init_pid();
  21.   while(1)
  22.   {
  23.     t=ReadTemperature();
  24.         if(t>999)
  25.         {
  26.           P0=dis_7[t/1000];
  27.           P3=0xfe;
  28.           delay(200);
  29.           P0=dis_7[(t%1000)/100];
  30.           P3=0xfd;
  31.           delay(200);        
  32.         }
  33.         else
  34.         {
  35.            P0=dis_7[t/100];
  36.            P3=0xfd;
  37.            delay(200);           
  38.         }
  39.         P0=(dis_7[(t%100)/10]&0x7f);
  40.         P3=0xfb;
  41.         delay(200);        
  42.         P0=dis_7[t%10];
  43.         P3=0xf7;
  44.         duty_cycle(t);
  45.         }        
  46. }

  47. //---------------------------------------------------------------

  48. void t0_int(void) interrupt 1                //PWM波輸出
  49. {
  50.         if(++count<=(high_time))
  51.      SWH=0;
  52.    else if(count<=100)
  53.      SWH=1;
  54.    else
  55.       count=0;
  56.    TH0=0x10;  //定時器初始化
  57.    TL0=0x10;  
  58. }

  59. /************* pid.c ******************/
  60. uchar set;                     //溫度初始值
  61. uint rout;                       // PID Response (Output)
  62. uchar high_time,count=0;         //占空比調節參數
  63. /*************PID**********************************/
  64. struct PID {
  65.             uint SetPoint;      // 設定目標 Desired Value
  66.             uint Proportion;    // 比例常數 Proportional Const
  67.             uint Integral;      // 積分常數 Integral Const
  68.             uint Derivative;    // 微分常數 Derivative Const
  69.       signed int LastError;     // Error[-1]
  70.       signed int PrevError;     // Error[-2]
  71.       signed int SumError;      // Sums of Errors
  72.            };                                             
  73. struct PID spid;                // PID Control Structure

  74. /****************pid初始化*********************/
  75. void init_pid()
  76. {
  77.         high_time=50;
  78.         spid.Proportion = 23;       // Set PID Coefficients
  79.         spid.Integral = 2;
  80.         spid.Derivative =6;
  81.         spid.SetPoint = set;      // Set PID Setpoint
  82. }
  83. /***************************PID算法**************************/
  84. unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint )
  85. {
  86.         signed int dError,Error;
  87.         Error = pp->SetPoint - NextPoint;           // 偏差
  88.         pp->SumError += Error;                      // 積分
  89.         dError = pp->LastError - pp->PrevError;     // 當前微分
  90.         pp->PrevError = pp->LastError;                          
  91.         pp->LastError = Error;
  92.         return (pp->Proportion * Error+ pp->Integral * pp->SumError        + pp->Derivative * dError);
  93. }
  94. /********************PID控制占空比*************************/
  95. //high_time表示高電平數
  96. void duty_cycle(uint t)                               // 占空比
  97. {  
  98.     uchar s;
  99.         t=t/10;   
  100.         s=set;
  101.         if(s>t)
  102.         {
  103.                 if(s-t>2)     
  104.                         high_time=100;                  
  105.                 else
  106.                 {        
  107.                     rout = PIDCalc ( &spid,t );   // Perform PID Interation                                                        
  108.                         if(high_time<=100)
  109.                           high_time=(uchar)(rout/600);
  110.                 else
  111.                       high_time=100;                                
  112.             }
  113.      }
  114.          else
  115.          {
  116.                   high_time=0;
  117.         }

  118. }                 


  119. /************* 18b20.c ******************/
  120. sbit DQ   = P2^7;              //定義DS18B20數據線
  121. sbit SWH  = P2^4;                            //PWM開關
  122. sbit BEEP = P2^1;          //蜂鳴器
  123. void delay(unsigned int t)
  124. {
  125.   while(t--)
  126.    ;
  127. }
  128. void Init_DS18B20(void)//初始化ds1820
  129. {
  130.         unsigned char x=0;
  131.         DQ = 1;    //DQ復位
  132.         delay(8);  //稍做延時
  133.         DQ = 0;    //單片機將DQ拉低
  134.         delay(80); //精確延時 大于 480us
  135.         DQ = 1;    //拉高總線
  136.         delay(14);
  137.         x=DQ;      //稍做延時后 如果x=0則初始化成功 x=1則初始化失敗
  138.         delay(20);
  139. }
  140. /******************************************************************************/
  141. unsigned char ReadOneChar(void)//讀一個字節
  142. {
  143.         unsigned char i=0;
  144.         unsigned char dat = 0;
  145.         for (i=8;i>0;i--)
  146.         {
  147.                 DQ = 0; // 給脈沖信號
  148.                 dat>>=1;
  149.                 DQ = 1; // 給脈沖信號
  150.                 if(DQ)
  151.                 dat|=0x80;
  152.                 delay(4);
  153.         }
  154.         return(dat);
  155. }

  156. /******************************************************************************/
  157. void WriteOneChar(unsigned char dat)//寫一個字節
  158. {
  159.         unsigned char i=0;
  160.         for (i=8; i>0; i--)
  161.         {
  162.                 DQ = 0;
  163.                 DQ = dat&0x01;
  164.             delay(5);
  165.                 DQ = 1;
  166.                 dat>>=1;
  167.         }
  168. }
  169. /******************************************************************************/
  170. uint ReadTemperature(void)//讀取溫度
  171. {
  172. ……………………

  173. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
DS18b20 PID 水溫控制.rar (1.9 KB, 下載次數: 145)


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

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

使用道具 舉報

沙發
ID:227434 發表于 2017-8-15 09:41 | 只看該作者
樓主這個能正常使用嗎?那個beep功能怎么沒有?
回復

使用道具 舉報

板凳
ID:326025 發表于 2018-5-10 17:22 | 只看該作者
學習一下
回復

使用道具 舉報

地板
ID:278230 發表于 2018-5-12 14:52 | 只看該作者

學習一下
回復

使用道具 舉報

5#
ID:515488 發表于 2019-4-18 14:55 | 只看該作者
這個能用啊
回復

使用道具 舉報

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

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表