For the functions created by the user in the project or user library, a subprogram is created in the compiled program. Each time it is loaded, the input parameters of the classes VAR_INPUT and VAR_IN_OUT are transferred first. Then the corresponding subprogram is jumped into. Finally the output parameters of the classes VAR_OUTPUT and VAR_IN_OUT are read again. The number of subprograms or functions you may define depends on the type of controller.
For a user-defined function, only one subprogram is created. It can then be loaded from various points.
In the following example, we have used FP addresses in order to facilitate verification of the assembler code created from them. Because the program becomes more complicated when FP addresses are used, we recommend using variables with meaningful names for normal use.
The user-defined function User_ADD is loaded in the ladder diagram:
It has the following contents:
For the PLC, the following assembler code is created:
ST |
R9010 |
Transfer input parameters |
|
F0 |
(*MV*) |
R9010 always = TRUE |
|
DT0 |
DT0 -> User_ADD.In1 |
||
DT550 |
|||
F0 |
(*MV*) |
||
DT1 |
DT1 -> User_ADD.In2 |
||
DT551 |
|||
CALL |
0 |
User_ADD subprogram cue |
|
ST |
R9010 |
Reload output parameters |
|
F0 |
(*MV*) |
||
DT552 |
|||
DT2 |
User_ADD -> DT2 |
||
... |
|||
ED |
End of the main program |
||
SUB |
0 |
User_ADD subprogram |
|
ST |
R9010 |
||
F22 |
(*PLUS_S*) |
||
DT550 |
|||
DT551 |
|||
DT552 |
if (TRUE) |
||
UserAdd=UserAdd.In1 + UserAdd.In2 |
|||
RET |
backward jump to main program |