下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、-/*模糊PID控制溫控系統(tǒng)仿真設(shè)計(jì)C程序代碼*/ #in cludevreg52.h>#defi ne uchar un sig ned char#defi ne uint un sig ned int#defi ne P ULSE 200#defi ne number 0.035sbit SDO = P 29;sbit SDI = P21;sbit CS = P22;sbit CLK = P23;sbit EOC = P2M;sbit RS = P25;sbit RW = P26;sbit EN = P 2八7;sbit KEY仁 P 39;sbit KEY2= P 3"s
2、bit KEY3= P 3八2;sbit KEY4= P 3八3;sbit KEY5= P 3八4;sbit IN1 = P 3八5;sbit IN2 = P 3八6;sbit ENA = P 3八7;uchar flag;uchar flag_start;float S_tem p=60.0;float P_tem p=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ù)功能:延時(shí)*/void delay_ms(uc
3、har z) "uchar i;uchar j;for(i=z;i>0;i-)for(j=360;j>0;j-);void delay_us(uchar z) uchar i;for(i=z;i>0;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; vo
4、id Show_Num(uchar x,uchar y,uchar n, float num) "uchar a3;uchar i;uint Temp;Temp=(i nt)num;for(i=0;i vn ;i+)ai = Temp %10;Temp = Temp /10;if(y%2 = 1)LCD_WriteCOM(0x80+x);elseLCD_WriteCOM(0x80+0x40+x);for( i=n ;i>0;i-)LCD_WriteData(ai-1+0x30);void Show_Ki(uchar num _Ki) " "uchar Tem
5、p; num_Ki=Ki*100;Temp=(uchar) nu m_Ki;Show_Num(10,2,1,Te mp %10);Temp=Temp /10;Show_Num(9,2,1,Te mp %10);Temp=Temp /10;Show_Num(7,2,1,Te mp); void Show_char(uchar x,uchar y,uchar ch) "if(y%2 = 1)LCD_WriteCOM(0x80+x);elseLCD_WriteCOM(0x80+0x40+x);LCD_WriteData(ch); void LCD_I ni t(void) "RW
6、 = 0;EN = 0;LCD_WriteCOM(0x38);LCD_WriteCOM(0x0c);LCD_WriteCOM(0x06);LCD_WriteCOM(0x01); /*函數(shù)功能:顯示函數(shù)*/ void LCD_dis pl ay(void) Show_char(1,1,T); delay_us(10);Show_char(0,1,卩');delay_us(10);Show_char(1,1,T);delay_us(10);Show_char(2,1,':');delay_us(10);Show_Num(3,1,3,P_tem p);delay_us(10)
7、;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_tem p);delay_us(10);Show_char(0,2,卩');delay_us(10);Show_char(1,2,':');delay_us(10);Show_Num(2,2,2,K p);delay_us(10);Show_char(5,2,T);delay_us(10);Show_char(
8、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ù)功能:定時(shí)器2初始化*/ void Timer2_l ni t() "RCA P2H =(65536-300)/256;RCA P2L =(65536-300)%25
9、6;TH2 = RCA P2H;TL2 = RCA P2L;ET2 = 1;TR2 = 1;EA = 1;/*函數(shù)功能:鍵盤(pán)掃描,調(diào)整設(shè)置溫度*/ void key_sca n(v oid)"if(KEY1=0) delay_ms(1); if(KEY1=0)S_te mp=S_te mp+1; if(S_tem p>=200) S_temp=200;while(!KEY1); if(KEY2=0) delay_ms(1); if(KEY2=0)if(S_tem p>0) S_temp=S_temp-1;else if(S_te mpv=0)S_te mp=0;while
10、(!KEY2);if(KEY3=0) delay_ms(1); if(KEY3=0) if(S_tem pv=190)S_tem p=S_te mp +10; while(!KEY3); if(KEY4=0)delay_ms(1);if(KEY4=0)if(S_te mp >=10)S_tem p=S_te mp -10; while(!KEY4);if(KEY5=0)delay_ms(1);if(KEY5=0)flag_start=1; while(!KEY5);/*函數(shù)功能:PID的計(jì)算* void P ID_Calculate()"Err = S_te mp - P_te
11、mp;Sum_Err += Err;D Err = Err - Last Err;Last_Err = Err;U=K p*Err+Ki*Sum_Err+Kd*D_Err; U=( in t)U;if(U>=0) if(U>=200) U=200;flag=1; elseU=-U;if(U>=200)U=200;flag=0;-/*函數(shù)功能:PID參數(shù)Kp的計(jì)算*/e,ec表示誤差,誤差變化率float fuzzy_k p( float e, float ec)float Kp_calcu;uchar num,pe,p ec;float code eRule7=-3.0,-2
12、.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 的模糊論 域/隸屬于誤差E的隸屬程度/隸屬于誤差變化率EC的隸屬程度/Kp的模糊子集/隸屬于Kp的隸屬程度/Kp的模糊控制表float eFuzzy2=0.0,0.0;float ecFuzzy2=0.0,0.0;float code kp Rule4=0.0,8.0,16.0,24.0;float KpF uzzy4=0.0,0.0,0.0,0.0;in t code Kp Rule77=3,3,3,3,
13、3,332,2,2,2,1,221,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隸屬函數(shù)描述*/if(e<eRule0)eFuzzy0 =1.0;pe = 0;else if(eRule0<=e && e<eRule1)eFuzzy0 = (eRule1-e)/(eRule1-eRule0);pe = 0;else if(eRule1<=e && e<eRule2)eFuzzy0 = (eRule2 -e)/(eRule2-eRul
14、e1);pe = 1;else if(eRule2<=e && e<eRule3)-/eFuzzyO = (eRule3 -e)/(eRule3-eRule2); pe = 2;else if(eRule3v=e && e<eRule4) eFuzzy0 = (eRule4-e)/(eRule4-eRule3); pe = 3;else if(eRule4<=e && e<eRule5)eFuzzy0 = (eRule 5-e)/(eRule5-eRule4); pe = 4;else if(eRule5<=e
15、 && e<eRule6)eFuzzy0 = (eRule6-e)/(eRule6-eRule 5); pe = 5;elseeFuzzy0 =0.0;pe =5;eFuzzy1 =1.0 - eFuzzy0;/*誤差變化率EC隸屬函數(shù)描述*/ if(ec<ecRule0)ecFuzzy0 =1.0;pec = 0;else if(ecRule0<=ec && ec<ecRule1)ecFuzzy0 = (ecRule1 - ec)/(ecRule1-ecRule0); pec = 0 ;else if(ecRule1<=ec &a
16、mp;& ec<ecRule2)ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1); pec = 1;else if(ecRule2v=ec && ec<ecRule3)ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2); pec = 2 ;else if(ecRule3<=ec && ec<ecRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3);p ec=3;else if(ecRule4<=ec &
17、;& ec<ecRule5) ecFuzzy0 = (ecRule 5-ec)/(ecRule5-ecRule4);p ec=4;else if(ecRule 5<=ec && ec<ecRule 6) ecFuzzy0 = (ecRule 6-ec)/(ecRule6-ecRule 5);p ec=5;elseecFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0;/* 查詢(xún)模糊規(guī)則表*/num =Kp Rule pe pec;KpF uzzy num += eFuzzy0*ecFuzzy0;num =Kp R
18、ule pe pec+1;KpF uzzy num += eFuzzy0*ecFuzzy1;num =Kp Rule pe+1 pec;KpF uzzy num += eFuzzy1*ecFuzzy0;num =K pRule pe+1 pec+1;KpF uzzy num += eFuzzy1*ecFuzzy1;/*力口權(quán) 平 均 法 解 模 糊*/Kp_calcu=K pF uzzy0*k pRule0+K pF uzzy1*k pRule1+K pFuzzy2*k pRule 2+Kp Fuzzy3*k pRule3;return(Kp_calcu);/*函數(shù)功能:PID參數(shù)Ki的計(jì)算*
19、/ float fuzzy_ki(float e, float ec)float Ki_calcu;uchar num,pe,p ec;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;in t code KiRule77
20、=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,333,3,3,3;/*誤差隸屬函數(shù)描述*/if(e<eRule0)eFuzzy0 =1.0;pe = 0;else if(eRule0v=e && e<eRule1)eFuzzy0 = (eRule1-e)/(eRule1-eRule0); pe = 0;else if(eRule1<=e && e<eRule2)eFuzzy0 = (eRule2 -e)/(eRu
21、le2-eRule1); pe = 1;else if(eRule2<=e && e<eRule3)eFuzzy0 = (eRule3 -e)/(eRule3-eRule2); pe = 2;else if(eRule3<=e && e<eRule4) eFuzzy0 = (eRule4-e)/(eRule4-eRule3); pe = 3;else if(eRule4<=e && e<eRule5)eFuzzy0 = (eRule5-e)/(eRule5-eRule4); pe = 4;else if(eRu
22、le5<=e && e<eRule6)eFuzzyO = (eRule6-e)/(eRule6-eRule 5); pe = 5;elseeFuzzyO =0.0;pe =5;*/eFuzzy1 =1.0 - eFuzzy0;/*誤差變化隸屬函數(shù)描述if(ec<ecRule0)ecFuzzy0 =1.0;pec = 0;else if(ecRule0v=ec && ec<ecRule1)ecFuzzy0 = (ecRule1 - ec)/(ecRule1-ecRule0); pec = 0 ;else if(ecRule1<=ec
23、&& ec<ecRule2)ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1); pec = 1;else if(ecRule2v=ec && ec<ecRule3)ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2); pec = 2 ;else if(ecRule3<=ec && ec<ecRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); p ec=3;else if(ecRule4<=ec &
24、amp;& ec<ecRule5) ecFuzzy0 = (ecRule 5-ec)/(ecRule5-ecRule4); p ec=4;else if(ecRule 5<=ec && ec<ecRule 6) ecFuzzy0 = (ecRule 6-ec)/(ecRule6-ecRule5); p ec=5;elseecFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0;/*查詢(xún)模糊規(guī)則表 */num =KiRule pe pec;KiFuzzy num += eFuzzy0*ecFuzzy0;num =Ki
25、Rule pe pec+1;KiFuzzy num += eFuzzy0*ecFuzzy1;num =KiRule pe+1 pec;KiFuzzy num += eFuzzy1*ecFuzzy0;num =KiRule pe+1 pec+1;KiFuzzy num += eFuzzy1*ecFuzzy1;/*力口權(quán) 平 均 法 解 模 糊 */Ki_calcu=KiFuzzy0*kiRule0+KiFuzzy1*kiRule1+KiFuzzy2*kiRule2 +KiFuzzy3*kiRule3;return(Ki_calcu);/*函數(shù)功能:PID參數(shù)Kd的計(jì)算*/ float fuzzy
26、_kd(float e, float ec) "float Kd_calcu;uchar num,pe,p ec;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;in t code KdRule77=3,3,3,2,2
27、,22222,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(e<eRule0)-/eFuzzyO =1.0;pe = 0;else if(eRule0v=e && e<eRule1)eFuzzy0 = (eRule1-e)/(eRule1-eRule0); pe = 0;else if(eRule1<=e && e<eRule2)eFuzzy0 = (eRule2 -e)/(eRule2-eRule
28、1); pe = 1;else if(eRule2<=e && e<eRule3)eFuzzy0 = (eRule3 -e)/(eRule3-eRule2); pe = 2;else if(eRule3<=e && e<eRule4) eFuzzy0 = (eRule4-e)/(eRule4-eRule3); pe = 3;else if(eRule4<=e && e<eRule5)eFuzzy0 = (eRule 5-e)/(eRule5-eRule4); pe = 4;else if(eRule 5<
29、=e && e<eRule6)eFuzzy0 = (eRule6-e)/(eRule6-eRule 5); pe = 5;elseeFuzzy0 =0.0;pe =5;eFuzzy1 =1.0 - eFuzzy0;/*誤差變化隸屬函數(shù)描述*/if(ec<ecRule0)ecFuzzy0 =1.0;pec = 0;-/else if(ecRuleOv=ec && ecvecRule1)ecFuzzyO = (ecRule1 - ec)/(ecRule1-ecRule0); pec = 0 ;else if(ecRule1<=ec &&am
30、p; ec<ecRule2)ecFuzzyO = (ecRule2 - ec)/(ecRule2-ecRule1); pec = 1;else if(ecRule2v=ec && ec<ecRule3)ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2); pec = 2 ;else if(ecRule3<=ec && ec<ecRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); p ec=3;else if(ecRule4<=ec &&
31、; ec<ecRule5) ecFuzzy0 = (ecRule 5-ec)/(ecRule5-ecRule4); p ec=4;else if(ecRule 5 <=ec && ec<ecRule 6) ecFuzzy0 = (ecRule 6-ec)/(ecRule6-ecRule5); p ec=5;elseecFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0;/*詢(xún)模糊規(guī)則表 */num =KdRule pe pec;KdFuzzy num += eFuzzy0*ecFuzzy0;num =KdRule pe
32、pec+1;KdFuzzy num += eFuzzy0*ecFuzzy1;num =KdRule pe+1 pec;KdFuzzy num += eFuzzy1*ecFuzzy0;num =KdRule pe+1 pec+1;KdFuzzy num += eFuzzy1*ecFuzzy1;/* 力廿權(quán)平均法解模糊*/Kd_calcu=KdFuzzy0*kdRule0+KdFuzzy1*kdRule1+KdFuzzy2*kdRule2+KdFuzzy3*kdRule3;return(Kd_calcu);/*函數(shù)功能:AD將采集到的溫度進(jìn)行轉(zhuǎn)化*/ uin t read_tlc2543(uchar port)"static uchar P ORT = 0; uchar Temp ,i,k=0;uint AD_value=0;Temp = p ort;CS = 1;CLK = 0;Tempv <=4;CS = 0;while(1)for(i=0;i<8;i+) CLK = 0; if(Te mp&0x80)SDI = 1;el
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 喉部阻塞感的健康宣教
- JJF(陜) 080-2021 連續(xù)式路面平整度測(cè)定儀(非激光型)校準(zhǔn)規(guī)范
- JJF(陜) 032-2020 混凝土坍落度儀校準(zhǔn)規(guī)范
- 《收集需求》課件
- 年度班級(jí)活動(dòng)安排與總結(jié)計(jì)劃
- 【小學(xué)課件】學(xué)生安全教育主題班會(huì)
- 2024-2025學(xué)年年九年級(jí)數(shù)學(xué)人教版下冊(cè)專(zhuān)題整合復(fù)習(xí)卷第28章 銳角三角函數(shù) 解答題練習(xí)及答案
- 秋季學(xué)期家校合作計(jì)劃
- 雙酚A相關(guān)項(xiàng)目投資計(jì)劃書(shū)范本
- 中心靜脈導(dǎo)管相關(guān)項(xiàng)目投資計(jì)劃書(shū)范本
- 2024年湖北省中考語(yǔ)文真題(學(xué)生版+解析版)
- 財(cái)務(wù)內(nèi)部員工培訓(xùn)課件
- 《管理學(xué)原理》課程期末考試復(fù)習(xí)題庫(kù)(含答案)
- 學(xué)歷史的論文開(kāi)題報(bào)告
- 高鐵橋墩吊圍欄施工合同
- 告訴我地址 -從IPv4到IPv6的傳奇 課件 2024-2025學(xué)年清華大學(xué)版(2024)B版初中信息技術(shù)七年級(jí)上冊(cè)
- 2024年全新初二化學(xué)上冊(cè)期末試卷及答案(人教版)
- AI賦能企業(yè)新未來(lái)-探索智能化技術(shù)在企業(yè)中的應(yīng)用
- 2023-2024學(xué)年湖北省武漢市洪山區(qū)九年級(jí)(上)期末物理試卷(含答案)
- 四年級(jí)英語(yǔ)上冊(cè) 【期末詞匯】 期末詞匯專(zhuān)項(xiàng)檢測(cè)卷(一)(含答案)(人教PEP)
- 創(chuàng)業(yè)人生學(xué)習(xí)通超星期末考試答案章節(jié)答案2024年
評(píng)論
0/150
提交評(píng)論