您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 冶金工业 > 温度控制的PID算法的C语言程序
我的题目是:基于PID算法的温度控制系统89C51单片机,通过键盘输入预设值,与DS18B20测得的实际值做比较,然后驱动制冷或加热电路。用keilC语言来实现PID的控制。最佳答案//PID算法温控C语言2008-08-1718:58#includereg51.h#includeintrins.h#includemath.h#includestring.hstructPID{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=35;unsignedchartemper;unsignedchari;unsignedcharj=0;unsignedints;/***********************************************************延时子程序,延时时间以12M晶振为准,延时时间为30us×time***********************************************************/voiddelay(unsignedchartime){unsignedcharm,n;for(n=0;ntime;n++)for(m=0;m2;m++){}}/***********************************************************写一位数据子程序***********************************************************/voidwrite_bit(unsignedcharbitval){EA=0;DQ=0;/*拉低DQ以开始一个写时序*/if(bitval==1){_nop_();DQ=1;/*如要写1,则将总线置高*/}delay(5);/*延时90us供DA18B20采样*/DQ=1;/*释放DQ总线*/_nop_();_nop_();EA=1;}/***********************************************************写一字节数据子程序***********************************************************/voidwrite_byte(unsignedcharval){unsignedchari;unsignedchartemp;EA=0;/*关中断*/TR0=0;for(i=0;i8;i++)/*写一字节数据,一次写一位*/{temp=vali;/*移位操作,将本次要写的位移到最低位*/temp=temp&1;write_bit(temp);/*向总线写该位*/}delay(7);/*延时120us后*///TR0=1;EA=1;/*开中断*/}/***********************************************************读一位数据子程序***********************************************************/unsignedcharread_bit(){unsignedchari,value_bit;EA=0;DQ=0;/*拉低DQ,开始读时序*/_nop_();_nop_();DQ=1;/*释放总线*/for(i=0;i2;i++){}value_bit=DQ;EA=1;return(value_bit);}/***********************************************************读一字节数据子程序***********************************************************/unsignedcharread_byte(){unsignedchari,value=0;EA=0;for(i=0;i8;i++){if(read_bit())/*读一字节数据,一个时序中读一次,并作移位处理*/value|=0x01i;delay(4);/*延时80us以完成此次都时序,之后再读下一数据*/}EA=1;return(value);}/***********************************************************复位子程序***********************************************************/unsignedcharreset(){unsignedcharpresence;EA=0;DQ=0;/*拉低DQ总线开始复位*/delay(30);/*保持低电平480us*/DQ=1;/*释放总线*/delay(3);presence=DQ;/*获取应答信号*/delay(28);/*延时以完成整个时序*/EA=1;return(presence);/*返回应答信号,有芯片应答返回0,无芯片则返回1*/}/***********************************************************获取温度子程序***********************************************************/voidget_temper(){unsignedchari,j;do{i=reset();/*复位*/}while(i!=0);/*1为无反馈信号*/i=0xcc;/*发送设备定位命令*/write_byte(i);i=0x44;/*发送开始转换命令*/write_byte(i);delay(180);/*延时*/do{i=reset();/*复位*/}while(i!=0);i=0xcc;/*设备定位*/write_byte(i);i=0xbe;/*读出缓冲区内容*/write_byte(i);j=read_byte();i=read_byte();i=(i4)&0x7f;s=(unsignedint)(j&0x0f);s=(s*100)/16;j=j4;temper=i|j;/*获取的温度放在temper中*/}/*====================================================================================================InitializePIDStructure=====================================================================================================*/voidPIDInit(structPID*pp){memset(pp,0,sizeof(structPID));}/*====================================================================================================PID计算部分=====================================================================================================*/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);//微分项}/***********************************************************温度比较处理子程序***********************************************************/compare_temper(){unsignedchari;if(set_tempertemper){if(set_temper-temper1){high_time=100;low_time=0;}else{for(i=0;i10;i++){get_temper();rin=s;//ReadInputrout=PIDCalc(&spid,rin);//PerformPIDInteration}if(high_time=100)high_time=(unsignedchar)(rout/800);elsehigh_time=100;low_time=(100-high_time);}}elseif(set_temper=temper){if(temper-set_temper0){high_time=0;low_time=100;}else{for(i=0;i10;i++){get_temper();rin=s;//ReadInputrout=PIDCalc(&spid,rin);//PerformPIDInteration}if(high_time100)high_time=(unsignedchar)(rout/10000);elsehigh_time=0;low_time=(100-high_time);}}//else//{}}/*****************************************************T0中断服务子程序,用于控制电平的翻转,40us*100=4ms周期******************************************************/voidserve_T0()interrupt1using1{if(++count=(high_time))output=1;elseif(count=100){output=0;}elsecount=0;TH0=0x2f;TL0=0xe0;}/*****************************************************串行口中断服务程序,用于上位
本文标题:温度控制的PID算法的C语言程序
链接地址:https://www.777doc.com/doc-5626147 .html