2022年模糊PID控制温控系统设计C语言程序代码 .pdf
《2022年模糊PID控制温控系统设计C语言程序代码 .pdf》由会员分享,可在线阅读,更多相关《2022年模糊PID控制温控系统设计C语言程序代码 .pdf(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= P3
2、2; 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)
3、 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 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;
4、 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
5、; 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_WriteCO
6、M(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_
7、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);
8、 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 =(65
9、536-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; wh
10、ile(!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(
11、) 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 页 - - - - - - - - - /
12、* 函数功能: 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; /隶
13、属于误差变化率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
14、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 -
15、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;
16、/* 误差变化率 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-
17、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
18、(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; nu
19、m =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,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年模糊PID控制温控系统设计C语言程序代码 2022 模糊 PID 控制 温控 系统 设计 语言 程序代码
限制150内