2022年模糊PID控制温控系统设计C语言程序代码 .pdf
/* 模糊 PID 控制温控系统仿真设计C 程序代码*/ #include #define uchar unsigned char #define uint unsigned int #define PULSE 200 #define number 0.035 sbit SDO = P20; sbit SDI = P21; sbit CS = P22; sbit CLK = P23; sbit EOC = P24; sbit RS = P25; sbit RW = P26; sbit EN = P27; sbit KEY1= P30; sbit KEY2= P31; sbit KEY3= P32; sbit KEY4= P33; sbit KEY5= P34; sbit IN1 = P35; sbit IN2 = P36; sbit ENA = P37; uchar flag; uchar flag_start; float S_temp=60.0; float P_temp=20.0; float Kp; float Ki; float Kd; float Err=0.0; float Last_Err=0.0; float D_Err=0.0; float Sum_Err=0.0; float U=0.0; /* 函数功能:延时*/ void delay_ms(uchar z) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 17 页 - - - - - - - - - uchar i; uchar j; for(i=z;i0;i-) for(j=360;j0;j-); void delay_us(uchar z) uchar i; for(i=z;i0;i-); void LCD_WriteData(uchar Dat) RS = 1; P1 = Dat; delay_us(10); EN = 1; delay_us(10); EN = 0; void LCD_WriteCOM(uchar com) RS = 0; P1 = com; delay_us(10); EN = 1; delay_us(10); EN = 0; void Show_Num(uchar x,uchar y,uchar n,float num) uchar a3; uchar i; uint Temp; Temp=(int)num; for(i=0;i0;i-) LCD_WriteData(ai-1+0 x30); void Show_Ki(uchar num_Ki) uchar Temp; num_Ki=Ki*100; Temp=(uchar)num_Ki; Show_Num(10,2,1,Temp%10); Temp=Temp/10; Show_Num(9,2,1,Temp%10); Temp=Temp/10; Show_Num(7,2,1,Temp); void Show_char(uchar x,uchar y,uchar ch) if(y%2 = 1) LCD_WriteCOM(0 x80+x); else LCD_WriteCOM(0 x80+0 x40+x); LCD_WriteData(ch); void LCD_Init(void) RW = 0; EN = 0; LCD_WriteCOM(0 x38); LCD_WriteCOM(0 x0c); LCD_WriteCOM(0 x06); LCD_WriteCOM(0 x01); /* 函数功能:显示函数*/ void LCD_display(void) Show_char(1,1,T); delay_us(10); Show_char(0,1,P); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 17 页 - - - - - - - - - delay_us(10); Show_char(1,1,T); delay_us(10); Show_char(2,1,:); delay_us(10); Show_Num(3,1,3,P_temp); delay_us(10); Show_char(10,1,S); delay_us(10); Show_char(11,1,T); delay_us(10); Show_char(12,1,:); delay_us(10); Show_Num(13,1,3,S_temp); delay_us(10); Show_char(0,2,P); delay_us(10); Show_char(1,2,:); delay_us(10); Show_Num(2,2,2,Kp); delay_us(10); Show_char(5,2,I); delay_us(10); Show_char(6,2,:); delay_us(10); Show_char(8,2,.); delay_us(10); Show_Ki(Ki); delay_us(10); Show_char(12,2,D); delay_us(10); Show_char(13,2,:); delay_us(10); Show_Num(14,2,2,Kd); delay_us(10); /* 函数功能:定时器 2 初始化*/ void Timer2_Init() RCAP2H =(65536-300)/256; RCAP2L =(65536-300)%256; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 17 页 - - - - - - - - - TH2 = RCAP2H; TL2 = RCAP2L; ET2 = 1; TR2 = 1; EA = 1; /* 函数功能:键盘扫描,调整设置温度*/ void key_scan(void) if(KEY1=0) delay_ms(1); if(KEY1=0) S_temp=S_temp+1; if(S_temp=200) S_temp=200; while(!KEY1); if(KEY2=0) delay_ms(1); if(KEY2=0) if(S_temp0) S_temp=S_temp-1; else if(S_temp=0) S_temp=0; while(!KEY2); if(KEY3=0) delay_ms(1); if(KEY3=0) if(S_temp=10) S_temp=S_temp-10; while(!KEY4); if(KEY5=0) delay_ms(1); if(KEY5=0) flag_start=1; while(!KEY5); /* 函数功能: PID 的计算*/ void PID_Calculate() Err = S_temp - P_temp; Sum_Err += Err; D_Err = Err - Last_Err; Last_Err = Err; U=Kp*Err+Ki*Sum_Err+Kd*D_Err; U=(int)U; if(U=0) if(U=200) U=200; flag=1; else U=-U; if(U=200) U=200; flag=0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 17 页 - - - - - - - - - /* 函数功能: PID 参数 Kp 的计算*/ float fuzzy_kp(float e, float ec) /e,ec ,表示误差,误差变化率 float Kp_calcu; uchar num,pe,pec; float code eRule7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0; /误差 E 的模糊论域float code ecRule7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0; /误差变化率 EC 的模糊论域float eFuzzy2=0.0,0.0; /隶属于误差 E 的隶属程度float ecFuzzy2=0.0,0.0; /隶属于误差变化率EC 的隶属程度float code kpRule4=0.0,8.0,16.0,24.0; /Kp 的模糊子集float KpFuzzy4=0.0,0.0,0.0,0.0; /隶属于 Kp 的隶属程度int code KpRule77= /Kp 的模糊控制表 3,3,3,3,3,3,3, 2,2,2,2,1,2,2, 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,0,2, 3,3,3,3,3,3,3 ; /* 误差 E 隶属函数描述 */ if(eeRule0) eFuzzy0 =1.0; pe = 0; else if(eRule0=e & eeRule1) eFuzzy0 = (eRule1-e)/(eRule1-eRule0); pe = 0; else if(eRule1=e & eeRule2) eFuzzy0 = (eRule2 -e)/(eRule2-eRule1); pe = 1; else if(eRule2=e & eeRule3) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 17 页 - - - - - - - - - eFuzzy0 = (eRule3 -e)/(eRule3-eRule2); pe = 2; else if(eRule3=e & eeRule4) eFuzzy0 = (eRule4-e)/(eRule4-eRule3); pe = 3; else if(eRule4=e & eeRule5) eFuzzy0 = (eRule5-e)/(eRule5-eRule4); pe = 4; else if(eRule5=e & eeRule6) eFuzzy0 = (eRule6-e)/(eRule6-eRule5); pe = 5; else eFuzzy0 =0.0; pe =5; eFuzzy1 =1.0 - eFuzzy0; /* 误差变化率 EC 隶属函数描述 */ if(ececRule0) ecFuzzy0 =1.0; pec = 0; else if(ecRule0=ec & ececRule1) ecFuzzy0 = (ecRule1 - ec)/(ecRule1-ecRule0); pec = 0 ; else if(ecRule1=ec & ececRule2) ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1); pec = 1; else if(ecRule2=ec & ececRule3) ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2); pec = 2 ; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 17 页 - - - - - - - - - else if(ecRule3=ec & ececRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); pec=3; else if(ecRule4=ec & ececRule5) ecFuzzy0 = (ecRule5-ec)/(ecRule5-ecRule4); pec=4; else if(ecRule5=ec & ececRule6) ecFuzzy0 = (ecRule6-ec)/(ecRule6-ecRule5); pec=5; else ecFuzzy0 =0.0; pec = 5; ecFuzzy1 = 1.0 - ecFuzzy0; /*查询模糊规则表 */ num =KpRulepepec; KpFuzzynum += eFuzzy0*ecFuzzy0; num =KpRulepepec+1; KpFuzzynum += eFuzzy0*ecFuzzy1; num =KpRulepe+1pec; KpFuzzynum += eFuzzy1*ecFuzzy0; num =KpRulepe+1pec+1; KpFuzzynum += eFuzzy1*ecFuzzy1; /*加权平均法解模糊*/ Kp_calcu=KpFuzzy0*kpRule0+KpFuzzy1*kpRule1+KpFuzzy2*kpRule2 +KpFuzzy3*kpRule3; return(Kp_calcu); /* 函数功能: PID 参数 Ki 的计算*/ float fuzzy_ki(float e, float ec) float Ki_calcu; uchar num,pe,pec; float code eRule7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0; float code ecRule7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0; float eFuzzy2=0.0,0.0; float ecFuzzy2=0.0,0.0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 17 页 - - - - - - - - - float code kiRule4=0.00,0.01,0.02,0.03; float KiFuzzy4=0.0,0.0,0.0,0.0; int code KiRule77= 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 2,0,0,0,0,0,1, 3,3,3,3,3,3,3 ; /* 误差隶属函数描述 */ if(eeRule0) eFuzzy0 =1.0; pe = 0; else if(eRule0=e & eeRule1) eFuzzy0 = (eRule1-e)/(eRule1-eRule0); pe = 0; else if(eRule1=e & eeRule2) eFuzzy0 = (eRule2 -e)/(eRule2-eRule1); pe = 1; else if(eRule2=e & eeRule3) eFuzzy0 = (eRule3 -e)/(eRule3-eRule2); pe = 2; else if(eRule3=e & eeRule4) eFuzzy0 = (eRule4-e)/(eRule4-eRule3); pe = 3; else if(eRule4=e & eeRule5) eFuzzy0 = (eRule5-e)/(eRule5-eRule4); pe = 4; else if(eRule5=e & eeRule6) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 17 页 - - - - - - - - - eFuzzy0 = (eRule6-e)/(eRule6-eRule5); pe = 5; else eFuzzy0 =0.0; pe =5; eFuzzy1 =1.0 - eFuzzy0; /* 误差变化隶属函数描述 */ if(ececRule0) ecFuzzy0 =1.0; pec = 0; else if(ecRule0=ec & ececRule1) ecFuzzy0 = (ecRule1 - ec)/(ecRule1-ecRule0); pec = 0 ; else if(ecRule1=ec & ececRule2) ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1); pec = 1; else if(ecRule2=ec & ececRule3) ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2); pec = 2 ; else if(ecRule3=ec & ececRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); pec=3; else if(ecRule4=ec & ececRule5) ecFuzzy0 = (ecRule5-ec)/(ecRule5-ecRule4); pec=4; else if(ecRule5=ec & ececRule6) ecFuzzy0 = (ecRule6-ec)/(ecRule6-ecRule5); pec=5; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 17 页 - - - - - - - - - ecFuzzy0 =0.0; pec = 5; ecFuzzy1 = 1.0 - ecFuzzy0; /*查询模糊规则表 */ num =KiRulepepec; KiFuzzynum += eFuzzy0*ecFuzzy0; num =KiRulepepec+1; KiFuzzynum += eFuzzy0*ecFuzzy1; num =KiRulepe+1pec; KiFuzzynum += eFuzzy1*ecFuzzy0; num =KiRulepe+1pec+1; KiFuzzynum += eFuzzy1*ecFuzzy1; /*加权平均法解模糊*/ Ki_calcu=KiFuzzy0*kiRule0+KiFuzzy1*kiRule1+KiFuzzy2*kiRule2 +KiFuzzy3*kiRule3; return(Ki_calcu); /* 函数功能: PID 参数 Kd 的计算*/ float fuzzy_kd(float e, float ec) float Kd_calcu; uchar num,pe,pec; float code eRule7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0; float code ecRule7=-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0; float eFuzzy2=0.0,0.0; float ecFuzzy2=0.0,0.0; float code kdRule4=0.0,1.0,2.0,3.0; float KdFuzzy4=0.0,0.0,0.0,0.0; int code KdRule77= 3,3,3,2,2,2,2, 2,2,2,1,1,1,1, 1,1,2,1,1,2,1, 1,1,0,1,0,1,1, 1,1,0,0,0,1,1, 2,2,1,0 ,1,1,1, 3,3,3,3,2,3,2 ; /* 误差隶属函数描述 */ if(eeRule0) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 17 页 - - - - - - - - - eFuzzy0 =1.0; pe = 0; else if(eRule0=e & eeRule1) eFuzzy0 = (eRule1-e)/(eRule1-eRule0); pe = 0; else if(eRule1=e & eeRule2) eFuzzy0 = (eRule2 -e)/(eRule2-eRule1); pe = 1; else if(eRule2=e & eeRule3) eFuzzy0 = (eRule3 -e)/(eRule3-eRule2); pe = 2; else if(eRule3=e & eeRule4) eFuzzy0 = (eRule4-e)/(eRule4-eRule3); pe = 3; else if(eRule4=e & eeRule5) eFuzzy0 = (eRule5-e)/(eRule5-eRule4); pe = 4; else if(eRule5=e & eeRule6) eFuzzy0 = (eRule6-e)/(eRule6-eRule5); pe = 5; else eFuzzy0 =0.0; pe =5; eFuzzy1 =1.0 - eFuzzy0; /* 误差变化隶属函数描述 */ if(ececRule0) ecFuzzy0 =1.0; pec = 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 17 页 - - - - - - - - - else if(ecRule0=ec & ececRule1) ecFuzzy0 = (ecRule1 - ec)/(ecRule1-ecRule0); pec = 0 ; else if(ecRule1=ec & ececRule2) ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1); pec = 1; else if(ecRule2=ec & ececRule3) ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2); pec = 2 ; else if(ecRule3=ec & ececRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); pec=3; else if(ecRule4=ec & ececRule5) ecFuzzy0 = (ecRule5-ec)/(ecRule5-ecRule4); pec=4; else if(ecRule5=ec & ececRule6) ecFuzzy0 = (ecRule6-ec)/(ecRule6-ecRule5); pec=5; else ecFuzzy0 =0.0; pec = 5; ecFuzzy1 = 1.0 - ecFuzzy0; /*查询模糊规则表 */ num =KdRulepepec; KdFuzzynum += eFuzzy0*ecFuzzy0; num =KdRulepepec+1; KdFuzzynum += eFuzzy0*ecFuzzy1; num =KdRulepe+1pec; KdFuzzynum += eFuzzy1*ecFuzzy0; num =KdRulepe+1pec+1; KdFuzzynum += eFuzzy1*ecFuzzy1; /*加权平均法解模糊 */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 17 页 - - - - - - - - - Kd_calcu=KdFuzzy0*kdRule0+KdFuzzy1*kdRule1+KdFuzzy2*kdRule2 +KdFuzzy3*kdRule3; return(Kd_calcu); /* 函数功能: AD 将采集到的温度进行转化*/ uint read_tlc2543(uchar port) static uchar PORT = 0; uchar Temp,i,k=0; uint AD_value=0; Temp = port; CS = 1; CLK = 0; Temp=4; CS = 0; while(1) for(i=0;i8;i+) CLK = 0; if(Temp&0 x80) SDI = 1; else SDI = 0; AD_value=1; if(SDO) AD_value |= 0 x01; CLK = 1; Temp=1; for(i=8;i12;i+) CLK = 0; AD_value=1; if(SDO) AD_value |= 0 x01; delay_us(10); CLK = 1; CLK = 0; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 17 页 - - - - - - - - - CS = 1; if(PORT = port) break; else Temp = port; Temp2) PORT = port; return AD_value; float AD_deal(void) uint AD_value; float temp; AD_value = read_tlc2543(0 x00); temp = AD_value*number; return temp; /*主函数 */ void main(void) uchar AD_value=0; flag=0; flag_start=0; ENA=1; IN1=0; IN2=0; LCD_Init(); LCD_display(); Timer2_Init(); while(1) if(flag_start=0) key_scan(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 17 页 - - - - - - - - - Show_Num(13,1,3,S_temp); Show_Num(3,1,3,P_temp); else if(flag_start=1) P_temp=AD_deal(); Show_Num(3,1,3,P_temp); PID_Calculate(); Kp=fuzzy_kp(Err/5,D_Err); /E 量化因子 5 Ki=fuzzy_ki(Err/5,D_Err); Kd=fuzzy_kd(Err/5,D_Err); Show_Num(2,2,2,Kp); Show_Ki(Ki); Show_Num(14,2,2,Kd); key_scan(); Show_Num(13,1,3,S_temp); /*定时器 2 中断*/ void timer2() interrupt 5 static uchar num1=0; TF2 = 0; num1+; if(num1PULSE) num1=0;ENA=1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 17 页 - - - - - - - - -