51单片机-温度PID算法(C程序)_第1页
51单片机-温度PID算法(C程序)_第2页
51单片机-温度PID算法(C程序)_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第第页51单片机-温度PID算法(C程序)#include"reg51.h"#include"intrins.h"#include"math.h"#include"string.h"structPID{unsignedintSetPoint;//设定目标DesiredValueunsignedintProportion;//比例常数ProportionalConstunsignedintIntegral;//积分常数IntegralConstunsignedintDerivative;//微分常数DerivativeConstunsignedintLastError;//Error[-1]unsignedintPrevError;//Error[-2]unsignedintSumError;//SumsofErrors};structPIDspid;//PIDControlStructureunsignedintrout;//PIDResponse(Output)unsignedintrin;//PIDFeedback(Input)sbitdata1=P1^0;sbitclk=P1^1;sbitplus=P2^0;sbitsubs=P2^1;sbitstop=P2^2;sbitoutput=P3^4;sbitDQ=P3^3;unsignedcharflag,flag_1=0;unsignedcharhigh_time,low_time,count=0;//占空比调节参数unsignedcharset_temper=25;unsignedchartemper;unsignedchari;unsignedcharj=0;unsignedints;voiddelay(unsignedchartime){unsignedcharm,n;for(n=0;nfor(m=0;m>i;temp=tempwrite_bit(temp);}delay(7);//TR0=1;EA=1;}unsignedcharread_bit(){unsignedchari,value_bit;EA=0;DQ=0;_nop_();_nop_();DQ=1;for(i=0;i>4;temper=i|j;}voidPIDInit(structPID*pp){memset(pp,0,sizeof(structPID));//全部初始化为0}unsignedintPIDCalc(structPID*pp,unsignedintNextPoint){unsignedintdError,Error;Error=pp->SetPoint-NextPoint;//偏差pp->SumError+=Error;//积分dError=pp->LastError-pp->PrevError;//当前微分pp->PrevError=pp->LastError;pp->LastError=Error;return(pp->Proportion*Error//比例项+pp->Integral*pp->SumError//积分项+pp->Derivative*dError);//微分项}voidcompare_temper(){unsignedchari;if(set_temper>temper)//是否设置的温度大于实际温度{if(set_temper-temper>1)//设置的温度比实际的温度是否是大于1度{high_time=100;//如果是,则全速加热low_time=0;}else//如果是在1度范围内,则运行PID计算{for(i=0;i0){high_time=0;low_time=100;}else{for(i=0;i>1;if(m==1)data1=1;elsedata1=0;_nop_();clk=1;_nop_();}}}voiddisplay(){unsignedcharcodenumber[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};unsignedchardisp_num[6];unsignedintk,k1;k=high_time;k=k00;k1=k/100;if(k1==0)disp_num[0]=0;elsedisp_num[0]=0x60;k=k0;disp_num[1]=number[k/10];disp_num[2]=number[k];k=temper;k=k0;disp_num[3]=number[k/10];disp_num[4]=number[k]+1;disp_num[5]=number[s/10];disp_1(disp_num);}voidmain(){unsignedcharz;unsignedchara,b,flag_2=1,count1=0;unsignedcharphil[]={2,0xce,0x6e,0x60,0x1c,2};TMOD=0x21;TH0=0x2f;TL0=0x40;SCON=0x50;PCON=0x00;TH1=0xfd;TL1=0xfd;PS=1;EA=1;EX1=0;ET0=1;ES=1;TR0=1;TR1=1;high_time=50;low_time=50;PIDInit(//InitializeStructurespid.Proportion=10;//SetPIDCoefficients比例常数ProportionalConstspid.Integral=8;//积分常数IntegralConstspid.Derivative=6;//微分常数DerivativeConstspid.SetPoint=100;//SetPIDSetpoint设定目标DesiredValuewhile(1){if(plus==0){EA=0;for(a=0;a5)temper=a;elsetemper=b;a=temper;flag_2=0;if(++count1>30){display();count1=0;}compare_temper();}TR0=

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论