模糊PID自动控制控制温控系统设计C语言知识程序代码.doc
《模糊PID自动控制控制温控系统设计C语言知识程序代码.doc》由会员分享,可在线阅读,更多相关《模糊PID自动控制控制温控系统设计C语言知识程序代码.doc(17页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-/*模糊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= P3
2、3;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
3、;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)
4、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%
5、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
6、_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);
7、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
8、=(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
9、_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_Er
10、r; 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
11、的模糊论域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隶
12、属函数描述*/ 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 = (eRule
13、4-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 & ececRul
14、e1)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); p
15、ec=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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 模糊 隐约 依稀 模胡 pid 自动控制 控制 节制 温控 系统 设计 语言 知识 程序代码
限制150内