In a project, there are several variable types:
Global - variables which can be used within a project, with correct references settings also between projects.
Variables related to functions and function blocks - variables, which always belong to a particular program, function, or function block. Further on, they are classified as inputs, outputs, and local variables. Inputs and outputs, unlike local variables, may be connected to another variable (global variable, input/output of a function block, etc.).
HW variables - variables belonging to a particular hardware device. They can not be used in programs, they must be mapped to global variables first. These are used in programs, containing the values of the hardware variables. The point is to simplify reconnecting of inputs/outputs in case of hardware damage.
Variables can be created one after another using function Create Global Variable which is available in the FUPLA Editor, or a whole bunch of variables can be edited comfortably in structured text (ST). Both ways can be combined, if a variable is created in FUPLA, the program text which defines the variable is changed accordingly automatically. Let's have a look at both ways:
In the FUPLA editor, right click into the ladder and select "Global variables", "Create global variable".
Create a variable as follows:
The variable is always bound to a project. If you want to use it in another project later, the original project must be referenced from the target project. Clicking OK creates a global variable. A folder named "Globals" has been generated automatically, with a subprogram named generated. If the program is opened, the generated variable can be seen in the program editor:
In this program, all variables created in the FUPLA editor will be generated. A variable can, of course, be created directly by editing the text of the program:
temp_ref_01, temp_ref_02: real;
This creates another variable named temp_ref_02; after the program is compiled, this variable can be inserted into the variable ladder.
If you wish to create a program with direct definition of variables, follow these steps:
Define global variables using ST in this program.
There may be more programs in one project where global variables are defined: the origin of variables is not relevant for the functionality.
Global retain variables are created in the same way as global variables. The retain variables are variables with backupped value in a non-volatile memory. Variables should be defined as retain if they are written to by a HMI rather than by a program - typically, they are setpoints, parameters, limits, etc.
By setting the retain property it is specified that even after the restart of the Runtime the variables will keep their values. Please note that the retain variables occupy the PLC's NVRAM memory and it is good to optimize their number.
In ST, declare the retain variables as follows:
temp_ref_01, temp_ref_02: real;
Every RETAIN variable is after each communication cycle (if there are more tasks, this applies for every task) copied into the RETAIN area. It is a dedicated part of memory which keeps the contents even after power outage.
At a warm restart (e.g. power outage) the RETAIN variables are initialised to values which are stored in the retain area before the task execution takes place. At a cold restart, the RETAIN variables are initialised to the default values taken from the source code.
RETAIN variables are used for example at operation hour counters, energy counters, and other values which shall be kept even after power outage or another manipulation.
At combinations of NON_RETAIN and RETAIN nested types, the highest flag is relevant. This means that user can override the type setting, and set RETAIN or NON_RETAIN for a particular variable. In this case, the setting of the nested items is not relevant, and all becomes RETAIN, or NON_RETAIN.
Abundant usage of RETAIN variables may effect the PLC performance, and should be considered carefully.