Instructions

Overview

Instructions describe what to do with the given expressions.

The following instructions can be used in ST:

Instruction

Example

assignment

A:=B; CV := CV + 1; C:=SIN(X);

Calling a function block and using the function block output

CMD_TMR(IN := %IX5, PT := 300);
A:=CMD_TMR.Q

RETURN

RETURN;

IF

D:=B*B;
IF D<0.0 THEN
C:=A;
ELSIF D=0.0 THEN
C:=B;
ELSE
C:=D;
END_IF;

CASE

CASE INT1 OF
1: BOOL1 := TRUE;
2: BOOL2 := TRUE;
ELSE
 BOOL1 := FALSE;
 BOOL2 := FALSE;
END_CASE;

FOR

J:=101;
FOR I:=1 TO 100 BY 2 DO
IF ARR[I] = 70 THEN
J:=I;
'EXIT;
END_IF;
END_FOR;

WHILE

J:=1;
WHILE J<= 100 AND ARR[J] <> 70 DO
J:=J+2;
END_WHILE;

REPEAT

J:=-1;
REPEAT
J:=J+2;
UNTIL J= 101 OR ARR[J] = 70
END_REPEAT;

EXIT

EXIT;

CONTINUE

CONTINUE;

JMP

label1: i:=i+1;
IF i=10 THEN
    JMP label2;
END_IF
JMP label1;
label2:

empty instruction

;

Assignment Operators

Default Assignment

On the left side of an assignment, there is an operand (variable, address) to which the value of the expression on the right side is assigned by the assignment operator :=.

Also refer to the description of the MOVE operator which has the same function.

For an example with assignment, refer to the Example of a Call with Assignments paragraph of the Calling a Function Block chapter.

Example

a:= b;

a gets the value of b.

Set operator S=

The value will be set: if it is once set to TRUE, it will remain TRUE.

Example

a S= b;

a is set to TRUE if b is TRUE while processing the assignment.

If b is FALSE while processing the assignment, the value of a will not be modified.

Reset operator R=

The value will be reset: if it is once set to FALSE, it will remain FALSE.

Example

a R= b;

a is set to FALSE as soon as b = TRUE.

NOTE: In case of a multiple assignment, set and reset assignments refer to the last member of the assignment.

Example

a S= b R= fun1(par1,par2)

In this case, b will be the reset output value of fun1. But a does not get the set value of b, but gets the set output value of fun1.

Consider that an assignment can be used as an expression. This is an extension to the IEC 61131-3 standard.

Assignment operator REF

This operator generates a reference to a value.

Syntax

<variable name> REF= <variablename> ;

Example

refA : REFERENCE TO DUT;
B : DUT;
C : DUT;
A REF= B; // corresponds to A := ADR(B);
A := C; // corresponds to A^ := C;

Calling Function Blocks in ST

A function block (abbreviated by FB) is called in structured text according to the following syntax:

<name of FB instance>(FB input variable:=<value or address>|, <further FB input variable:=<value or address>|...further FB input variables);

Example

In the following example, a timer function block (TON) is called with assignments for the parameters IN and PT. Then result variable Q is assigned to variable A. The timer function block is instantiated by TMR:TON;. The result variable, as in IL, is addressed according to syntax <FB instance name>.<FB variable>:

TMR(IN := %IX5, PT := 300);
A:=TMR.Q;

There is also another syntax available for outputs:

fb(in1:=myvar, out1=>myvar2);

RETURN Instruction

You can use the RETURN instruction to leave a POU.

Syntax

RETURN;

Example

IF b=TRUE THEN
RETURN;
END_IF;
a:=a+1;

If b is TRUE, instruction a:=a+1; will not be executed. As a result, the POU will be left immediately.

IF Instruction

With the IF instruction you can test for a condition, and, depending upon this condition, execute instructions.

Syntax

IF <boolean_expression1> THEN

<IF_instructions>

{ELSIF <boolean_expression2> THEN

<ELSIF_instructions1>

..

..

ELSIF <boolean_expression n> THEN

<ELSIF_instructions-1>

ELSE

<ELSE_instructions>}

END_IF;

The segment in brackets {} is optional.

If the <boolean_expression1> returns TRUE, then only the <IF_instructions> are executed and, as a result, none of the other instructions. Otherwise, the boolean expressions, beginning with <boolean_expression2>, are evaluated one after the other until 1 of the expressions returns TRUE. Then only those instructions after this boolean expression and before the next ELSE or ELSIF are evaluated. If none of the boolean expressions produce TRUE, then only the <ELSE_instructions> are evaluated.

Example

IF temp<17
THEN heating_on := TRUE;
ELSE heating_on := FALSE;
END_IF;

Here, the heating is turned on when the temperature sinks below 17 degrees. Otherwise, it remains off.

CASE Instruction

With the CASE instruction, you can combine several conditioned instructions with the same condition variable in one construct.

Syntax

CASE <Var1> OF

<value1>: <instruction 1>

<value2>: <instruction 2>

<value3, value4, value5>: <instruction 3>

<value6..value10>: <instruction4>

..

..

<value n>: <instruction n>

ELSE <ELSE instruction>

END_CASE;

A CASE instruction is processed according to the following model:

  • If the variable in <Var1> has the value <Value I>, then the instruction <Instruction I> will be executed.

  • If <Var 1> has none of the indicated values, then the <ELSE Instruction> will be executed.

  • If the same instruction is to be executed for several values of the variables, then you can write these values one after the other separated by commas and thus condition the common execution.

  • If the same instruction is to be executed for a value range of a variable, you can write the initial value and the end value separated by 2 dots. Therefore, you can condition the common condition.

Example

CASE INT1 OF
1, 5: BOOL1 := TRUE;
      BOOL3 := FALSE;
2: BOOL2 := FALSE;
      BOOL3 := TRUE;
10..20: BOOL1 := TRUE;
      BOOL3:= TRUE;
ELSE
    BOOL1 := NOT BOOL1;
    BOOL2 := BOOL1 OR BOOL2;
END_CASE;

FOR Loop

With the FOR loop, you can program repeated processes.

Syntax

INT_Var:INT;

FOR <INT_Var> := <INIT_VALUE> TO <END_VALUE> {BY <step size>} DO

<instructions>

END_FOR;

The segment in brackets {} is optional.

The <instructions> are executed as long as the counter <INT_Var> is not greater than the <END_VALUE>. This is checked before executing the <instructions> so that the <instructions> are not executed if <INIT_VALUE> is greater than <END_VALUE>.

When <instructions> are executed, <INT_Var> is increased by <Step size>. The step size can have any integer value. If it is missing, then it is set to 1. The loop will terminate when <INT_Var> is greater than the <END_VALUE>.

Example

FOR Counter:=1 TO 5 BY 1 DO
Var1:=Var1*2;
END_FOR;
Erg:=Var1;

Assuming that the default setting for Var1 is 1. Then it will have the value 32 after the FOR loop.

NOTE: If <END_VALUE> is equal to the limit value for the data type of <INT_Var> (Counter in the above example), you will produce an infinite, or endless, loop. If Counter is of type SINT, for example, and the <END_VALUE> is 127 (the maximum positive value for a SINT type variable), then the loop can never terminate because adding 1 to this maximum value would result in the variable becoming negative and never exceeding the limits imposed by the FOR instruction.
 WARNING
ENDLESS LOOP
Ensure that the variable type used in FOR instructions is of a sufficient capacity to account for the <END_VALUE> + 1.
Failure to follow these instructions can result in death, serious injury, or equipment damage.

You can use the CONTINUE instruction within a FOR loop. This is an extension to the IEC 61131-3 standard.

WHILE Loop

An alternative to the FOR loop is the WHILE loop, which executes the loop if, and for as long as, a boolean condition is, and remains, TRUE. If the condition is not initially TRUE, the loop is not executed. If the condition which was initially TRUE becomes FALSE, the loop is terminated.

Syntax

WHILE <boolean expression> DO

<instructions>

END_WHILE;

Evidently, the initial and ongoing boolean expression must assume a value of FALSE at some point within the instructions of the loop. Otherwise, the loop will not terminate, resulting in an infinite, or endless, loop condition.

 WARNING
ENDLESS LOOP
Ensure that the WHILE loop will be terminated within the instructions of the loop by creating a FALSE condition of the boolean expression controlling the WHILE loop.
Failure to follow these instructions can result in death, serious injury, or equipment damage.

The following is an example of instructions in the loop causing the loop to terminate:

WHILE Counter>0 DO
 Var1 := Var1*2;
 Counter := Counter-1;
END_WHILE

You can use the CONTINUE instruction within a WHILE loop.

REPEAT Loop

The REPEAT loop is another alternative to the FOR loop, as it is for the WHILE loop. The REPEAT loop differs from the WHILE loop in that the exit condition is evaluated only after the loop has been executed at least once, at the end of the loop.

Syntax

REPEAT

<instructions>

UNTIL <boolean expression>

END_REPEAT;

The <instructions> are carried out repeatedly as long as the <boolean expression> returns TRUE. If <boolean expression> is produced already at the first UNTIL evaluation, then <instructions> are executed only once. The <boolean expression> must assume a value of TRUE at some point within the instructions of the loop. Otherwise, the loop will not terminate, resulting in an infinite, or endless, loop condition.

 WARNING
ENDLESS LOOP
Ensure that the REPEAT loop will be terminated within the instructions of the loop by creating a TRUE condition of the boolean expression.
Failure to follow these instructions can result in death, serious injury, or equipment damage.

The following is an example of instructions in the loop causing the loop to terminate:

REPEAT
 Var1 := Var1*2;
 Counter := Counter-1;
UNTIL
 Counter<=0
END_REPEAT;

You can use the CONTINUE instruction within a REPEAT loop. This is an extension to the IEC 61131-3 standard.

CONTINUE Instruction

As an extension to the IEC 61131-3 standard, the CONTINUE instruction is supported within FOR, WHILE, and REPEAT loops. CONTINUE makes the execution proceed with the next loop cycle.

Example

FOR Counter:=1 TO 5 BY 1 DO
INT1:=INT1/2;
IF INT1=0 THEN
CONTINUE; (* to avoid division by zero *)
END_IF
Var1:=Var1/INT1; (* only executed, if INT1 is not "0" *)
END_FOR;
Erg:=Var1;

EXIT Instruction

The EXIT instruction terminates the FOR, WHILE or REPEAT loop in which it resides without regard to any condition.

JMP Instruction

You can use the JMP instruction for an unconditional jump to a code line marked by a jump label.

Syntax

JMP <label>;

The <label> is an arbitrary, but unique identifier that is placed at the beginning of a program line. The instruction JMP has to be followed by the indication of the jump destination that has to equal a predefined label.

 WARNING
ENDLESS LOOP
Ensure that the use of the JMP instruction is conditional.
Failure to follow these instructions can result in death, serious injury, or equipment damage.

The following is an example of instructions in the create logical conditions that avoid an infinite, or endless, loop between the jump and its destination:

aaa:=0;
_label1: aaa:=aaa+1;
(*instructions*)
IF (aaa < 10) THEN
JMP _label1;
END_IF;

As long as the variable i, being initialized with 0, has a value less than 10, the jump instruction of the example above will affect a repeated flyback to the program line defined by label _label1. Therefore, it will affect a repeated processing of the instructions comprised between the label and the JMP instruction. Since these instructions also include the increment of the variable i, the jump condition will be infringed (at the ninth check) and program flow will proceed.

You can also achieve this functionality by using a WHILE or REPEAT loop in the example. Generally, using jump instructions reduces the readability of the code.

Comments in ST

There are 2 possibilities to write comments in a structured text object:

  • Start the comment with (* and close it with *). This allows you to insert comments which run over several lines. Example: (*This is a comment.*)

  • Single-line comments as an extension to the IEC 61131-3 standard: // denotes the start of a comment that ends with the end of the line. Example: // This is a comment.

You can place the comments everywhere within the declaration or implementation part of the ST editor.

Nested comments: You can place comments within other comments.

Example

(*
a:=inst.out; (* to be checked *)
b:=b+1;
*)

In this example, the comment that begins with the first bracket is not closed by the bracket following checked, but only by the last bracket.