版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(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程序代碼*/#include<reg52.h>#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
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;/* 函數(shù)功能:延時(shí)*/void delay_ms(uchar z) uchar i; uchar
3、 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;void Show_Num(uchar x,u
4、char y,uchar n,float num) uchar a3; uchar i; uint Temp; Temp=(int)num; for(i=0;i<n;i+) ai = Temp%10; Temp = Temp/10; if(y%2 = 1) LCD_WriteCOM(0x80+x); else LCD_WriteCOM(0x80+0x40+x); for(i=n;i>0;i-) LCD_WriteData(ai-1+0x30); 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(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);
6、 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_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,
7、39;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'); del
8、ay_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ù)功能:定時(shí)器2初始化*/void Timer2_Init() RCAP2H =(65536-300)/25
9、6; 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_temp>0)S_temp=S_temp-1;else if(S_temp<=0)S_te
10、mp=0;while(!KEY2);if(KEY3=0) delay_ms(1);if(KEY3=0)if(S_temp<=190)S_temp=S_temp+10;while(!KEY3); if(KEY4=0) delay_ms(1);if(KEY4=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的計(jì)算*/void PID_Calculate() Err = S_temp - P_temp;
11、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; /* 函數(shù)功能:PID參數(shù)Kp的計(jì)算 */ float fuzzy_kp(float e, float ec)/e,ec,表示誤差,誤差變化率 float Kp_calcu;uchar num,pe,pec; float code eRule7=-3.0,
12、-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,
13、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隸屬函數(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
14、-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(eRule5&
15、lt;=e && e<eRule6)eFuzzy0 = (eRule6-e)/(eRule6-eRule5);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
16、 && ec<ecRule2)ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1);pec = 1;else if(ecRule2<=ec && ec<ecRule3)ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2);pec = 2 ; else if(ecRule3<=ec && ec<ecRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); pec=3; else if(ecRule4<=e
17、c && ec<ecRule5) ecFuzzy0 = (ecRule5-ec)/(ecRule5-ecRule4); pec=4; else if(ecRule5<=ec && ec<ecRule6) ecFuzzy0 = (ecRule6-ec)/(ecRule6-ecRule5); pec=5; elseecFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0; /*查詢模糊規(guī)則表*/num =KpRulepepec;KpFuzzynum += eFuzzy0*ecFuzzy0;num =KpRule
18、pepec+1;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的計(jì)算 */ float fuzzy_ki(float e, float
19、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
20、,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; /*誤差隸屬函數(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-eRule1);pe = 1;else
21、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(eRule5<=e && e<
22、;eRule6)eFuzzy0 = (eRule6-e)/(eRule6-eRule5);pe = 5;elseeFuzzy0 =0.0;pe =5; eFuzzy1 =1.0 - eFuzzy0;/*誤差變化隸屬函數(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 && ec<ecRule
23、2)ecFuzzy0 = (ecRule2 - ec)/(ecRule2-ecRule1);pec = 1;else if(ecRule2<=ec && ec<ecRule3)ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2);pec = 2 ; else if(ecRule3<=ec && ec<ecRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); pec=3; else if(ecRule4<=ec && ec<ecRul
24、e5) ecFuzzy0 = (ecRule5-ec)/(ecRule5-ecRule4); pec=4; else if(ecRule5<=ec && ec<ecRule6) ecFuzzy0 = (ecRule6-ec)/(ecRule6-ecRule5); pec=5; elseecFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0; /*查詢模糊規(guī)則表*/num =KiRulepepec;KiFuzzynum += eFuzzy0*ecFuzzy0;num =KiRulepepec+1;KiFuzzynum += eF
25、uzzy0*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_calcu);/* 函數(shù)功能:PID參數(shù)Kd的計(jì)算 */ float fuzzy_kd(float e, float ec) float Kd_calcu;ucha
26、r 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
27、,1,0,0,0,1,1,2,2,1,0 ,1,1,1,3,3,3,3,2,3,2; /*誤差隸屬函數(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-eRule1);pe = 1;else if(eRule2<=e &&
28、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(eRule5<=e && e<eRule6)eFuzzy0 = (eRule6-e
29、)/(eRule6-eRule5);pe = 5;elseeFuzzy0 =0.0;pe =5; eFuzzy1 =1.0 - eFuzzy0; /*誤差變化隸屬函數(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 && ec<ecRule2)ecFuzzy0 = (ecRule2 - ec
30、)/(ecRule2-ecRule1);pec = 1;else if(ecRule2<=ec && ec<ecRule3)ecFuzzy0 = (ecRule3 - ec)/(ecRule3-ecRule2);pec = 2 ; else if(ecRule3<=ec && ec<ecRule4) ecFuzzy0 = (ecRule4-ec)/(ecRule4-ecRule3); pec=3; else if(ecRule4<=ec && ec<ecRule5) ecFuzzy0 = (ecRule5-ec
31、)/(ecRule5-ecRule4); pec=4; else if(ecRule5<=ec && ec<ecRule6) ecFuzzy0 = (ecRule6-ec)/(ecRule6-ecRule5); pec=5; elseecFuzzy0 =0.0;pec = 5;ecFuzzy1 = 1.0 - ecFuzzy0; /*查詢模糊規(guī)則表*/num =KdRulepepec;KdFuzzynum += eFuzzy0*ecFuzzy0;num =KdRulepepec+1;KdFuzzynum += eFuzzy0*ecFuzzy1;num =KdRule
32、pe+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將采集到的溫度進(jìn)行轉(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<8;i+) CLK = 0; if(Temp&0x80) SDI = 1; else SDI =
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝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ù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《消化系統(tǒng)護(hù)理課件》課件
- 【2021高考復(fù)習(xí)參考】高三數(shù)學(xué)(理)配套黃金練習(xí):10-7
- 【先學(xué)后教新思路】2020高考物理一輪復(fù)習(xí)-教案8-電路-閉合電路歐姆定律
- 一年級(jí)反義詞100個(gè)
- 湖北省恩施州高中教育聯(lián)盟2024年秋季學(xué)期高一年級(jí)期末考試物理試題(含答案)
- 【高效提能】2021高考?xì)v史(人教版)一輪作業(yè):第七單元-資本主義世界市場(chǎng)的形成和發(fā)展
- 【名師一號(hào)】2021年岳麓歷史必修3-雙基限時(shí)練21
- 五年級(jí)數(shù)學(xué)(小數(shù)乘除法)計(jì)算題專項(xiàng)練習(xí)及答案匯編
- 四年級(jí)數(shù)學(xué)(三位數(shù)乘兩位數(shù))計(jì)算題專項(xiàng)練習(xí)及答案
- 二年級(jí)數(shù)學(xué)計(jì)算題專項(xiàng)練習(xí)集錦
- 浙江省2023年1月學(xué)業(yè)考試物理物理試題(解析版)
- 一年級(jí)數(shù)學(xué)加減法口算題每日一練(25套打印版)
- 黑河黃藏寺水利樞紐工程環(huán)境影響評(píng)價(jià)報(bào)告書-黑河流域管理局
- 2024-2025學(xué)年五年級(jí)科學(xué)上冊(cè)第二單元《地球表面的變化》測(cè)試卷(教科版)
- 幸福創(chuàng)業(yè)智慧樹知到期末考試答案2024年
- GB/T 13912-2020金屬覆蓋層鋼鐵制件熱浸鍍鋅層技術(shù)要求及試驗(yàn)方法
- 縣領(lǐng)導(dǎo)在新錄用公務(wù)員培訓(xùn)班開班典禮上的講話
- 2022年工會(huì)會(huì)計(jì)制度——會(huì)計(jì)科目和會(huì)計(jì)報(bào)表
- 搞笑小品劇本《家長(zhǎng)駕到》臺(tái)詞完整版 金牌喜劇班李海銀高海寶盛偉
- (完整版)高級(jí)管理人員年薪制管理辦法
- 教學(xué)管理系統(tǒng)業(yè)務(wù)流程圖
評(píng)論
0/150
提交評(píng)論