模糊PID控制溫控系統(tǒng)設(shè)計C語言程序代碼_第1頁
模糊PID控制溫控系統(tǒng)設(shè)計C語言程序代碼_第2頁
模糊PID控制溫控系統(tǒng)設(shè)計C語言程序代碼_第3頁
模糊PID控制溫控系統(tǒng)設(shè)計C語言程序代碼_第4頁
模糊PID控制溫控系統(tǒng)設(shè)計C語言程序代碼_第5頁
已閱讀5頁,還剩12頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、/*模糊PID控制溫控系統(tǒng)仿真設(shè)計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ù)功能:延時*/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ù)功能:定時器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的計算*/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的計算 */ 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的計算 */ 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的計算 */ 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將采集到的溫度進行轉(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等.壓縮文件請下載最新的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)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論