Procedure

Procedures are provided in the form of libraries. The logic of the procedure is created in the programming language C and may not be modified in the IL editor.

Procedures - like functions - have no internal states. If the input values are the same, the value on the output is the same every time the procedure is executed. For example, the addition of two values gives the same result every time.

In contrast to functions, procedures do not return a value and support VAR_IN_OUT variables.

Procedures are a supplement to IEC 61131-3 and must be enabled explicitly.

Parameter

"Inputs and outputs" are required to transfer values to or from procedures. These are called formal parameters.

The current process states are transferred to the formal parameters. These are called actual parameters.

The following can be used as actual parameters for procedure inputs:

  • Variable

  • Address

  • Literal

The following can be used as actual parameters for procedure outputs:

  • Variable

  • Address

The data type of the actual parameter must match the data type of the formal parameter. The only exceptions are generic formal parameters whose data type is determined by the actual parameter.

When dealing with generic ANY_BIT formal parameters, actual parameters of the INT or DINT (not UINT and UDINT) data types can be used.

This is a supplement to IEC 61131-3 and must be enabled explicitly.

Example:

Allowed:

AND (AnyBitParam := IntVar1, AnyBitParam2 := IntVar2)

Not allowed:

AND_WORD (WordParam1 := IntVar1, WordParam2 := IntVar2)

(In this case, AND_INT must be used.)

AND_ARRAY_WORD (ArrayInt, ...)

(In this case an explicit type conversion must be carried out using INT_ARR_TO_WORD_ARR (...).

Not all formal parameters must be assigned a value for formal calls. Which formal parameter types must be assigned a value can be seen in the following table.

Parameter type

EDT

STRING

ARRAY

ANY_ARRAY

IODDT

STRUCT

FB

ANY

Input

-

-

+

+

+

+

+

+

VAR_IN_OUT

+

+

+

+

+

+

/

+

Output

-

-

-

-

-

-

/

+

+ Actual parameter required

- Actual parameter not required

/ not applicable

If no value is allocated to a formal parameter, then the initial value will be used for executing the function block. If no initial value has been defined, the default value (0) is used.

Programming Notes

Attention should be paid to the following programming notes:

  • Procedures are only executed if the input EN=1 or the EN input is not used (see also EN and ENO).

  • Special conditions apply when using VAR_IN_OUT variables.

  • There are two ways of calling a procedure:

    • Formal call (calling a function with formal parameter names)

      In this case variables can be assigned to outputs using the => operator (calling a function block in shortened form).

    • Informal call (calling a function without formal parameter names)

Formal Call

With this type of call (call with formal parameter names), the procedure is called using an optional CAL instruction sequence followed by the name of the procedure and a bracketed list of actual parameter to formal parameter assignments. The assignment of the input formal parameter is made using the := assignment and the output formal parameter is made using the => assignment. The order in which the input formal parameters and output formal parameters are listed is not significant.

The list of actual parameters may be wrapped immediately following a comma.

EN and ENO can be used for this type of call.

Calling a procedure with formal parameter names:

or

CAL PROC (IN1:=var1, IN2:=var1, OUT1=>result1,OUT2=>result2)

or

PROC (IN1:=var1,
      IN2:=var1,
      OUT1=>result1,
      OUT2=>result2)

or

CAL PROC (IN1:=var1,
      IN2:=var1,
      OUT1=>result1,
      OUT2=>result2)

Calling the same procedure in FBD:

With formal calls, values do not have to be assigned to all formal parameters (see also Parameter).

PROC (IN1:=var1, OUT1=>result1, OUT2=>result2)

or

CAL PROC (IN1:=var1, OUT1=>result1, OUT2=>result2)

Calling the same procedure in FBD:

Informal Call without CAL Instruction

With this type of call (call without formal parameter names), procedures are called using an instruction sequence consisting of the first actual parameter loaded into the accumulator, followed by the procedure name, followed by a list of the input and output actual parameters. The order in which the actual parameters are listed is significant. The list of actual parameters cannot be wrapped.

EN and ENO cannot be used for this type of call.

Calling a procedure with formal parameter names:

Calling the same procedure in FBD:

NOTE: Note that when making an informal call, the list of actual parameters cannot be put in brackets. IEC 61133-3 requires that the brackets be left out in this case to illustrate that the first actual parameter is not a part of the list.

Invalid informal call for a procedure:

If the value to be processed (first actual parameter) is already in the accumulator, the load instruction can be omitted.

EXAMP1 var2,result1,result2

Informal Call with CAL Instruction

With this type of call, procedures are called using an instruction sequence consisting of the CAL instruction, followed by the procedure name followed by a list of the input and output actual parameters. The order in which the actual parameters are listed is significant. The list of actual parameters cannot be wrapped.

EN and ENO cannot be used for this type of call.

Calling a procedure with formal parameter names using CAL instruction:

or

CAL PROC (var1,
     var2,
     result1,
     result2)

Calling the same procedure in FBD:

NOTE: Unlike informal calls without a CAL instruction, when making informal calls with a CAL instruction, the value to be processed (first actual parameter) is not explicitly loaded in the battery. Instead it is part of the list of actual parameters. For this reason, when making informal calls with a CAL instruction, the list of actual parameters must be put in brackets.

EN and ENO

With all procedures, an EN input and an ENO output can be configured.

If the value of EN is equal to "0" when the procedure is called, the algorithms defined by the procedure are not executed and ENO is set to "0".

If the value of EN is "1" when the procedure is called, the algorithms defined by the function are executed. After the algorithms have been executed successfully, the value of ENO is set to "1". If an error occurs when executing these algorithms, ENO is set to "0".

If the EN pin is not assigned a value, when the FFB is invoked, the algorithm defined by the FFB is executed (same as if EN equals to "1").

If ENO is set to "0" (caused when EN=0 or an error occurred during executing), the outputs of the procedure are set to "0".

If EN/ENO are used, the procedure call must be formal. The assignment of variables to ENO must be made using the => operator.

PROC (EN:=1, IN1:=var1, IN2:=var2,
        ENO=>error, OUT1=>result1, OUT2=>result2) ;

Calling the same procedure in FBD:

VAR_IN_OUT Variable

Procedures are often used to read a variable at an input (input variables), to process it and to output the updated values of the same variable (output variables). This special type of input/output variable is also called a VAR_IN_OUT variable.

The following special features are to be noted when using procedures with VAR_IN_OUT variables.

  • All VAR_IN_OUT inputs must be assigned a variable.

  • VAR_IN_OUT inputs may not have literals or constants assigned to them.

  • VAR_IN_OUT outputs may not have values assigned to them.

  • VAR_IN_OUT variables cannot be used outside of the procedure call.

Calling a procedure with VAR_IN_OUT variable in IL:

PROC3 (IN1:=V1, IN2:=V2, IO1:=V3,
         OUT1=>V4, OUT2=>V5) ;

Calling the same procedure in FBD:

VAR_IN_OUT variables cannot be used outside the procedure call.

The following procedure calls are therefore invalid:

Invalid call, example 1:

LD V1

Loading a V1 variable in the accumulator

CAL InOutProc

Calling a procedure with the VAR_IN_OUT parameter.

The accumulator now contains a reference to a VAR_IN_OUT parameter.

AND V2

AND operation on contents of accumulator with variable V2.

Error: The operation cannot be carried out since the VAR_IN_OUT parameter (contents of accumulator) cannot be accessed outside the procedure call.

Invalid call, example 2:

LD V1

Loading a V1 variable in the accumulator

AND InOutProc.inout

AND operation on the contents of the accumulator and a reference to a VAR_IN_OUT parameter.

Fehler: The operation cannot be carried out since the VAR_IN_OUT parameter cannot be accessed outside the procedure call.

Invalid call, example 3:

LD V1

Loading a V1 variable in the accumulator

InOutFB V2

Calling the procedure with assignment of the actual parameter (V2) to the VAR_IN_OUT parameter.

Error: The operation cannot be carried out as with this type of procedure call only the VAR_IN_OUT parameter would be stored in the accumulator for later use.

The following procedure calls are always valid:

Valid call, example 1:

CAL InOutProc (IN1:=V1,inout:=V2)

Calling a procedure with the VAR_IN_OUT parameter and formal assignment of the actual parameter within the procedure call.

Valid call, example 2:

InOutProc (IN1:=V1,inout:=V2)

Calling a procedure with the VAR_IN_OUT parameter and formal assignment of the actual parameter within the procedure call.

Valid call, example 3:

CAL InOutProc (V1,V2)

Calling a procedure with the VAR_IN_OUT parameter and informal assignment of the actual parameter within the procedure call.