PID算法温控C语言.docx
《PID算法温控C语言.docx》由会员分享,可在线阅读,更多相关《PID算法温控C语言.docx(68页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、/PID 算法温控 C 语言 2008-08-17 18:58#include#include#include#includestruct PID unsigned int SetPoint; /Z 设定目标 Desired Valueunsigned int Proportion; / 比例常数 Proportional Constunsigned int Integral; /Z 积分常数 Integral Constunsigned int Derivative; /Z 微分常数 Derivative Constunsigned int LastError; / Error-1unsig
2、ned int PrevError; / Error-2unsigned int SumError; / Sums of Errors);struct PID spid; / PID Control Structureunsigned int rout; / PID Response (Output)unsigned int rin; / PID Feedback (Input)sbit data1=P1 A0;sbit clk=P1 A1;sbit plus=P2A0;sbit subs=P2A1;sbit stop=P2A2;sbit output=P3A4;sbit DQ=P3A3;un
3、signed char flag,flag_1 =0;unsigned char high_time,low_time,count=0;占空比调节参数unsigned char set_temper=35;unsigned char temper;unsigned char i;unsigned char j=0;unsigned int s;/*延时子程序,延时时间以12M晶振为准,延时时间为30usxtime*/void delay(unsigned char time)(unsigned char m,n;for(n=0;ntime;n+)for(m=0;m2;m+)写位数据子程序*/
4、void write_bit(unsigned char bitval)(EA=0;DQ=0; /拉低DQ以开始个写时序/if(bitval=1)(_nop_();DQ=1; /如要写1,则将总线置高/)delay(5); /*延时 90us 供 DA18B20 采样/DQ=!释放DQ总线_nop_();_nop_();EA=1;/* 写一字节数据子程序*/void write_byte(unsigned char val)unsigned char i;unsigned char temp;EA=O;/关中断/TRO=O;for(i二;iv8;i+) /*写一字节数据,一次写一位/(temp
5、:vali; /移位操作,将本次要写的位移到最低位/temp=temp&1;write_bit(temp); /向总线写该位/)delay。);/* 延时 120us 后/ TRO=1;EA=1;/开中断/*读一位数据子程序*/unsigned char read_bit()(unsigned char i,value_bit;EA=O;DQ=O;/拉低DQ,开始读时序/_nop_();_nop_();DQ=1;/*释放总线/for(i=0;i2;i+)value_bit=DQ;EA=1;return(value_bit);/*读字节数据子程序* /unsigned char read_byt
6、e()(unsigned char i,value=O;EA=O;for(i=0;iSetPoint - NextPoint; / 偏差pp-SumError += Error; /Z 积分dError = pp-LastError - pp-PrevError; /Z 当前微分 pp-PrevError = pp-LastError;pp-LastError = Error;return (pp-Proportion * Error比例+ pp-lntegral * pp-SumError /积分项+ pp-Derivative * dError); /Z 微分项)/* 温度比较处理子程序*
7、/compare_temperQunsigned char i;是否设置的温度大于实if(set_tempertemper)际温度if(set_temper-temper1)度是否是大于1度(high_time=100;low_time=0;)else运行PID计算(for(i=0;i10;i+) get_temper();rin = s; / Read Input设置的温度比实际的温如果是,则全速加热如果是在1度范围内,则rout = PIDCalc ( &spid,rin ); / Perform PID Interation ) if (high_time=100)high_time=(
8、unsigned char)(rout/800);else high_time=100;Iow_time= (100-high_time);)else if(set_temperO)(high_time=O;low_time=100;)else(for(i=0;i10;i+) get_temper();rin = s; / Read Inputrout = PIDCalc ( &spid,rin ); / Perform PID Interation)if (high_time100)high_time=(unsigned char)(rout/10000);elsehigh_time=0;l
9、ow_time= (100-high_time);)/ else/*TO中断服务子程序,用于控制电平的翻转,40us*100=4ms 周期*/void serve_T0() interrupt 1 using 1(if(+count=(high_time)output=1;else if(count=100)(output=0;)elsecount=0;TH0=0x2f;TL0=0xe0;*串行口中断服务程序,用于上位机通讯*void serve_sio() interrupt 4 using 2(/* EA=0;RI=0;i=SBUF;if(i=2)(while(FU=0)RI=0;set_
10、temper=SBUF;SBUF=0x02;while(TI=0)TI=0;else if(i=3)(TI=0;SBUF=temper;while(TI=0)TI=O;)EA=1 ;*/void disp_1 (unsigned char disp_num16)(unsigned char n,a,m;for(n=0;n6;n+)(/ k=disp_num1n;for(a=0;a8;a+)(clk=0;m=(disp_num1 n&1);disp_num1 n=disp_num1 n1;if(m=1)data1=1;elsedata1=0;_nop_();clk=1;_nop_();)/*显示
11、子程序功能:将占空比温度转化为单个字符,显示占空比和测得到 的温度*/void display()(unsigned char codenumber=Oxfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6;unsigned char disp_num6;unsigned int k,k1;k=high_time;k=k%1000;k1=k/100;if(k1=0)disp_num0=0;elsedisp_num0=0x60;k=k%100;disp_num1 =numberk/10;disp_num2=numberk%10;k=temper;k=k%
12、100;disp_num3=numberk/10;disp_num4=numberk%10+1;disp_num5=numbers/10;disp_1 (disp_num);)/* 主程序* / main()(unsigned char z;unsigned char a,b,flag_2=1 ,count1 =0;unsigned char phil=2,0xce,0x6e,0x60,0x1c,2;TMOD=Ox21;TH0=0x2f;TL0=0x40;SCON=0x50;PCON=OxOO;TH1=0xfd;TL1=0xfd;PS=1;EA=1;EX1=0;ET0=1;ES=1;TR0=1
13、;TR1=1;high_time=50;low_time=50;PIDInit ( &spid ); / Initialize Structure spid.Proportion = 10; / Set PID Coefficients spid.Integral = 8;spid.Derivative =6;spid.SetPoint = 100; / Set PID Setpoint while(1)(if(plus=0)EA=0;for(a=0;a5;a+) for(b=0;b102;b+) if(plus=O) set_temper+;flag=O; else if(subs=O) (
14、 for(a=0;a5;a+) for(b=0;a102;b+) if(subs=O) ( set_temper-;flag=O;)else if(stop=0)(for(a=0;a5;a+)for(b=0;b5) temper=a;elsetemper=b;a=temper; flag_2=0;if(+count130) display();count1=0;)compare_temper();)TRO=O;z=1;while(1)(EA=O;if(stop=0)(for(a=0;a5;a+) for(b=0;b102;b+) if(stop=0) disp_1(phil);/ break;
15、EA=1;)/DS18b20子程序定义端口#include sbit DQ=P2A1;typedef unsigned char byte;typedef unsigned int word;延时void delay(word useconds)(for(;useconds0;useconds-);byte ow_reset(void)(byte presence;DQ=0;delay(29);DQ=1;delay(3);presence=DQ;delay(25);return(presence);个字节复位/DQ低电平/480usDQ高电平等待/presence 信号/0允许,1禁止从1-w
16、ire总线上读取byte read_byte(viod)byte i;byte value=O;for (i=8;i0;i-)(value=1;DQ=0;DQ=1;delay(1);if(DQ)value|=0x80;delay(6);return(value);向1-wire总线上写个字节void write_byte(char val)(byte i;for (i=8;i0;i-)一次写个字节DQ=O;DQ=val&OxO1;delay(5);DQ=1;val=val/2;)delay(5);读取温度)char Read_Temperature(void)(unionbyte c2;int
17、 x;temp;ow_reset();write_byte(Oxcc);write_byte(OxBE);temp.c1=read_byte();temp.cO=read_byte();ow_reset();write_byte(OxCC);write_byte(0x44);return temp.x/2;)PID算法(c语言) 2010-01-27 08:53BC31 TC30编译过,可运行。include #includestruct _pid |int pv; / * integer that contains the process value*/ int sp; / * intege
18、r that contains the set point*/ float integral; /Z积分值偏差累计值float pgain;float igain;float dgain;int deadband;int last_error;);struct _pid warm, *pid;int process_point, set_point, dead_band;float p_gain, i_gain, d_gain, integral_val, new_integ;/ *pid_init DESCRIPTION This function initializes the point
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- PID 算法 温控 语言
限制150内