模糊PID控制温控系统设计C语言程序代码.doc_第1页
模糊PID控制温控系统设计C语言程序代码.doc_第2页
模糊PID控制温控系统设计C语言程序代码.doc_第3页
模糊PID控制温控系统设计C语言程序代码.doc_第4页
模糊PID控制温控系统设计C语言程序代码.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*模糊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) 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+0x30); 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(0x80+x); else LCD_WriteCOM(0x80+0x40+x); LCD_WriteData(ch); void LCD_Init(void) RW = 0; EN = 0; LCD_WriteCOM(0x38); LCD_WriteCOM(0x0c); LCD_WriteCOM(0x06); LCD_WriteCOM(0x01);/* 函数功能:显示函数*/void LCD_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初始化*/void Timer2_Init() RCAP2H =(65536-300)/256; RCAP2L =(65536-300)%256; 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; /* 函数功能: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)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;elseeFuzzy0 =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 ; 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; elseecFuzzy0 =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; 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)eFuzzy0 = (eRule6-e)/(eRule6-eRule5);pe = 5;elseeFuzzy0 =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; 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; 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)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;elseeFuzzy0 =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; 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; 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;i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论