# ST Programming Example That Uses Expressions or Functions for In- In-put Variables

In document Instructions Reference Manual (Page 153-158)

FOR position:=INT#0 TO INT#9 BY INT#1 DO AryOut[position]:=INT#0;

END_FOR;

LD ST

INT#0 AryOut[position]

INT#0 INT#9 INT#1

position FOR

EN ENO InitVal Index EndVal StepVal

NEXT EN ENO

MOVE EN ENO In Out

INT#0 → InitVal INT#9 → EndVal INT#1 → StepVal

INT#0 INT#0 is assigned in order to AryOut[0] to AryOut[9].

InitVal → Index

* For ST: InitVal ≤ EndVal Index + StepVal → Index

INT#0 → AryOut[Index]

Index ≤ EndVal TRUE

TRUE

FALSE

END

FALSE

INT#0 INT#0 INT#0 INT#0 INT#0 INT#0 INT#0 INT#0 INT#0 INT#0 AryOut[0]

AryOut[1]

AryOut[2]

AryOut[3]

AryOut[4]

AryOut[5]

AryOut[6]

AryOut[7]

AryOut[8]

AryOut[9]

StepVal ≥ 0 and Index < EndVal *

START

### The following shows an example with EndVal for the function and StepVal for the expression.

A:= DINT#1;

B:= DINT#2;

C:= REAL#9.6;

FOR i := 0 TO RoundUp(C) BY A+B DO DINTArray[i]:= i;

END_FOR;

### Process A → Process B → Process B → Process C → Process A → Process B → Process B → Process C → Process A → Process B → Process B → Process C

Process A

Process B

Process C

NEXT NEXT FOR FOR

InitVal = INT#0 EndVal = INT#2 StepVal = INT#1

InitVal = INT#0 EndVal = INT#1 StepVal = INT#1

### Do not set the maximum value for the data type in EndVal.

Variable Value of input parameter InitVal SINT#0

EndVal SINT#127 StepVal SINT#1

Index

### ---• The following table describes operation according to the values of StepVal, InitVal, and EndVal.

Programming

language Value of StepVal Values of InitVal and

EndVal Operation

StepVal > 0

InitVal ≤ EndVal Operation is normal.

InitVal > EndVal

The processing between the FOR and NEXT instructions is not executed even once.

An error does not occur.

StepVal < 0

InitVal < EndVal

The processing between the FOR and NEXT instructions is executed an indeter-minate number of times.

Do not use settings like these. An error does not occur.

InitVal ≥ EndVal

The processing between the FOR and NEXT instructions is not executed even once.

An error does not occur.

StepVal = 0

InitVal < EndVal An infinite loop occurs and task execution times out.

InitVal ≥ EndVal

The processing between the FOR and NEXT instructions is not executed even once.

An error does not occur.

ST

StepVal > 0

InitVal ≤ EndVal Operation is normal.

InitVal > EndVal

The processing between the FOR and END_FOR instructions is not executed even once.

An error does not occur.

StepVal < 0 InitVal < EndVal

The processing between the FOR and END_FOR instructions is not executed even once.

An error does not occur.

InitVal ≥ EndVal Operation is normal.

StepVal = 0

InitVal ≤ EndVal An infinite loop occurs and task execution times out.

InitVal > EndVal

The processing between the FOR and END_FOR instructions is not executed even once.

An error does not occur.

Sequence Control Instructions

FOR and NEXT

### • The operation to change the values of InitVal, EndVal, and StepVal during repeat processing is dif-ferent in a ladder diagram and ST.

Variable Operation

InitVal The new value is not applied until repeat proc-essing is completed.

The new value is not applied until repeat proc-essing is completed.

EndVal The new value is applied even during repeat processing.

StepVal The intended operation may not occur. Do not change the value of this variable during repeat processing.

### • The value of Index after repeat processing is different in a ladder diagram and ST. In a ladder dia-gram, the value of StepVal is not added to Index at the end of repeat processing. In ST, the value of

StepVal is added to Index at the end of repeat processing. Processing is repeated the same number

### The following example is for when InitVal is 1, EndVal is 100 and StepVal is 1.

Ladder diagram : The value of Index will be 100 after 100 repetitions.

ST : The value of Index will be 101 after 100 repetitions.

### The following table shows the relationship between the values of x[i-1] and x[i], and the increment processing for Count1[i].

Value of x[i-1] Value of x[i] Increment processing for Count1[i]

TRUE TRUE Not executed.

TRUE FALSE Not executed.

FALSE TRUE Executed.

FALSE FALSE Not executed.

DINT#0 DINT#10 DINT#1

i FOR EN ENO InitVal Index EndVal StepVal

NEXT EN ENO x[i]

Count1[i]

Upward differentiation is detected by comparing x[i] to x[i−1].

Inc EN ENO InOut

### • In the following programming, upward differentiation of x[i] is detected by the R_TRIG instruction. An instance of the R_TRIG instruction is provided for each element of x[i], so it is possible to detect the elements of x[i] for which there was upward differentiation. The following table shows the relation-ship between the value of x[i] for the previous execution of R_TRIG_instance[i], the value of x[i] for the current execution of R_TRIG_instance[i], and the increment processing of Count2[i].

Value of x[i] for previous exe-cution of R_TRIG_instance[i]

Value of x[i] for current

execu-tion of R_TRIG_instance[i] Increment processing for Count2[i]

TRUE TRUE Not executed.

TRUE FALSE Not executed.

FALSE TRUE Executed.

FALSE FALSE Not executed.

DINT#0 DINT#10 DINT#1

i FOR EN ENO InitVal Index EndVal StepVal

NEXT EN ENO

Count2[i]

x[i]

Inc EN ENO InOut R_TRIG R_TRIG_instance[i]

R_TRIG_instance[i].Q

Clk Q

The values of x[i] at the previous execution and the current execution of R_TRIG_instance[i] are compared to determine upward differentiation.

Sequence Control Instructions

### 2

FOR and NEXT

In document Instructions Reference Manual (Page 153-158)