微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PID算法调节C51程序(5)

PID算法调节C51程序(5)

时间:11-23 来源:互联网 点击:
typedef struct PIDValue
{
uint32 Ek_Uint32[3]; //差值保存,给定和反馈的差值
uint8 EkFlag_Uint8[3]; //符号,1则对应的Ek[i]为负数,0为对应的Ek[i]为正数
uint8 KP_Uint8;
uint8 KI_Uint8;
uint8 KD_Uint8;
uint8 B_Uint8; //死区电压

uint8 KP; //显示修改的时候用
uint8 KI; //
uint8 KD; //
uint8 B; //
uint16 Uk_Uint16; //上一时刻的控制电压
}PIDValueStr;

PIDValueStr xdata PID;

void PIDProcess(void)
{
uint32 idata Temp[3]; //
uint32 idata PostSum; //正数和
uint32 idata NegSum; //负数和
Temp[0] = 0;
Temp[1] = 0;
Temp[2] = 0;
PostSum = 0;
NegSum = 0;
if( ADPool.Value_Uint16[UINADCH] > ADPool.Value_Uint16[UFADCH] ) //给定大于反馈,则EK为正数
{
Temp[0] = ADPool.Value_Uint16[UINADCH] - ADPool.Value_Uint16[UFADCH]; //计算Ek[0]
if( Temp[0] > PID.B_Uint8 )
{
//数值移位
PID.Ek_Uint32[2] = PID.Ek_Uint32[1];
PID.Ek_Uint32[1] = PID.Ek_Uint32[0];
PID.Ek_Uint32[0] = Temp[0];
//符号移位
PID.EkFlag_Uint8[2] = PID.EkFlag_Uint8[1];
PID.EkFlag_Uint8[1] = PID.EkFlag_Uint8[0];
PID.EkFlag_Uint8[0] = 0; //当前EK为正数
Temp[0] = (uint32)PID.KP_Uint8 * PID.Ek_Uint32[0]; // KP*EK0
Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[1]; // KI*EK1
Temp[2] = (uint32)PID.KD_Uint8 * PID.Ek_Uint32[2]; // KD*EK2
}
}
else //反馈大于给定
{
Temp[0] = ADPool.Value_Uint16[UFADCH] - ADPool.Value_Uint16[UINADCH]; //计算Ek[0]
if( Temp[0] > PID.B_Uint8 )
{
//数值移位
PID.Ek_Uint32[2] = PID.Ek_Uint32[1];
PID.Ek_Uint32[1] = PID.Ek_Uint32[0];
PID.Ek_Uint32[0] = Temp[0];
//符号移位
PID.EkFlag_Uint8[2] = PID.EkFlag_Uint8[1];
PID.EkFlag_Uint8[1] = PID.EkFlag_Uint8[0];
PID.EkFlag_Uint8[0] = 1; //当前EK为负数
Temp[0] = (uint32)PID.KP_Uint8 * PID.Ek_Uint32[0]; // KP*EK0
Temp[1] = (uint32)PID.KI_Uint8 * PID.Ek_Uint32[1]; // KI*EK1
Temp[2] = (uint32)PID.KD_Uint8 * PID.Ek_Uint32[2]; // KD*EK2
}
}

if(PID.EkFlag_Uint8[0]==0)
{
PostSum += Temp[0]; //正数和
}
else
{
NegSum += Temp[0]; //负数和
} // KP*EK0
if(PID.EkFlag_Uint8[1]!=0)
{
PostSum += Temp[1]; //正数和
}
else
{
NegSum += Temp[1]; //负数和
} // - kI * EK1
if(PID.EkFlag_Uint8[2]==0)
{
PostSum += Temp[2]; //正数和
}
else
{
NegSum += Temp[2]; //负数和
} // KD * EK2
PostSum += (uint32)PID.Uk_Uint16; //
if( PostSum > NegSum ) // 是否控制量为正数
{
Temp[0] = PostSum - NegSum;
if( Temp[0] < (uint32)ADPool.Value_Uint16[UMAXADCH] ) //小于限幅值则为计算值输出
{
PID.Uk_Uint16 = (uint16)Temp[0];
}
else
{
PID.Uk_Uint16 = ADPool.Value_Uint16[UMAXADCH]; //否则为限幅值输出
}
}
else //控制量输出为负数,则输出0
{
PID.Uk_Uint16 = 0;
}
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top