專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機教程網(wǎng) >> MCU設(shè)計實例 >> 瀏覽文章

單片機PID控制算法的最簡單程序

作者:韓冰   來源:本站原創(chuàng)   點擊數(shù):  更新時間:2013年11月29日   【字體:

這個程序應(yīng)用在51單片機上的
/*************定義全局變量*****************/
    uchar  Kp,Ti,Td
    int    Set_speed;

/*************PID初始化函數(shù)***************/
void PID_init(void){
     Serror=0;
     FError=0;   
     Kp=30;
     Ti=500;
     Td=10;
}
  
int PID_control(int Now_speed)
{
    int Error,Serror,result;
    Error=Now_speed-Set_speed;
    Serror=Serror+Error; 
    result=(Kp*Error+Kp*0.05/Ti*Serror+Kp*Td/0.05*(Error-FError));
    FError=Error;
    //對占空比進行限幅處理
    if(result<10)
     {result=0;}  else
    if(result>1000)
     {result=1000;}
    return result;
}
 

 

關(guān)閉窗口

相關(guān)文章