




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、/*模糊PID控制溫控系統(tǒng)仿真設(shè)計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
2、;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;/* 函數(shù)功能:延時*/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) u
4、char 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
5、 = 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);/* 函數(shù)功能:顯示函數(shù)*/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); S
7、how_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);/* 函數(shù)功能:定時器2初始化*/void Timer2_Init() RCAP2H =
8、(65536-300)/256; RCAP2L =(65536-300)%256; TH2 = RCAP2H; TL2 = RCAP2L; ET2 = 1; TR2 = 1; EA = 1;/* 函數(shù)功能:鍵盤掃描,調(diào)整設(shè)置溫度*/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);/* 函數(shù)功能: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
10、; U=(int)U; if(U=0) if(U=200)U=200;flag=1; else U=-U;if(U=200)U=200;flag=0; /* 函數(shù)功能:PID參數(shù)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、函數(shù)描述*/ 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
13、-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隸屬函數(shù)描述*/ if(ececRule0)ecFuzzy0 =1.0;pec = 0;else if(ecRule0=ec & ececRule
14、1)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); pe
15、c=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; /*查詢模糊規(guī)則表*/num =KpRulepepec;KpFuzzynum += eFuzzy0*ecFuzzy0;num =KpRulepepec+1;
16、KpFuzzynum += eFuzzy0*ecFuzzy1;num =KpRulepe+1pec;KpFuzzynum += eFuzzy1*ecFuzzy0;num =KpRulepe+1pec+1;KpFuzzynum += eFuzzy1*ecFuzzy1;/*加權(quán)平均法解模糊*/ Kp_calcu=KpFuzzy0*kpRule0+KpFuzzy1*kpRule1+KpFuzzy2*kpRule2+KpFuzzy3*kpRule3;return(Kp_calcu);/* 函數(shù)功能:PID參數(shù)Ki的計算 */ float fuzzy_ki(float e, float ec) floa
17、t 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
18、,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; /*誤差隸屬函數(shù)描述*/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)/(e
19、Rule3-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;/*誤差變化隸屬函數(shù)描述*/
20、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(ecRul
21、e3=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; /*查詢模糊規(guī)則表*/num
22、 =KiRulepepec;KiFuzzynum += eFuzzy0*ecFuzzy0;num =KiRulepepec+1;KiFuzzynum += eFuzzy0*ecFuzzy1;num =KiRulepe+1pec;KiFuzzynum += eFuzzy1*ecFuzzy0;num =KiRulepe+1pec+1;KiFuzzynum += eFuzzy1*ecFuzzy1;/*加權(quán)平均法解模糊*/ Ki_calcu=KiFuzzy0*kiRule0+KiFuzzy1*kiRule1+KiFuzzy2*kiRule2+KiFuzzy3*kiRule3; return(Ki_ca
23、lcu);/* 函數(shù)功能:PID參數(shù)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
24、,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; /*誤差隸屬函數(shù)描述*/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-eRule
25、1);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;e
26、lseeFuzzy0 =0.0;pe =5; eFuzzy1 =1.0 - eFuzzy0; /*誤差變化隸屬函數(shù)描述*/ 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)ecFuz
27、zy0 = (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; elsee
28、cFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0; /*查詢模糊規(guī)則表*/num =KdRulepepec;KdFuzzynum += eFuzzy0*ecFuzzy0;num =KdRulepepec+1;KdFuzzynum += eFuzzy0*ecFuzzy1;num =KdRulepe+1pec;KdFuzzynum += eFuzzy1*ecFuzzy0;num =KdRulepe+1pec+1;KdFuzzynum += eFuzzy1*ecFuzzy1;/*加權(quán)平均法解模糊*/ Kd_calcu=KdFuzzy0*kdRule0+KdFuzzy1*kdRule1+KdFuzzy2*kdRule2+KdFuzzy3*kdRule3; return(Kd_calcu);/*函數(shù)功能:AD將采集到的溫度進行轉(zhuǎn)化*/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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- T-ZJWL 001-2024 大宗商品供應(yīng)鏈金融動產(chǎn)質(zhì)押監(jiān)管倉儲服務(wù)規(guī)范
- T-ZNZ 264.3-2024 重金屬中度污染農(nóng)田土壤修復(fù)和安全利用技術(shù)規(guī)范 第3部分:超積累東南景天與芝麻輪作
- 二零二五年度服裝品牌廠家全國代理合作協(xié)議
- 2025年度電影院包場租賃及廣告合作合同
- 二零二五年度金融科技股權(quán)投資合同協(xié)議
- 二零二五年度工傷事故賠償調(diào)解協(xié)議(含傷殘評定)
- 二零二五年度全包裝修合同模板含定制家具制作
- 2025年度離職賠償協(xié)議書中的離職員工離職通知及手續(xù)辦理
- 二零二五年度聯(lián)防聯(lián)控疫情防控科研合作合同
- 2025年度線上虛擬購物體驗合同書
- 生產(chǎn)車間管理制度辦法
- 機電企業(yè)管理導(dǎo)論第1章課件
- 水平一足球全冊教案
- 蘇教版科學(xué)二年級下冊全冊教案
- 約束評分標(biāo)準(zhǔn)
- GB/T 28799.2-2020冷熱水用耐熱聚乙烯(PE-RT)管道系統(tǒng)第2部分:管材
- 法律和道德的關(guān)系 課件
- GB 16780-2021水泥單位產(chǎn)品能源消耗限額
- 全面推進依法行政課件
- 政務(wù)服務(wù)一網(wǎng)通辦平臺解決方案-最新
- 第十四屆全國交通運輸行業(yè)職業(yè)技能競賽(公路收費及監(jiān)控員)賽項題庫-上(單選題匯總-共3部分-1)
評論
0/150
提交評論