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 ST editor.

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

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

  • ST Expression

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

  • Variable

  • Address

The data type of the actual parameters must match the data type of the formal parameters. 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 with a value. You can see which formal parameter types must be assigned with a value 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 then 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)

      This way variables can be assigned to outputs using the => operator.

    • Informal call (call without formal parameter names)

Formal Call

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

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

Calling a procedure with formal parameter names:

Calling the same procedure in FBD:

With formal calls it is not necessary to assign a value to all formal parameters (see also Parameter).

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

Calling the same procedure in FBD:

Informal Call

With informal calls (call without formal parameter names), procedures are called using an instruction made from the procedure name, followed by a bracketed list of the inputs and outputs actual parameters. The order that the actual parameters are enumerated in a procedure call is significant.

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

Calling a procedure without formal parameter names:

Calling the same procedure in FBD:

With informal calls it is not necessary to assign a value to all formal parameters (see also Parameter).

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

An empty parameter field is used to skip a parameter.

Call with empty parameter field:

PROC (var1, , result1, result2) ;

Calling the same procedure in FBD:

An empty parameter field does not have to be used if formal parameters are omitted at the end.

PROC (var1, var2, result1) ;

Calling the same procedure in FBD:

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 successful execution of these algorithms, the value of ENO is set to "1". If an error occurs during execution of these algorithms, ENO will be 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".

The output behavior of the procedure does not depend on whether the function is called without EN or with EN=1.

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 restate the altered 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 ST:

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

Calling the same procedure in FBD:

VAR_IN_OUT variables cannot be used outside of the procedure call.

The following procedure calls are therefore invalid:

Invalid call, example 1:

InOutProc.inout := V1;

Assigning the variables V1 to a VAR_IN_OUT parameter.

Error: The operation cannot be executed since the VAR_IN_OUT parameter cannot be accessed outside of the procedure call.

Invalid call, example 2:

V1 := InOutProc.inout;

Assigning a VAR_IN_OUT parameter to the V1 variable.

Error: The operation cannot be executed since the VAR_IN_OUT parameter cannot be accessed outside of the procedure call.

The following procedure calls are always valid:

Valid call, example 1:

InOutProc (inout:=V1);

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 (V1);

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