The simplest microcontroller PID control algorithm program

/****************** Global Variables Definition *****************/

unsigned char Kp, Ti, Td;

int Set_Speed;

/****************** PID Initialization Function *****************/

void PID_Init(void)

{

Error = 0;

Last_Error = 0;

Kp = 30;

Ti = 500;

Td = 10;

}

/****************** PID Control Function *****************/

int PID_Control(int Now_Speed)

{

int Error, Last_Error, Result;

Error = Now_Speed - Set_Speed;

Integral += Error;

Result = (Kp * Error) + (Kp * 0.05 / Ti * Integral) + (Kp * Td / 0.05 * (Error - Last_Error));

Last_Error = Error;

// Limit the output to a specific range

if (Result < 0)

{

Result = 0;

}

else if (Result > 1000)

{

Result = 1000;

}

return Result;

}

This PID control function is commonly used in motor speed regulation systems. The proportional term (Kp) adjusts the current error, the integral term (Ti) helps eliminate steady-state error over time, and the derivative term (Td) predicts future errors based on the rate of change of the error. By tuning these parameters, you can achieve better control performance and stability. It's important to ensure that the output is within a valid range to prevent over-driving the system or causing instability.

Contact Switch

Contact Switch,Reed Contact Switch,Magnetic Contact,Magnetic Contact Switch

Shanghai Janetec Electric Co., Ltd. , https://www.janetecelectric.com