Applications will often require variables that retain their value between power cycles. The programmable Compax3 (T30, T40) offers this functionality through the use of RETAIN variables in the Codesys program.
Retain variables are declared as follows:
In the variable declaration window type "VAR RETAIN" and then close by typing "END_VAR", as shown below:
Any variable placed in this section of the declaration editor will have the same value upon power up that it did when powered down.
Retain variables can be placed in any program or Global Variable list. The declaration for Global variables is slightly different and is shown below. In the Global variable declaration window type "VAR_GLOBAL RETAIN" and then close by typing "END_VAR", as shown below:
Initialization of the variables can still be specified. The initialization syntax is identical to that of a standard variable. Shown below the variable iVarRetain1 will be initialized to 1.
The behavior of initialization of retain variables is different than standard variables. Standard variables will be assigned the initialization upon each power up. Retain variables will only be initialized according to the chart below. A "Reset" can be viewed as a power cycle.
The Compax3 has a limited amount of memory available for retain variables. The limits are shown in the chart below:
These limits apply to the TOTAL number of retain variables in the project. For example, having two retain INTs in a POU and one retain INT in a GVL is acceptable. Having two retain INTs in a POU and two retain INTs in a GVL is NOT acceptable. If the number of retain variables is exceeded an error will appear during compile.
VL Created: 20210617