資源描述:
《直流電機pi控制-pwm部分》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、直流電機PI控制-PWM部分PI_PWM部分#include#include#includebitflag=1;unsignedcharpwm=0;unsignedchara,b;sbitP21=P2^1;sbitP20=P2^0;voidsdf()interrupt3using0//定時255us,0.063s{P1=0x00;if(flag==1){a--;if(a==0){flag=0;a=1;}else{P21=0;}}if(flag==0){b--;if(b==0){flag
2、=1;b=253;}else{P21=1;}}}main(){TMOD=0x20;TH1=0x06;//對TH0TL0賦值TL1=0x06;P20=1;ET1=1;TF1=0;b=1;a=253;TR1=1;EA=1;while(1);}PID控制程序#include#include#include#includestructPID{unsignedintSetPoint;/*設(shè)定目標(biāo)DesiredValue*/unsignedintProportion;/*比例常
3、數(shù)ProportionalConst*/unsignedintIntegral;/*積分常數(shù)IntegralConst*/unsignedintDerivative;/*微分常數(shù)DerivativeConst*/unsignedintLastError;/*Error[-1]*/unsignedintPrevError;/*Error[-2]*/unsignedintSumError;/*SumsofErrors*/};structPIDspid;/*PIDControlStructure*/unsignedintrout;/*PIDRes
4、ponse(Output)*/unsignedintrin;/*PIDFeedback(Input)*/sbitdata1=P1^0;sbitclk=P1^1;sbitplus=P2^0;sbitsubs=P2^1;sbitstop=P2^2;sbitoutput=P3^4;sbitDQ=P3^3;unsignedcharflag,flag_1=0;unsignedcharhigh_time,low_time,count=0;/*占空比調(diào)節(jié)參數(shù)*/unsignedcharset_temper=35;unsignedchartemper;un
5、signedchari;unsignedcharj=0;unsignedints;/********************************************************************PID計算部分********************************************************************/unsignedintPIDCalc(structPID*pp,unsignedintNextPoint){unsignedintdError,Error;Error=pp-
6、>SetPoint-NextPoint;//偏差pp->SumError+=Error;//積分dError=pp->LastError-pp->PrevError;//當(dāng)前微分pp->PrevError=pp->LastError;pp->LastError=Error;return(pp->Proportion*Error//比例項+pp->Integral*pp->SumEror//積分項+pp->Derivative*dError);//微分項}low_time=(100-high_time);}}elseif(set_temper
7、<=temper){if(temper-set_temper>0){high_time=0;low_time=100;}else{for(i=0;i<10;i++){get_temper();rin=s;//ReadInputrout=PIDCalc(&spid,rin);//PerformPIDInteration}if(high_time<100)high_time=(unsignedchar)(rout/10000);elsehigh_time=0;low_time=(100-high_time);}}//else//{}}break
8、;}EA=1;}get_temper();b=temper;if(flag_2==1)a=b;if((abs(a-b))>5)temper=a;elsetemper=b;a=te