• 沒有找到結果。

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

在文檔中 Instructions Reference Manual (頁 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

ST Programming Example That Uses Expressions or Functions for

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;

Version Information

Sysmac Studio version 1.08 or higher is required to use expressions for EndVal and StepVal.

You can use an expression for InitVal even with Sysmac Studio version 1.07 or lower.

Additional Information

• Execute the BREAK instruction (or the EXIT instruction in ST) to cancel repeat processing. The processing between the BREAK instruction and the NEXT instruction will not be executed.

• FOR-NEXT loops (or FOR-END_FOR loops in ST) can be nested. In the following figure, the proc-esses are performed in the following order.

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

Precautions for Correct Use

• In a ladder diagram, connect the FOR and NEXT instructions directly to the left bus bar.

• Always use the FOR and NEXT instructions (FOR and END_FOR statements in ST) as a pair. A programming error will occur if there is not the same number of both instructions.

• Program the paired FOR and NEXT instructions in the same section.

• Set the condition to end repetition carefully so that you do not create an infinite loop. If an infinite

loop occurs, task execution will time out.

If the values that are given in the following table are used for the input parameters to the variables, the value of Index will never be greater than the value of EndVal because the maximum value of SINT data is 127. Therefore, an infinite loop is created.

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

Ladder diagram

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

2

FOR and NEXT

• The FOR-NEXT loops can be nested up to 15 levels, but count all nesting levels for the following instructions: IF, CASE, FOR, WHILE, and REPEAT.

• If loops are nested, you will need one BREAK instruction (or one EXIT instruction in ST) for each nesting level to cancel all repeat processing.

• Do not use Jump Instructions (e.g., the JMP instruction) to interrupt repeat processing. Always use the BREAK instruction (or the EXIT instruction in ST) to cancel repeat processing.

• 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

Ladder diagram ST

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.

• In a ladder diagram, use the same data type for InitVal, EndVal, StepVal, and Index. Otherwise, a building error will occur.

• Set the data type of Index to include the valid ranges of InitVal, EndVal, and StepVal. Otherwise, a building error will occur.

• 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

of times.

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.

• Caution is required when you specify upward or downward differentiation for a LD, AND, or OR in-struction in a FOR loop in a ladder diagram and an array is used for the LD, AND, or OR inin-struction.

For upward or downward differentiation, the value of the specified variable at the previous execution is compared with the value of the specified variable at the current execution to determine upward or downward differentiation. Normally, the value of the specified variable does not change every time the instruction is executed. However, if an array is specified in a FOR loop, the array element changes each time the instruction is executed. Therefore, upward or downward differentiation is de-termined by comparing different array elements.

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

在文檔中 Instructions Reference Manual (頁 153-158)