找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3948|回复: 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 单片机教程网

快速回复 返回顶部 返回列表