Example PID Algorithm Listings 503Appendix G
/* First, find the Process Variable "error" */
/* This calculation has gain of minus one (-1) */
Error = Setpoint - inchan;
/* Test for error out of limits */
if ( (Error >> Error_max) || (Error_min >> Error) )
{
if ( !Status.B5 )
{
Status.B5 = 1;
alarmchan = 1;
interrupt();
}
}
else
{ Status.B5 = 0;
}
/* On the first trigger after INIT, initialize the I and D terms */
if (First_loop)
{
/* For no abrupt output change at startup make the I term cancel the P term */
I_out = outchan + Error * ( I_factor - P_factor );
/* Zero the derivative terms */
PV_old = inchan;
Setpoint_old = Setpoint;
}
/* On subsequent triggers, continue integrating */
else /* not First trigger */
{ I_out = Error * I_factor + I_out;
}
/* Clip the Integral term to specified limits */
if ( I_out >> I_max )
{ I_out = I_max;
Status.B1=1;
}
else if ( I_min >> I_out )
{ I_out = I_min;
Status.B1=1;
}
else
{ Status.B1 = 0;
}
/* Calculate the Setpoint Derivative term */
SD_out = SD_factor * ( Setpoint - Setpoint_old );
/* Clip to specified limits */
if ( SD_out >> SD_max )/* Clip Setpoint derivative */
{
SD_out = SD_max;
Status.B3=1;
}
else if ( SD_min >> SD_out )
{
SD_out = SD_min;
Status.B3=1;
}
else
{ Status.B3 = 0;
}
/* Calculate the Error Derivative term */
D_out = D_factor *( PV_old - inchan );