《模糊PID控制温度系统的C语言设计程式.docx》由会员分享,可在线阅读,更多相关《模糊PID控制温度系统的C语言设计程式.docx(38页珍藏版)》请在第一文库网上搜索。
1、模糊PID控制温控系统仿真设计C程序代码#inc1ude#defineucharunsignedchar#defineuintunsignedint#definePU1SE200#definenumber0.035sbitSDO=P20;sbitSDI=P21;sbitCS=P22;sbitC1K=P23;sbitEOC=P24;sbitRS=P25;sbitRW=P26;sbitEN=P27;sbitKEY1=P30;sbitKEY2=P31;sbitKEY3=P32;sbitKEY4=P33;sbitKEY5=P34;sbitIN1=P35;sbitIN2=P36;sbitENA=P37;
2、ucharf1ag;ucharf1ag_start;f1oatS_temp=60.0;f1oatP_temp=20.0;f1oatK;f1oatKi;f1oatKd;f1oatErr=0.0;f1oat1ast_Err=0.0;f1oatD_Err=0.0;f1oatSum_Err=0.0;f1oatU=0.0;函数功能:延时uchari;ucharj;for(i=z;i0;i-)for(j=360;j0;j-);)voidde1ay_us(ucharz)(uchari;fbr(i=z;iO;i-);)void1CD_WriteData(ucharDat)(RS=1;P1=Dat;de1ay_
3、us(10);EN=1;de1ay_us(10);EN=O;)RS=O;P1=com;de1ay_us(10);EN=1;de1ay_us(10);EN=O;voidShow_Num(ucharx,uchary,ucharn,f1oatnum)uchara3;uchari;uintTemp;Temp=(int)num;for(i=0;i0;i)1CD_WriteData(ai-1+0x30);voidShow_Ki(ucharnum_Ki)(ucharTemp;num_Ki=Ki*100;TemP=(UChar)num_Ki;Show_Num(10,2,1,Temp%10);Temp=Temp
4、/10;ShOW_NUm(9,2,1,Temp%10);Temp=Temp/10;Show_Num(7,2,1,Temp);)voidShow_char(ucharx,uchary,ucharch)(if(y%2=1)1CD_WriteCOM(0x80+x);e1se1CD_WriteCOM(0x80+0x40+x);1CD_WriteData(ch);)void1CD_Init(void)(RW=O;EN=O;1CD_WriteCOM(Ox38);1CD-WriteCOM(OxOc);1CD.WriteCOM(0x06);1CD_WriteCOM(0x01);函数功能:显示函数void1CD
5、_disp1ay(void)(Show_char(1,1,T,);de1ay_us(10);Show_char(0,1,P,);de1ay_us(10);Show-char(1,1,T);de1ay_us(10);ShoW_char(2,1de1ay_us(10);Show-Num(3,1,3,P_temp);de1ay_us(10);Show_char(10,S);de1ay_us(10);Show.char(11,1,T);de1ay-us(10);Show-char(12,1,:);de1ay_us(10);Show_Num(13,1,3,S_temp);de1ay_us(10);Sho
6、w.char(0,2P,);de1ay_us(10);Show_char(1,2/:);de1ay_us(10);ShoW_Num(2,2,2,KP);de1ay_us(10);Show_char(5,2,T);de1ay_us(10);Show_char(6,27:1);de1ay_us(10);Show_char(8,2/.);de1ay_us(10);Show_Ki(Ki);de1ay_us(10);Show_char(12,2,D);de1ay_us(10);Show_char(13,2,:);de1ay-us(10);ShOW_Num(14,2,2,Kd);de1ay_us(10);
7、函数功能:定期器2初始化voidTimer2_Init()RCAP2H=(65536-300)/256;RCAP21=(65536-300)%256;TH2=RCAP2H;T12=RCAP21;ET2=1;TR2=1;EA=1;)函数功能:键盘扫描,调整设置温度voidkey_scan(void)(if(KEY1=0)(de1ay_ms(1);if(KEY1=0)(S_temp=S_temp+1;if(S_temp=200)S_temp=200;whi1e(!KEY1);)if(KEY2=0)(de1ay_ms(1);if(KEY2=0)(if(S-tempO)S_temp=S_temp-1;
8、e1seif(S_temp=0)S-temp=O;whi1e(!KEY2);if(KEY3=O)fde1ay_ms(1);if(KEY3=O)(if(S-temp=10)S_temp=S_temp-10;whi1e(!KEY4);)if(KEY5=0)(de1ay_ms(1);if(KEY5=0)(f1ag-start=1;whi1e(!KEY5);函数功能:P1D的计算voidPID_Ca1cu1ate()(Err=S_temp-P_temp;Sum_Err+=Err;D_Err=Err-1ast_Err;1ast_Err=Err;U=Kp*Err+Ki*Sum_Err+Kd*D_Err;U
9、=(int)U;if(U=0)if(U=200)U=200;f1ag=1;)e1seU=-U;if(U=200)U=200;f1ag=O;函数功能:PID参数KpB计算f1oatfuzzy_kp(f1oate,f1oatec)/e,ec,表达误差,误差变化率f1oatKp-ca1cu;ucharnum,pe,pec;f1oatcodeeRu1e7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0;误差EB模糊论域f1oatcodeecRu1e7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0;误差变化率EC的模糊论f1oateFuzzy2J=0.0,0.0;从属于误差
10、E日勺从属程度f1oatecFuzzy2=0.0,0.0);从属于误差变化率EC的从属程度/KpB模糊子集从属于KPB从属程度intcodeKpRu1e77=/Kp的模糊控制表f1oatcodekpRu1e4=0.0,8.0,16.0,24.0;f1oatKpFuzzy4=0.0,0.0,0.0,0.0;3,3,3,3,3,332,2,2,2,122,1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0,1,0,0,1,0,0,1,0,1,0,023,3,3,3,3,3,3;/*误差E从属函数描述*/if(eeRu1e0)(eFuzzyO=1.0;pe=O;)e1seif(eRu1e
11、O=e&eeRu1e1)(eFuzzyO=(eRu1e1-e)(eRu1e11-eRu1eO);pe=0;e1seif(eRu1e1=e&eeRu1e2)eFuzzyO=(eRu1e2-e)(eRuie2-eRu1e1);pe=1;1e1seif(eRu1e12=e&eeRu1e3)(eFuzzyO=(eRu1e3-e)(eRu1e3-eRu1e2);e=2;)e1seif(eRu1e3=e&eeRu1e4)eFuzzyO=(eRu1e4-e)(eRu1e4-eRu1e3);pe=3;)e1seif(eRu1e4=e&eeRu1e5)(eFuzzyO=(eRu1e5-e)(eRu1e5-eRu
12、1e4);pe=4;)e1seif(eRu1e51=e&eeRu1e6)eFuzzyO=(eRu1e6-e)(eRu1e6-eRu1e5);pe=5;e1se(eFuzzyO=O.O;pe=5;)eFuzzy1=1.0-eFuzzyO;/*误差变化率EC从属函数描述*/if(ececRu1eO)(ecFuzzyO=1.0;PeC=0;)e1seif(ecRu1eO=ec&ececRu1e1)ecFuzzyO=(ecRu1e1-ec)(ecRu1e1-ecRu1eO);pec=O;)e1seif(ecRu1e1=ec&ececRu1e2)ecFuzzyO=(ecRu1e2-ec)(ecRu1e2-ecRu1e1);pec=1;)e1seif(ecRu1ev=ec&ececRu1e3)(ecFuzzyO=(ecRu1e3-ec)(ecRu1e3-ecRu1e2);pec=2;e1seif(ecRu1e3=ec&ececRu1e4)ecFuzzyO=(ecRu1e4-ec)(ecRu1e4-ecRu1e3);pec=3;)e1seif(ecRu1e4=ec&ececRu1e5)ecFuzzyO=(ecRu1e5-ec)(ec