/********************************************************************模糊PID控制温控系统仿真设计C程序代码********************************************************************/#includereg52.h#defineucharunsignedchar#defineuintunsignedint#definePULSE200#definenumber0.035sbitSDO=P2^0;sbitSDI=P2^1;sbitCS=P2^2;sbitCLK=P2^3;sbitEOC=P2^4;sbitRS=P2^5;sbitRW=P2^6;sbitEN=P2^7;sbitKEY1=P3^0;sbitKEY2=P3^1;sbitKEY3=P3^2;sbitKEY4=P3^3;sbitKEY5=P3^4;sbitIN1=P3^5;sbitIN2=P3^6;sbitENA=P3^7;ucharflag;ucharflag_start;floatS_temp=60.0;floatP_temp=20.0;floatKp;floatKi;floatKd;floatErr=0.0;floatLast_Err=0.0;floatD_Err=0.0;floatSum_Err=0.0;floatU=0.0;/******************************函数功能:延时******************************/voiddelay_ms(ucharz){uchari;ucharj;for(i=z;i0;i--)for(j=360;j0;j--);}voiddelay_us(ucharz){uchari;for(i=z;i0;i--);}voidLCD_WriteData(ucharDat){RS=1;P1=Dat;delay_us(10);EN=1;delay_us(10);EN=0;}voidLCD_WriteCOM(ucharcom){RS=0;P1=com;delay_us(10);EN=1;delay_us(10);EN=0;}voidShow_Num(ucharx,uchary,ucharn,floatnum){uchara[3];uchari;uintTemp;Temp=(int)num;for(i=0;in;i++){a[i]=Temp%10;Temp=Temp/10;}if(y%2==1)LCD_WriteCOM(0x80+x);elseLCD_WriteCOM(0x80+0x40+x);for(i=n;i0;i--)LCD_WriteData(a[i-1]+0x30);}voidShow_Ki(ucharnum_Ki){ucharTemp;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);}voidShow_char(ucharx,uchary,ucharch){if(y%2==1)LCD_WriteCOM(0x80+x);elseLCD_WriteCOM(0x80+0x40+x);LCD_WriteData(ch);}voidLCD_Init(void){RW=0;EN=0;LCD_WriteCOM(0x38);LCD_WriteCOM(0x0c);LCD_WriteCOM(0x06);LCD_WriteCOM(0x01);}/***********************************函数功能:显示函数*************************************/voidLCD_display(void){Show_char(1,1,'T');delay_us(10);Show_char(0,1,'P');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初始化*************************************/voidTimer2_Init(){RCAP2H=(65536-300)/256;RCAP2L=(65536-300)%256;TH2=RCAP2H;TL2=RCAP2L;ET2=1;TR2=1;EA=1;}/*************************************函数功能:键盘扫描,调整设置温度****************************************/voidkey_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;elseif(S_temp=0)S_temp=0;while(!KEY2);}}if(KEY3==0){delay_ms(1);if(KEY3==0){if(S_temp=190)S_temp=S_temp+10;while(!KEY3);}}if(KEY4==0){delay_ms(1);if(KEY4==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的计算**********************************/voidPID_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;}}/***********************************************函数功能:PID参数Kp的计算************************************************/floatfuzzy_kp(floate,floatec)//e,ec,表示误差,误差变化率{floatKp_calcu;ucharnum,pe,pec;floatcodeeRule[7]={-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0};//误差E的模糊论域floatcodeecRule[7]={-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0};//误差变化率EC的模糊论域floateFuzzy[2]={0.0,0.0};//隶属于误差E的隶属程度floatecFuzzy[2]={0.0,0.0};//隶属于误差变化率EC的隶属程度floatcodekpRule[4]={0.0,8.0,16.0,24.0};//Kp的模糊子集floatKpFuzzy[4]={0.0,0.0,0.0,0.0};//隶属于Kp的隶属程度intcodeKpRule[7][7]=//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(eeRule[0]){eFuzzy[0]=1.0;pe=0;}elseif(eRule[0]=e&&eeRule[1]){eFuzzy[0]=(eRule[1]-e)/(eRule[1]-eRule[0]);pe=0;}elseif(eRule[1]=e&&eeRule[2]){eFuzzy[0]=(eRule[2]-e)/(eRule[2]-eRule[1]);pe=1;}elseif(eRule[2]=e&&eeRule[3]){eFuzzy[0]=(eRule[3]-e)/(eRule[3]-eRule[2]);pe=2;}elseif(eRule[3]=e&&eeRule[4]){eFuzzy[0]=(eRule[4]-e)/(eRule[4]-eRule[3]);pe=3;}elseif(eRule[4]=e&&eeRule[5]){eFuzzy[0]=(eRule[5]-e)/(eRule[5]-eRule[4]);pe=4;}elseif(eRule[5]=e&&eeRule[6]){eFuzzy[0]=(eRule[6]-e)/(eRule[6]-eRule[5]);pe=5;}else{eFuzzy[0]=0.0;pe=5;}eFuzzy[1]=1.0-eFuzzy[0];/*****误差变化率EC隶属函数描述*****/if(ececRule[0]){ecFuzzy[0]=1.0;pec=0;}elseif(ecRule[0]=ec&&ececRule[1]){ecFuzzy[0]=(ecRule[1]-ec)/(ecRule[1]-ecRule[0]);pec=0;}elseif(ecRule[1]=ec&&ececRule[2]){ecFuzzy[0]=(ecRule[2]-ec)/(ecRule[2]-ecRule[1]);pec=1;}elseif(ecRule[2]=ec&&ececRule[3]){ecFuzzy[0]=(ecRule[3]-ec)/(ecRule[3]-ecRule[2]);pec=2;}elseif(ecRule[3]=ec&&ececRule[4]){ecFuzzy[0]=(ecRule[4]-ec)/(ecRule[4]-ecRule[3]);pec=3;}elseif(ecRule[4]=ec&&ececRule[5]){ecFuzzy[0]=(ecRu