2022年模糊PID控制温控系统设计方案C语言程序代码.docx
精品学习资源/*模糊 PID 掌握温控系统仿真设计 C 程序代码*/ #include<reg52.h>#define uchar unsigned char#define uint unsigned int #define PULSE 200#define number 0.035sbit 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_msuchar z欢迎下载精品学习资源uchar i;uchar j;fori=z ;i>0;i- forj=360;j>0;j- ;void delay_usuchar zuchar i;fori=z ;i>0;i- ;void LCD_WriteDatauchar DatRS = 1;P1 = Dat;delay_us10;EN = 1;delay_us10;EN = 0;void LCD_WriteCOMuchar comRS = 0;P1 = com;delay_us10;EN = 1;delay_us10;EN = 0;void Show_Numuchar x,uchar y,uchar n,float numuchar a3;uchar i;uint Temp;Temp=intnum;fori=0 ;i<n;i+ai = Temp%10;Temp = Temp/10;ify%2 = 1欢迎下载精品学习资源LCD_WriteCOM0x80+x ;elseLCD_WriteCOM0x80+0x40+x ;fori=n ;i>0;i- LCD_WriteDataai-1+0x30 ;void Show_Kiuchar num_Kiuchar Temp;num_Ki=Ki*100 ;Temp=ucharnum_Ki;Show_Num10,2,1,Temp%10;Temp=Temp/10;Show_Num9,2,1,Temp%10;Temp=Temp/10;Show_Num7,2,1,Temp;void Show_charuchar x,uchar y,uchar chify%2 = 1LCD_WriteCOM0x80+x ;elseLCD_WriteCOM0x80+0x40+x ;LCD_WriteDatach;void LCD_InitvoidRW = 0;EN = 0;LCD_WriteCOM0x38 ;LCD_WriteCOM0x0c ;LCD_WriteCOM0x06 ;LCD_WriteCOM0x01 ;/*函数功能:显示函数*/ void LCD_displayvoidShow_char1,1,'T';delay_us10;欢迎下载精品学习资源Show_char0,1,'P';delay_us10;Show_char1,1,'T';delay_us10;Show_char2,1,':';delay_us10;Show_Num3,1,3,P_temp;delay_us10;Show_char10,1,'S';delay_us10;Show_char11,1,'T';delay_us10;Show_char12,1,':';delay_us10;Show_Num13,1,3,S_temp;delay_us10;Show_char0,2,'P';delay_us10;Show_char1,2,':';delay_us10;Show_Num2,2,2,Kp;delay_us10;Show_char5,2,'I';delay_us10;Show_char6,2,':';delay_us10;Show_char8,2,'.';delay_us10; Show_KiKi ;delay_us10;Show_char12,2,'D';delay_us10;Show_char13,2,':';delay_us10;Show_Num14,2,2,Kd;delay_us10;/*函数功能:定时器 2 初始化*/ void Timer2_InitRCAP2H =65536-300/256;欢迎下载精品学习资源RCAP2L =65536-300%256;TH2 = RCAP2H;TL2 = RCAP2L ;ET2 = 1;TR2 = 1;EA = 1;/*函数功能:键盘扫描,调整设置温度*/ void key_scanvoidifKEY1=0delay_ms1;ifKEY1=0S_temp=S_temp+1;ifS_temp>=200S_temp=200;while.KEY1 ;ifKEY2=0delay_ms1;ifKEY2=0ifS_temp>0S_temp=S_temp-1;else ifS_temp<=0S_temp=0;while.KEY2 ;ifKEY3=0delay_ms1;ifKEY3=0ifS_temp<=190S_temp=S_temp+10;while.KEY3 ;欢迎下载精品学习资源ifKEY4=0delay_ms1;ifKEY4=0ifS_temp>=10S_temp=S_temp-10;while.KEY4 ;ifKEY5=0delay_ms1;ifKEY5=0flag_start=1;while.KEY5 ;/*函数功能: PID 的运算*/ void PID_CalculateErr = 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=intU ;ifU>=0欢迎下载精品学习资源elseifU>=200U=200;flag=1;U=-U ;ifU>=200U=200;欢迎下载精品学习资源flag=0;/*函数功能: PID 参数 Kp 的运算*/float fuzzy_kpfloat 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 隶属函数描述 */ ife<eRule0eFuzzy0 =1.0;pe = 0;else ifeRule0<=e && e<eRule1eFuzzy0 = eRule1-e/eRule1-eRule0 ;pe = 0;else ifeRule1<=e && e<eRule2欢迎下载精品学习资源eFuzzy0 = eRule2 -e/eRule2-eRule1 ;pe = 1;else ifeRule2<=e && e<eRule3eFuzzy0 = eRule3 -e/eRule3-eRule2 ;pe = 2;else ifeRule3<=e && e<eRule4eFuzzy0 = eRule4-e/eRule4-eRule3 ;pe = 3;else ifeRule4<=e && e<eRule5eFuzzy0 = eRule5-e/eRule5-eRule4 ;pe = 4;else ifeRule5<=e && e<eRule6欢迎下载精品学习资源elseeFuzzy0 = eRule6-e/eRule6-eRule5 ;pe = 5;eFuzzy0 =0.0;pe =5;欢迎下载精品学习资源eFuzzy1 =1.0 - eFuzzy0;/*误差变化率 EC 隶属函数描述 */ ifec<ecRule0ecFuzzy0 =1.0;pec = 0;else ifecRule0<=ec && ec<ecRule1ecFuzzy0 = ecRule1 - ec/ecRule1-ecRule0;pec = 0 ;else ifecRule1<=ec && ec<ecRule2ecFuzzy0 = ecRule2 - ec/ecRule2-ecRule1;pec = 1;欢迎下载精品学习资源else ifecRule2<=ec && ec<ecRule3ecFuzzy0 = ecRule3 - ec/ecRule3-ecRule2;pec = 2 ;else ifecRule3<=ec && ec<ecRule4ecFuzzy0 = ecRule4-ec/ecRule4-ecRule3 ;pec=3;else ifecRule4<=ec && ec<ecRule5ecFuzzy0 = ecRule5-ec/ecRule5-ecRule4 ;pec=4;else ifecRule5<=ec && ec<ecRule6ecFuzzy0 = ecRule6-ec/ecRule6-ecRule5 ;pec=5;欢迎下载精品学习资源elseecFuzzy0 =0.0;pec = 5;欢迎下载精品学习资源ule2ecFuzzy1 = 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*kpR欢迎下载精品学习资源+KpFuzzy3*kpRule3 ;returnKp_calcu;/*函数功能: PID 参数 Ki 的运算*/ float fuzzy_kifloat 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 ;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 ;/*误差隶属函数描述 */ ife<eRule0eFuzzy0 =1.0;pe = 0;else ifeRule0<=e && e<eRule1eFuzzy0 = eRule1-e/eRule1-eRule0 ;pe = 0;else ifeRule1<=e && e<eRule2eFuzzy0 = eRule2 -e/eRule2-eRule1 ;pe = 1;else ifeRule2<=e && e<eRule3eFuzzy0 = eRule3 -e/eRule3-eRule2 ;pe = 2;else ifeRule3<=e && e<eRule4eFuzzy0 = eRule4-e/eRule4-eRule3 ;pe = 3;else ifeRule4<=e && e<eRule5欢迎下载精品学习资源eFuzzy0 = eRule5-e/eRule5-eRule4 ;pe = 4;else ifeRule5<=e && e<eRule6欢迎下载精品学习资源elseeFuzzy0 = eRule6-e/eRule6-eRule5 ;pe = 5;eFuzzy0 =0.0;pe =5;欢迎下载精品学习资源eFuzzy1 =1.0 - eFuzzy0;/*误差变化隶属函数描述 */ ifec<ecRule0ecFuzzy0 =1.0;pec = 0;else ifecRule0<=ec && ec<ecRule1ecFuzzy0 = ecRule1 - ec/ecRule1-ecRule0;pec = 0 ;else ifecRule1<=ec && ec<ecRule2ecFuzzy0 = ecRule2 - ec/ecRule2-ecRule1;pec = 1;else ifecRule2<=ec && ec<ecRule3ecFuzzy0 = ecRule3 - ec/ecRule3-ecRule2;pec = 2 ;else ifecRule3<=ec && ec<ecRule4ecFuzzy0 = ecRule4-ec/ecRule4-ecRule3 ;pec=3;else ifecRule4<=ec && ec<ecRule5ecFuzzy0 = ecRule5-ec/ecRule5-ecRule4 ;pec=4;欢迎下载精品学习资源else ifecRule5<=ec && ec<ecRule6ecFuzzy0 = ecRule6-ec/ecRule6-ecRule5 ;pec=5;欢迎下载精品学习资源elseecFuzzy0 =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 ;returnKi_calcu;/*函数功能: PID 参数 Kd 的运算*/ float fuzzy_kdfloat e, float ecfloat 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 ;/*误差隶属函数描述 */ ife<eRule0eFuzzy0 =1.0;pe = 0;else ifeRule0<=e && e<eRule1eFuzzy0 = eRule1-e/eRule1-eRule0 ;pe = 0;else ifeRule1<=e && e<eRule2eFuzzy0 = eRule2 -e/eRule2-eRule1 ;pe = 1;else ifeRule2<=e && e<eRule3eFuzzy0 = eRule3 -e/eRule3-eRule2 ;pe = 2;else ifeRule3<=e && e<eRule4eFuzzy0 = eRule4-e/eRule4-eRule3 ;pe = 3;else ifeRule4<=e && e<eRule5eFuzzy0 = eRule5-e/eRule5-eRule4 ;pe = 4;else ifeRule5<=e && e<eRule6欢迎下载精品学习资源elseeFuzzy0 = eRule6-e/eRule6-eRule5 ;pe = 5;eFuzzy0 =0.0;pe =5;欢迎下载精品学习资源eFuzzy1 =1.0 - eFuzzy0;/*误差变化隶属函数描述 */ ifec<ecRule0ecFuzzy0 =1.0;pec = 0;else ifecRule0<=ec && ec<ecRule1ecFuzzy0 = ecRule1 - ec/ecRule1-ecRule0;pec = 0 ;else ifecRule1<=ec && ec<ecRule2ecFuzzy0 = ecRule2 - ec/ecRule2-ecRule1;pec = 1;else ifecRule2<=ec && ec<ecRule3ecFuzzy0 = ecRule3 - ec/ecRule3-ecRule2;pec = 2 ;else ifecRule3<=ec && ec<ecRule4ecFuzzy0 = ecRule4-ec/ecRule4-ecRule3 ;pec=3;else ifecRule4<=ec && ec<ecRule5ecFuzzy0 = ecRule5-ec/ecRule5-ecRule4 ;pec=4;else ifecRule5<=ec && ec<ecRule6ecFuzzy0 = ecRule6-ec/ecRule6-ecRule5 ;pec=5;欢迎下载精品学习资源elseecFuzzy0 =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 ;/*加权平均法解模糊 */Kd_calcu=KdFuzzy0*kdRule0+KdFuzzy1*kdRule1+KdFuzzy2*kdRule2+KdFuzzy3*kdRule3 ;returnKd_calcu;/*函数功能: AD 将采集到的温度进行转化*/ uint read_tlc2543uchar portstatic uchar PORT = 0;uchar Temp,i,k=0;uint AD_value=0;Temp = port;CS = 1;CLK = 0 ;Temp<<=4;CS = 0;while1fori=0 ;i<8;i+CLK = 0 ;ifTemp&0x80SDI = 1;elseSDI = 0;AD_value<<=1;ifSDOAD_value |= 0x01;CLK = 1 ;Temp<<=1;fori=8;i<12;i+欢迎下载精品学习资源CLK = 0 ;AD_value<<=1;ifSDOAD_value |= 0x01;delay_us10; CLK = 1 ; CLK = 0 ;CS = 1;ifPORT = port break;else欢迎下载精品学习资源k+;ifk>2Temp = port;Temp<<=4;delay_us10;CS = 0;AD_value = 0;欢迎下载精品学习资源PORT = port;return AD_value;float AD_dealvoiduint AD_value;float temp;AD_value = read_tlc25430x00;temp = AD_value*number;return temp;/*主函数 */ void mainvoiduchar AD_value=0;flag=0;flag_start=0;ENA=1 ;IN1=0;欢迎下载精品学习资源IN2=0;LCD_Init ;LCD_display ;Timer2_Init ;while1ifflag_start=0key_scan;Show_Num13,1,3,S_temp;Show_Num3,1,3,P_temp;else ifflag_start=1P_temp=AD_deal;Show_Num3,1,3,P_temp;PID_Calculate;Kp=fuzzy_kpErr/5,D_Err ;/E 量化因子 5Ki=fuzzy_kiErr/5,D_Err ;Kd=fuzzy_kdErr/5,D_Err ;Show_Num2,2,2,Kp;Show_KiKi ;Show_Num14,2,2,Kd;key_scan;Show_Num13,1,3,S_temp;/*定时器 2 中断*/ void timer2 interrupt 5static uchar num1=0;TF2 = 0;num1+;ifnum1<=U欢迎下载精品学习资源ifflag=1 IN1 = 0;elseIN2 = 1;欢迎下载精品学习资源IN1 = 1;欢迎下载精品学习资源IN2 = 0;elseENA=0;ifnum1>PULSEnum1=0;ENA=1 ;欢迎下载