溫度控制地PID算法_第1頁(yè)
溫度控制地PID算法_第2頁(yè)
溫度控制地PID算法_第3頁(yè)
溫度控制地PID算法_第4頁(yè)
已閱讀5頁(yè),還剩10頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、實(shí)用標(biāo)準(zhǔn)文案溫度控制的 PID 算法的 C 語(yǔ)言程序PID 算法的溫度控制系統(tǒng)89C51單片機(jī),通過(guò)鍵盤(pán)輸入預(yù)設(shè)值,與DS18B20測(cè)得的實(shí)際值做比較,然后驅(qū)動(dòng)制冷或加熱電路。用keil C語(yǔ)言來(lái)實(shí)現(xiàn) PID 的控制。/PID 算法溫控 C語(yǔ)言 2008-08-17 18:58#include<reg51.h>#include<intrins.h>#include<math.h>#include<string.h>struct PID unsigned int SetPoint; /設(shè)定目標(biāo) Desired Valueunsigned int P

2、roportion; /比例常數(shù) Proportional Constunsigned int Integral; /積分常數(shù) Integral Constunsigned int Derivative; /微分常數(shù) Derivative Constunsigned int LastError; / Error-1unsigned int PrevError; / Error-2unsigned int SumError; / Sums of Errors;struct PID spid; / PID Control Structureunsigned int rout; / PID Resp

3、onse (Output)unsigned int rin; / PID Feedback (Input)sbit data1=P10;sbit clk=P11;sbit plus=P20;sbit subs=P21;sbit stop=P22;sbit output=P34;sbit DQ=P33;unsigned char flag,flag_1=0;unsigned char high_time,low_time,count=0;/ 占空比調(diào)節(jié)參數(shù) unsigned char set_temper=35;精彩文檔實(shí)用標(biāo)準(zhǔn)文案unsigned char temper;unsigned ch

4、ar i;unsigned char j=0;unsigned int s;/*延時(shí)子程序 , 延時(shí)時(shí)間以 12M晶振為準(zhǔn) , 延時(shí)時(shí)間為 30us×time*/void delay(unsigned char time)unsigned char m,n;for(n=0;n<time;n+)for(m=0;m<2;m+)/*寫(xiě)一位數(shù)據(jù)子程序*/void write_bit(unsigned char bitval)EA=0;DQ=0; /* 拉低 DQ以開(kāi)始一個(gè)寫(xiě)時(shí)序 */if(bitval=1)_nop_();DQ=1; /* 如要寫(xiě) 1,則將總線(xiàn)置高 */delay

5、(5); /*延時(shí) 90us 供 DA18B20采樣 */DQ=1; /* 釋放 DQ總線(xiàn) */_nop_();_nop_();EA=1;/*寫(xiě)一字節(jié)數(shù)據(jù)子程序*/精彩文檔實(shí)用標(biāo)準(zhǔn)文案void write_byte(unsigned char val)unsigned char i;unsigned char temp;EA=0;/*關(guān)中斷 */TR0=0;for(i=0;i<8;i+) /*寫(xiě)一字節(jié)數(shù)據(jù),一次寫(xiě)一位*/temp=val>>i; /* 移位操作,將本次要寫(xiě)的位移到最低位 */ temp=temp&1;write_bit(temp); /*向總線(xiàn)寫(xiě)該位

6、*/delay(7); /*延時(shí) 120us 后*/ TR0=1;EA=1; /* 開(kāi)中斷 */*讀一位數(shù)據(jù)子程序*/unsigned char read_bit()unsigned char i,value_bit;EA=0;DQ=0; /* 拉低 DQ,開(kāi)始讀時(shí)序 */_nop_();_nop_();DQ=1; /* 釋放總線(xiàn) */for(i=0;i<2;i+)value_bit=DQ;EA=1;return(value_bit);/*讀一字節(jié)數(shù)據(jù)子程序精彩文檔實(shí)用標(biāo)準(zhǔn)文案*/ unsigned char read_byte() unsigned char i,value=0;EA=

7、0;for(i=0;i<8;i+)if(read_bit() /*讀一字節(jié)數(shù)據(jù),一個(gè)時(shí)序中讀一次,并作移位處理*/value|=0x01<<i;delay(4); /*延時(shí) 80us 以完成此次都時(shí)序,之后再讀下一數(shù)據(jù)*/EA=1;return(value);/*復(fù)位子程序*/unsigned char reset()unsigned char presence;EA=0;DQ=0; /* 拉低 DQ總線(xiàn)開(kāi)始復(fù)位 */delay(30); /*保持低電平 480us*/DQ=1; /* 釋放總線(xiàn) */delay(3);presence=DQ; /* 獲取應(yīng)答信號(hào) */dela

8、y(28); /*延時(shí)以完成整個(gè)時(shí)序 */EA=1;return(presence); /*返回應(yīng)答信號(hào),有芯片應(yīng)答返回0, 無(wú)芯片則返回1*/*獲取溫度子程序*/void get_temper()精彩文檔實(shí)用標(biāo)準(zhǔn)文案unsigned char i,j;doi=reset(); /*復(fù)位 */while(i!=0); /*1為無(wú)反饋信號(hào) */i=0xcc; /*發(fā)送設(shè)備定位命令 */write_byte(i);i=0x44; /*發(fā)送開(kāi)始轉(zhuǎn)換命令 */write_byte(i);delay(180); /*延時(shí) */doi=reset(); /*復(fù)位 */while(i!=0);i=0xcc;

9、 /*設(shè)備定位 */write_byte(i);i=0xbe; /*讀出緩沖區(qū)內(nèi)容 */write_byte(i);j=read_byte();i=read_byte();i=(i<<4)&0x7f;s=(unsigned int)(j&0x0f);s=(s*100)/16;j=j>>4;temper=i|j; /*獲取的溫度放在 temper 中 */*=Initialize PID Structure=*/void PIDInit (struct PID *pp)精彩文檔實(shí)用標(biāo)準(zhǔn)文案memset ( pp,0,sizeof(struct PID);/

10、*=PID 計(jì)算部分=*/unsigned int PIDCalc( struct PID *pp, unsigned int NextPoint )unsigned int dError,Error;Error = pp->SetPoint - NextPoint; /偏差pp->SumError += Error; /積分dError = pp->LastError - pp->PrevError; /當(dāng)前微分pp->PrevError = pp->LastError;pp->LastError = Error;return (pp->Pro

11、portion * Error/比例+ pp->Integral * pp->SumError /積分項(xiàng)+ pp->Derivative * dError); /微分項(xiàng)/*溫度比較處理子程序*/compare_temper()unsigned char i;if(set_temper>temper)if(set_temper-temper>1)high_time=100;low_time=0;else精彩文檔實(shí)用標(biāo)準(zhǔn)文案for(i=0;i<10;i+) get_temper();rin = s; / Read Inputrout = PIDCalc ( &a

12、mp;spid,rin ); / Perform PID Interationif (high_time<=100)high_time=(unsigned char)(rout/800);elsehigh_time=100;low_time= (100-high_time);else if(set_temper<=temper)if(temper-set_temper>0)high_time=0;low_time=100;elsefor(i=0;i<10;i+) get_temper();rin = s; / Read Inputrout = PIDCalc ( &am

13、p;spid,rin ); / Perform PID Interationif (high_time<100)high_time=(unsigned char)(rout/10000);elsehigh_time=0;low_time= (100-high_time);/ else精彩文檔實(shí)用標(biāo)準(zhǔn)文案/ /*T0 中斷服務(wù)子程序,用于控制電平的翻轉(zhuǎn) ,40us*100=4ms 周期*/void serve_T0() interrupt 1 using 1if(+count<=(high_time)output=1;else if(count<=100)output=0;el

14、secount=0;TH0=0x2f;TL0=0xe0;/*串行口中斷服務(wù)程序,用于上位機(jī)通訊*/void serve_sio() interrupt 4 using 2/* EA=0;RI=0;i=SBUF;if(i=2)while(RI=0)RI=0;set_temper=SBUF;SBUF=0x02;while(TI=0)TI=0;精彩文檔實(shí)用標(biāo)準(zhǔn)文案else if(i=3)TI=0;SBUF=temper;while(TI=0)TI=0;EA=1; */void disp_1(unsigned char disp_num16)unsigned char n,a,m;for(n=0;n&

15、lt;6;n+)/ k=disp_num1n; for(a=0;a<8;a+)clk=0;m=(disp_num1n&1);disp_num1n=disp_num1n>>1; if(m=1)data1=1;elsedata1=0;_nop_();clk=1;_nop_();/*顯示子程序功能:將占空比溫度轉(zhuǎn)化為單個(gè)字符,顯示占空比和測(cè)得到的溫度精彩文檔實(shí)用標(biāo)準(zhǔn)文案*/void display()unsigned char codenumber=0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6;unsigned char

16、 disp_num6;unsigned int k,k1;k=high_time;k=k%1000;k1=k/100;if(k1=0)disp_num0=0;elsedisp_num0=0x60;k=k%100;disp_num1=numberk/10;disp_num2=numberk%10;k=temper;k=k%100;disp_num3=numberk/10;disp_num4=numberk%10+1;disp_num5=numbers/10;disp_1(disp_num);/*主程序*/main()unsigned char z;unsigned char a,b,flag_2

17、=1,count1=0;unsigned char phil=2,0xce,0x6e,0x60,0x1c,2;TMOD=0x21;TH0=0x2f;精彩文檔實(shí)用標(biāo)準(zhǔn)文案TL0=0x40;SCON=0x50;PCON=0x00;TH1=0xfd;TL1=0xfd;PS=1;EA=1;EX1=0;ET0=1;ES=1;TR0=1;TR1=1;high_time=50;low_time=50;PIDInit ( &spid ); / Initialize Structurespid.Proportion = 10; / Set PID Coefficientsspid.Integral =

18、8;spid.Derivative =6;spid.SetPoint = 100; / Set PID Setpointwhile(1)if(plus=0)EA=0;for(a=0;a<5;a+)for(b=0;b<102;b+)if(plus=0)set_temper+;flag=0;else if(subs=0)精彩文檔實(shí)用標(biāo)準(zhǔn)文案for(a=0;a<5;a+)for(b=0;a<102;b+)if(subs=0)set_temper-;flag=0;else if(stop=0)for(a=0;a<5;a+)for(b=0;b<102;b+)if(st

19、op=0)flag=0;break;EA=1;get_temper();b=temper;if(flag_2=1)a=b;if(abs(a-b)>5)temper=a;elsetemper=b;a=temper;flag_2=0;if(+count1>30)display();count1=0;精彩文檔實(shí)用標(biāo)準(zhǔn)文案compare_temper();TR0=0;z=1;while(1)EA=0;if(stop=0)for(a=0;a<5;a+)for(b=0;b<102;b+)if(stop=0)disp_1(phil);/ break;EA=1;/DS18b20 子程序#include <REG52.H>sbit DQ=P21;/定義端口typedef unsigned char byte;typedef unsigned intword;/延時(shí)void delay(word useconds)for(;useconds>0;useconds-);/復(fù)位by

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論