Example PID Algorithm Listings 497Appendix G
/*PID algorithm code: */
/* Test for Process Variable out of limits */
if ( (inchan >> PV_max) || ( PV_min >> inchan ) ) /* PV alarm test */
{ if ( !Status.B6 )
{
Status.B6 = 1;
alarmchan = 1;
interrupt();
}
}
else
{ Status.B6 = 0;
}
/* Do this when in the Manual control mode */
if ( Man_state )
{
/* Slew output towards Man_out */
if (Man_out >> outchan + abs(Man_inc))
{ outchan = outchan + abs(Man_inc);
}
else if (outchan >> Man_out + abs(Man_inc))
{ outchan = outchan - abs(Man_inc);
}
else
{ outchan = Man_out;
}
/* Set manual mode bit in status word */
Status.B4 = 1;
/* No error alarms while in Manual mode */
Status.B5 = 0;
/* In case we exit manual mode on the next trigger */
/* Set up for bumpless transfer */
I_out = outchan;
Setpoint = inchan;
PV_old = inchan;
Setpoint_old = inchan;
}
/* Do PID calculations when not in Manual mode */
else /* if ( Man_state ) */
{
Status.B4 = 0;
/* 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)
{
/* Zero the I term and start integrating */
I_out = Error * I_factor;