找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 49714|回复: 24
收起左侧

51单片机PID温度控制程序

  [复制链接]
ID:73526 发表于 2015-2-11 15:52 | 显示全部楼层 |阅读模式
PID是比例,积分,微分的缩写. 比例调节作用:是按比例反应系统的偏差,系统一旦出现了偏差,比例调节立即产生调节作用用以减少偏差。比例作用大,可以加快调节,减少误差,但是过大的比例,使系统的稳定性下降,甚至造成系统的不稳定。积分调节作用:是使系统消除稳态误差,提高无差度。因为有误差,积分调节就进行,直至无差,积分调节停止,积分调节输出一常值。积分作用的强弱取决与积分时间常数Ti,Ti越小,积分作用就越强。反之Ti大则积分作用弱,加入积分调节可使系统稳定性下降,动态响应变慢。积分作用常与另两种调节规律结合,组成PI调节器或PID调节器。微分调节作用:微分作用反映系统偏差信号的变化率,具有预见性,能预见偏差变化的趋势,因此能产生超前的控制作用,在偏差还没有形成之前,已被微分调节作用消除。因此,可以改善系统的动态性能。在微分时间选择合适情况下,可以减少超调,减少调节时间。微分作用对噪声干扰有放大作用,因此过强的加微分调节,对系统抗干扰不利。此外,微分反应的是变化率,而当输入没有变化时,微分作用输出为零。微分作用不能单独使用,需要与另外两种调节规律相结合,组成PD或PID控制器。

程序如下:
  1. /***********************************************************************
  2.                      PID温度控制程序
  3. 程序说明:
  4.    系统上电后显示 “--温度”
  5.    表示需要先设定温度才开始进行温度检测
  6.    温度设定完毕后程序才开始进行PID温控
  7. ***********************************************************************/
  8. #include <reg52.h>
  9. #include <absacc.h>
  10. #include"DS18B20.H"
  11. #include"PID.H"
  12. #define uchar unsigned char
  13. #define uint unsigned int
  14. unsigned char code tab[]=
  15. {
  16.     0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF
  17. }
  18. ;
  19. /*个位0~9的数码管段码*/
  20. unsigned char code sao[]=
  21. {
  22.     0x7f,0xbf,0xdf,0xef
  23. }
  24. ;
  25. //扫描码
  26. uchar set=30,keyflag=1 ; //set初始化为30° keyflag为进入温度设定的标志位
  27. //4个按键使用说明
  28. sbit key_out=P1^0 ; //用于温度设定后的退出
  29. sbit key_up=P1^1 ; //设定温度加
  30. sbit key_down=P1^2 ; //设定温度减
  31. sbit key_in=P1^3 ; //在程序的运行中如需要重新设定温度 按下此键才能进入设置模式并且此时是停在温度控制的,按下key_out键后才表示设定完毕
  32. void Show_key();
  33. /***********************************************************/
  34. void delays(unsigned char k)
  35. {
  36.     unsigned char i,j ;
  37.     for(i=0;i<k;i++)
  38.     for(j=0;j<50;j++);
  39. }
  40. /*********************************************************
  41. //数码管显示函数
  42. P0口 作为数据口
  43. P2口的低四位作为扫描口

  44. 变量 x表示扫描
  45. d表示是否要加小数点 为1是 为0不加
  46. y表示传递的数值
  47. *********************************************************/
  48. LCD_disp_char(uchar x,bit d,uchar y)
  49. {
  50.     P2=0XFF ;
  51.     P0=0xFF ;
  52.     if(d==0)
  53.     P0=tab[y];
  54.     else
  55.     P0=tab[y]&0x7f ; //与上0x7f表示是否要加小数点
  56.     P2=sao[x]; //打开扫描端号
  57.    
  58. }
  59. /*********************************************************
  60. 按键扫描
  61. *********************************************************/

  62. void keyscan(void)
  63. {
  64.     if(key_in==0) //按键进入函数
  65.     {
  66.         delays(10);    //延时消抖 (以下同)
  67.         if(key_in==0)
  68.         {
  69.             while(key_in==0)
  70.             {
  71.                 Show_key(); //如果一直按着键不放 就一直显示在当前状态 (以下同)
  72.             }
  73.             keyflag=1 ; //按键标志位
  74.         }
  75.     }
  76.     /***********************/
  77.     if(key_out==0)   //按键退出
  78.     {
  79.         delays(10);
  80.         if(key_out==0)
  81.         {
  82.             while(key_out==0)
  83.             {
  84.                 Show_key();
  85.             }
  86.             keyflag=0 ;
  87.             set_temper=set ;
  88.         }
  89.     }
  90.     /*************************/
  91.     if(key_up==0)   //设定温度的加
  92.     {
  93.         delays(10);
  94.         if(key_up==0)
  95.         {
  96.             while(key_up==0)
  97.             {
  98.                 Show_key();
  99.             }
  100.             if(keyflag==1)
  101.             {
  102.                 set++;
  103.                 if(set>90) //如果大于90°就不在加
  104.                 set=90 ;
  105.             }
  106.         }
  107.     }
  108.     /*************************/
  109.     if(key_down==0)   //温度设定的减
  110.     {
  111.         delays(10);
  112.         if(key_down==0)
  113.         {
  114.             while(key_down==0)
  115.             {
  116.                 Show_key();
  117.             }
  118.             if(keyflag==1)
  119.             {
  120.                 set--;
  121.                 if(set<30) //温度减到30°时不在往下减
  122.                 set=30 ;
  123.             }
  124.         }
  125.     }
  126. }
  127. /*********************************************************************
  128. 按键按下时的显示函数
  129. ***********************************************************************/
  130. void Show_key()  
  131. {
  132.     output=1 ;
  133.     LCD_disp_char(3,0,10); //显示 -
  134.     delays(3);
  135.     LCD_disp_char(2,0,10); //显示- (表示温度设定 )
  136.     delays(3);
  137.     LCD_disp_char(1,0,set/10); //显示温度十位
  138.     delays(3);
  139.     LCD_disp_char(0,0,set%10); //显示温度个位
  140.     delays(3);
  141. }
  142. /*****************************************************************/
  143. void main()
  144. {
  145.     unsigned int tmp ;//声明温度中间变量  
  146.     unsigned char counter=0 ;
  147.     PIDBEGIN(); //PID参数的初始化
  148.     output=1 ; //关闭继电器输出
  149.     while(1)
  150.     {
  151.         keyscan();
  152.         if(keyflag)
  153.         {
  154.             Show_key(); //显示温度设定
  155.         }
  156.         else
  157.         {
  158.             if(counter--==0)
  159.             {
  160.                 tmp=ReadTemperature();//每隔一段时间读取温度值
  161.                 counter=20 ;
  162.             }
  163.             LCD_disp_char(3,0,tmp/1000);   //显示温度十位
  164.             delays(3);
  165.             LCD_disp_char(2,1,tmp/100%10); //显示温度个位
  166.             //显示小数点
  167.             delays(3);
  168.             LCD_disp_char(1,0,tmp/10%10); //显示温度小数后一位
  169.             delays(3);
  170.             LCD_disp_char(0,0,tmp%10);//显示温度小数后二位
  171.             delays(3);
  172.             P2=0XFF ;
  173.             P0=0xff ;
  174.             compare_temper(); //比较温度
  175.            
  176.            
  177.         }
  178.     }
  179. }

  180. /**********************************************************************************************************************************************/

  181. //PID算法温控C语言2008-08-17 18:58
  182. #ifndef _PID_H__
  183. #define _PID_H__

  184. #include<intrins.h>
  185. #include<math.h>
  186. #include<string.h>
  187. struct PID
  188. {
  189.     unsigned int SetPoint ;
  190.     // 设定目标 Desired Value
  191.     unsigned int Proportion ;
  192.     // 比例常数 Proportional Const
  193.     unsigned int Integral ;
  194.     // 积分常数 Integral Const
  195.     unsigned int Derivative ;
  196.     // 微分常数 Derivative Const
  197.     unsigned int LastError ;
  198.     // Error[-1]
  199.     unsigned int PrevError ;
  200.     // Error[-2]
  201.     unsigned int SumError ;
  202.     // Sums of Errors
  203. }
  204. ;
  205. struct PID spid ;
  206. // PID Control Structure
  207. unsigned int rout ;
  208. // PID Response (Output)
  209. unsigned int rin ;
  210. // PID Feedback (Input)


  211. sbit output=P1^4;
  212. unsigned char high_time,low_time,count=0 ;
  213. //占空比调节参数
  214. unsigned char set_temper ;

  215. void PIDInit(struct PID*pp)
  216. {
  217.     memset(pp,0,sizeof(struct PID)); //PID参数初始化全部设置为0
  218. }

  219. unsigned int PIDCalc(struct PID*pp,unsigned int NextPoint)
  220. {
  221.     unsigned int dError,Error ;
  222.     Error=pp->SetPoint-NextPoint ;
  223.     // 偏差
  224.     pp->SumError+=Error ;
  225.     // 积分
  226.     dError=pp->LastError-pp->PrevError ;
  227.     // 当前微分
  228.     pp->PrevError=pp->LastError ;
  229.     pp->LastError=Error ;
  230.     //比例
  231.     //积分项
  232.     return(pp->Proportion*Error+pp->Integral*pp->SumError+pp->Derivative*dError);
  233.     // 微分项
  234. }

  235. /***********************************************************
  236. 温度比较处理子程序
  237. ***********************************************************/
  238. void compare_temper()
  239. {
  240.     unsigned char i ;
  241.     //EA=0;
  242.     if(set_temper>temper)
  243.     {
  244.         if(set_temper-temper>1)
  245.         {
  246.             high_time=100 ; //大于1°不进行PID运算
  247.             low_time=0 ;
  248.         }
  249.         else
  250.         {   //在1°范围内进行PID运算
  251.             for(i=0;i<10;i++)
  252.             {
  253.                 //get_temper();
  254.                 rin=s;
  255.                 // Read Input
  256.                 rout=PIDCalc(&spid,rin); //执行PID运算
  257.                 // Perform PID Interation
  258.             }
  259.             if(high_time<=100) //限制最大值
  260.             high_time=(unsigned char)(rout/800);
  261.             else
  262.             high_time=100;
  263.             low_time=(100-high_time);
  264.         }
  265.     }
  266. /****************************************/
  267.     else if(set_temper<=temper) //当实际温度大于设置温度时
  268.     {
  269.         if(temper-set_temper>0)//如果实际温度大于设定温度
  270.         {
  271.             high_time=0 ;
  272.             low_time=100 ;
  273.         }
  274.         else
  275.         {
  276.             for(i=0;i<10;i++)
  277.             {
  278.                 //get_temper();
  279.                 rin=s ;
  280.                 // Read Input
  281.                 rout=PIDCalc(&spid,rin);
  282.                 // Perform PID Interation
  283.             }
  284.             if(high_time<100) //此变量是无符号字符型
  285.             high_time=(unsigned char)(rout/10000);
  286.             else
  287.             high_time=0 ;//限制不输出负值
  288.             low_time=(100-high_time);
  289.             //EA=1;
  290.         }
  291.     }
  292. }


  293. /*****************************************************
  294. T0中断服务子程序,用于控制电平的翻转 ,40us*100=4ms周期
  295. ******************************************************/
  296. void serve_T0()interrupt 1 using 1
  297. {
  298.     if(++count<=(high_time))
  299.          output=0 ;
  300.     else if(count<=100)
  301.     {
  302.         output=1 ;
  303.     }
  304.     else
  305.     count=0 ;
  306.     TH0=0x2f ;
  307.     TL0=0xe0 ;
  308. }

  309. void PIDBEGIN()
  310. {
  311.    
  312.     TMOD=0x01 ;
  313.     TH0=0x2f ;
  314.     TL0=0x40 ;
  315.    
  316.     EA=1 ;
  317.     ET0=1 ;
  318.     TR0=1 ;
  319.    
  320.     high_time=50 ;
  321.     low_time=50 ;
  322.     PIDInit(&spid);
  323.     // Initialize Structure
  324.     spid.Proportion=10 ;
  325.     // Set PID Coefficients
  326.     spid.Integral=8 ;
  327.     spid.Derivative=6 ;
  328.     spid.SetPoint=100 ;
  329.     // Set PID Setpoint
  330.    
  331. }
  332. #endif
复制代码



回复

使用道具 举报

ID:82175 发表于 2015-6-6 10:30 | 显示全部楼层
make an effort  51hei有你更精彩!!
回复

使用道具 举报

ID:83639 发表于 2015-6-22 19:24 | 显示全部楼层
rin=s,这里的s你在哪里定义过?突然就出现了啊
回复

使用道具 举报

ID:1 发表于 2015-11-17 16:33 | 显示全部楼层
szyzw 发表于 2015-6-22 19:24
**** 作者被禁止或删除 内容自动屏蔽 ****

struct PID spid ;
// PID Control Structure
unsigned int rout ;
// PID Response (Output)
unsigned int rin ;
// PID Feedback (Input)

你看214行的定义
回复

使用道具 举报

ID:79544 发表于 2015-12-27 18:44 | 显示全部楼层
楼主,其他的.h文件呢?
回复

使用道具 举报

ID:102615 发表于 2016-1-10 13:33 | 显示全部楼层
都没见仿真图呢
回复

使用道具 举报

ID:106833 发表于 2016-3-1 13:34 | 显示全部楼层
不想说有多少错误了
回复

使用道具 举报

ID:103523 发表于 2016-4-20 17:23 | 显示全部楼层
high_time=(unsigned char)(rout/800);楼主  这个为什么要除以800而不是其他数值  怎么算的呀  求回答  非常感谢!
回复

使用道具 举报

ID:59609 发表于 2016-8-10 10:32 | 显示全部楼层
这好的程序
为什么不把它做的更完美呢
回复

使用道具 举报

ID:147531 发表于 2017-1-16 13:42 | 显示全部楼层
admin 发表于 2015-11-17 16:33
struct PID spid ;
// PID Control Structure
unsigned int rout ;

他问的应该是第264行中 rin =s 中的 s 是怎么来的
回复

使用道具 举报

ID:169407 发表于 2017-3-13 15:05 | 显示全部楼层
li562721098 发表于 2016-3-1 13:34
**** 作者被禁止或删除 内容自动屏蔽 ****

很多错误?
回复

使用道具 举报

ID:71525 发表于 2017-4-17 11:11 来自手机 | 显示全部楼层
整个分析了他的代码,这样的程序你发上来有什么意思?你用PID的意义何在?当温度超调你就不再加热,这个自动调节有违背呀。还有你的定时器实现PWM明显逻辑错误…我不知道说什么好。误人子弟,浪费我墨水。

评分

参与人数 1黑币 +40 收起 理由
admin + 40 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:161033 发表于 2017-6-4 23:39 | 显示全部楼层
感谢,先下后看。
回复

使用道具 举报

ID:110278 发表于 2017-7-23 15:57 | 显示全部楼层
感谢分享分享。。。。
回复

使用道具 举报

ID:229761 发表于 2017-9-12 11:24 | 显示全部楼层
黑橙orangerx 发表于 2017-1-16 13:42
他问的应该是第264行中 rin =s 中的 s 是怎么来的

他还有的文件没显示出来,应该是温度传感器里面的
回复

使用道具 举报

ID:167679 发表于 2017-12-8 12:43 | 显示全部楼层
zae234234 发表于 2017-4-17 11:11
整个分析了他的代码,这样的程序你发上来有什么意思?你用PID的意义何在?当温度超调你就不再加热,这个自 ...

一看到ds18b20 就应该知道 这不是什么正经的PID 温控 ,顶多算学校练手的东西 ,  真正的经典的pid温控都是用51单片机做的双积分adc 然后可以达到比较高的采样分辨率 ,万能信号输入.
回复

使用道具 举报

ID:266854 发表于 2017-12-30 14:47 | 显示全部楼层
harvardx 发表于 2017-12-8 12:43
一看到ds18b20 就应该知道 这不是什么正经的PID 温控 ,顶多算学校练手的东西 ,  真正的经典的pid温控都是 ...

您是指PID温控的话使用DS18B20是不合适的吗?
回复

使用道具 举报

ID:294233 发表于 2018-6-25 20:48 | 显示全部楼层
楼主请问一下
if(high_time<=100) //限制最大值
            high_time=(unsigned char)(rout/800);
            else
            high_time=100;
            low_time=(100-high_time);
这一块程序里面的红色部分是什么原理,看不懂,加急求回复
回复

使用道具 举报

ID:346116 发表于 2018-10-27 10:01 | 显示全部楼层
楼主估计是在哪里抄来的,他没办法解答大家的疑问
回复

使用道具 举报

ID:346116 发表于 2018-10-27 10:41 | 显示全部楼层
zae234234 发表于 2017-4-17 11:11
整个分析了他的代码,这样的程序你发上来有什么意思?你用PID的意义何在?当温度超调你就不再加热,这个自 ...

中断控制电平翻转,确实逻辑有错误,若pid算出了的high_time = 0,outpu = 1,并非预想中的output = 0。

评分

参与人数 1黑币 +20 收起 理由
admin + 20 回帖助人的奖励!

查看全部评分

回复

使用道具 举报

ID:358930 发表于 2018-10-31 21:40 来自手机 | 显示全部楼层
mingzinanqu 发表于 2018-6-25 20:48
楼主请问一下
if(high_time

high_time=(unsigned char)(rout/800);红色大约代表,high_time=(rout/800)后强制转为255
的char型变量。
回复

使用道具 举报

ID:456973 发表于 2018-12-28 08:12 | 显示全部楼层
PID算法新人,学习学习
回复

使用道具 举报

ID:146874 发表于 2018-12-28 08:45 | 显示全部楼层
太感谢分享了,很值得研究
回复

使用道具 举报

ID:517951 发表于 2020-11-15 14:20 | 显示全部楼层

收藏学习学习,谢谢楼主了
回复

使用道具 举报

ID:67838 发表于 2022-10-18 15:47 | 显示全部楼层
感谢,先下后看。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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