```
' To start the program you must pass values for the defined parameters.
' Parameters can be either be defined statically within the program or
' passed to ADwin via TestPoint, Visual Basic, or LabVIEW.

' Define Integer Variables that can be passed in and out of ADwin
' registers
#DEFINE V_IN            PAR_9                   ' Measured input value
#DEFINE SETPOINT        PAR_10          ' Set point - desired output level
#DEFINE PROP            PAR_11          ' Proportional Constant for PID
#DEFINE V_OUT           PAR_12  ' Voltage output value
#DEFINE DELAY           PAR_13  ' Delay between loop executions

' Define Floating Point Variables that can be passed to/from ADwin
' registers
#DEFINE GAIN            FPAR_1  ' Gain of PID
#DEFINE INTGRL  FPAR_2  ' Integral Constant for PID
#DEFINE DERIV           FPAR_3  ' Derivative Constant for PID
#DEFINE FILTER  FPAR_9  ' Filter for error terms

DIM yd_old, w_old AS INTEGER            ' Hold iterative calculations
DIM sum AS INTEGER                      ' Sum of the error
DIM d_yd AS FLOAT                               ' Derivative constant for PID
DIM index AS INTEGER                    ' Array-Index
DIM DATA_1 AS INTEGER             ' Data array for filtered error term

INIT:                                           ' INIT section runs once at start
SET_MUX(0)                                      ' Set mux for ADC1 to Channel 1
sum = 0                                 ' Set the error term to zero
index = 1                                       ' Initialize array index

'---------------- ATTENTION:  INGRL and DELAY should not be zero. -----------
IF (INTGRL < 1) THEN INTGRL = 10000  ' INTGRL should not be less than 1
IF (DELAY < 40) THEN DELAY = 40              ' GLOBALDELAY no smaller than 40�s
GLOBALDELAY = DELAY                     ' Set GLOBALDELAY

EVENT:                                  ' EVENT define program loop
START_CONV(1)                           ' Start A/D conversion ADC1

'--------------------- The Control Calculation: ----------------------
'  y =      KR   * (   P   +     I       +      D      )
'  |        |        __|_    ____|____       ___|_____
'  |        |       /    \  /         \     /         \
V_OUT  =  GAIN  * ( PROP +  sum/(INTGRL) +  d_yd*DERIV)

DAC(1, V_OUT + 2048)                            ' Set DAC1 output level
yd_old = PROP                                   ' Save old value of PROP constant
WAIT_EOC(1)                                             ' Wait for end of conversion - ADC1
PROP = V_IN - SETPOINT                          ' Error = Present value - Set point
sum = sum + PROP                                        ' Calculate the sum of the error

'------ The limit for the sum of the error = 5000 * INTGRL / GAIN -----
IF (sum > 2000000) THEN sum = 2000000                ' Upper limit for sum
IF (sum <-2000000) THEN sum = -2000000               ' Lower limit for sum

d_yd = PROP - yd_old                            ' Derivative gain = new error - old

' ___________________________________________________________________
'/  This program will interrupt the PC when the error array is full! \
FILTER = V_IN*0.9999 + PROP*0.0001            ' Filter for error
IF (FILTER <> w_old) THEN                       ' Not equal?
DATA_1[index] = PROP                                ' Write variable to array
index = index + 1                                   ' Increment index
IF (index >= 2000) THEN                          ' Check array size
ACTIVATE_PC                                       ' Give TestPoint interrupt
index = 1                                         ' Reset counter
w_old = FILTER                                    ' Old value = New value
ENDIF
ENDIF
'\____________________________________________________________________/

FINISH:                                 ' FINISH section runs once at end
DAC (1, 2048)                                   ' Program DAC to 0V after completion

```

Example Program

NOTE: The example program provided is intended to illustrate the concepts presented in this note. The program may need to be altered in order to accommodate desired test parameters and timing.

Equipment List

1. PC running Windows® 95, 98, NT4, or 2000