Sie sind auf Seite 1von 69

TR3020 | Training for new users | Overview

TwinCAT 3 New Automation Technology

Training fr Umsteiger

Identifier
Indentifier serves to assign individual names to variables, data types,
functions, etc.
The identifier starts with a letter or underscore
followed by numbers, letters and underscore
No distinction is made between upper and lower case
The following are not permitted
special characters (!, , , $, etc.)
spaces
consecutive underscores
umlauts

TwinCAT Training: Programmer

29.10.2013

Keywords
Keywords are identifiers specified by IEC61131-3. They are thus fixed
components of the syntax and therefore may not be used for other
purposes.

Examples
Standard operators AND, OR, NOT
Standard types BOOL, INT, REAL...
Types TYPE, STRUCT
Block types FUNCTION, FUNCTION_BLOCK, PROGRAM

TwinCAT Training: Programmer

29.10.2013

Keywords and comments


The comments are delimited by character strings with (* or *) at the beginning
and at the end. Comments may be placed wherever spaces are also permitted.
Exception: within string literals.

(*digitale Eingnge*)
bStart AT %IX0.0 :BOOL;(*Anlagenstart*)
(*analoge Eingnge*)
TemK1 AT %IW10 (*Byte 10-11*) :WORD;

Comments to the end of the line


bStart AT %IX0.0 :BOOL; // Anlagenstart

TwinCAT Training: Programmer

29.10.2013

Elementary data types


Type

Lower

Upper

BOOL

FALSE

TRUE

Size

Prefix
x
b

BYTE

8 BIT

by

Bitstring

WORD

16 BIT

Bitstring

DWORD

32 BIT

dw

Bitstring

TwinCAT Training: Programmer

29.10.2013

Elementary data types


Typ

Lower

Upper

Gre

Prfix

SINT

-127

127

8 Bit

si

USINT

255

8 BIT

usi

INT

-32768

32767

16 BIT

UINT

65535

16 BIT

ui

DINT

-134 217 728

134 217 727

32 BIT

di

UDINT

4294 967 295

32 BIT

udi

64 BIT

li

64 BIT

uli

LINT
ULINT

Detail slides
Overflows
Example: EL3102
Example: KL2531
TwinCAT Training: Programmer

29.10.2013

Data types 1: Elementary data types


Type

Lower

Upper

Size

Prefix

TIME_OF_DAY

TOD#0:0:0

TOD#23:59:
59

32 Bit

tod

DATE

D#1970:01:01

D#2106 ???

32 Bit

date

DATE_AND_TIME

DT#1970:01:01:00:00:00

DT#2106
???

32 Bit

dt

TIME

T#0s

T#49d17h2m 32BIT
47s295ms

tim

Detail slides
DT example - reading the system time
DT example - working with standard operators

TwinCAT Training: Programmer

29.10.2013

Elementary data types


Type

Lower

Upper

Size

Prefix

REAL

4 Byte

LREAL

8 Byte

lr

TwinCAT Training: Programmer

29.10.2013

Data types: STRING


Type

Description

Example

Size

Prefix

STRING

String in ASCII
code. Standard
length 80
characters.
Maximum length
255. Strings are
zero-terminated

1234ABCDE

80 +1

ABCDE$R$L
ABCDE$0D$0A

String length specifications


Example
declaration

Assignment

Result SIZEOF

Result LEN

sVar : STRING;

sVar:=ABC;

81

sVar1 :STRING(1);

sVar := X;

sVar: STRING(255);

sVar:=ABC;

256

TwinCAT Training: Programmer

29.10.2013

Data types: STRING


Constants
$<2 Hex values>

ASCII Code

$0D

CR

$R

$r

CR

$L

$l

Line Feed

$N

$n

New Line

$T

$t

Tab

Detail slides
Example: FIND
Example: string functions LEN, REPLACE
String conversion with Union

TwinCAT Training: Programmer

29.10.2013

10

Data types: WSTRING


Type

Description

Example

Description

Pre
fix

WSTRING

String in Unicode
format

Level 0,Block
0x0400-0x4FFF
Cyrillic

ws

Training, seminar

Level 0
Block 0x00000x007F
Basic Latin

TwinCAT Training: Programmer

29.10.2013

11

Data types: Examples of literals


Variable Type

Examples

BOOL

TRUE

2#1

16#1

FALSE

2#0

16#0

WORD,
DWORD

2#1010111111111110

16#AFFE

45054

INT

2#1000000000000001

16#8001

-32768

TIME

t#1h

t#60m

t#3600000ms

t#0.5d

t#12h

t#43200000ms

day

hours

min

sec

ms

ms

REAL

t#30m18s90ms

t#0.505025h

0.3333

3.333e-1

t#1818090ms

TwinCAT Training: Programmer

29.10.2013

12

Data types 1: Variable declaration el. data types


A variable possesses a name behind which a value (number, string,
date, etc.) is concealed. The variable name is a type of description of
the path to the declared data. Variables are characterised above all by
the fact that their contents can be changed at runtime.

Identifier

Data type

Initial value

bStellerUntenLinks:BOOL:=TRUE;

The physical-logical storage


location of this variable is
unknown to the user
(unlocated)

The degrees of freedom and restrictions in the


assignment of the identifiers can be found on
the slide entitled Identifiers and Prefixes
TwinCAT Training: Programmer

29.10.2013

13

Data types 1: Located variables


It is possible when declaring a variable to link the name with an
address that must be explicitly specified. For the allocation of inputs
and outputs of the hardware the incomplete location is to be carried
out with I* and Q*
Identifier

AT

%I

Data type;

%Q*

TwinCAT Training: Programmer

29.10.2013

14

Data types 1: Located variables


Completely located variables.
Identifier

AT

%I

%Q

%M

Byte

Bit

Type

Byte

These variables possess a clear address


(located)
In TwinCAT 3 incompletely located variables can be used for inputs and
outputs
Applications for %M variables can be solved simply with Unions and
direct masking

Detail slides
Detail - Replace %MB by UNION
TwinCAT Training: Programmer

29.10.2013

15

Data types 1: Address division


Examples:
IX10.7

IX10.6

IX10.5

IX10.4

IX10.3

IX10.2

IX10.1

IX10.0

Din0 AT%IX10.0 : BOOL;

IB10

IB1

IB0

Ain AT%IB0 : INT;


equivalent

IW0

Ain AT%IW0 : INT;


IX22.7

IX22.6

IX22.5

IB23

IX22.4

IX22.3

IB22

IX22.2

IX22.1

IB21

IW22

IB20
IW20

ID20

IX22.0

BitVar AT%IX22.1 : BOOL;


Posi AT%IB20 : UDINT;
equivalent

Posi AT%ID20 : UDINT;

TwinCAT Training: Programmer

29.10.2013

16

Data types 1: Variable classes, scope


Local variables are restricted
to the block in which they
were declared.
Keywords
VAR ..
END_VAR
VAR_INPUT ..
END_VAR
VAR_IN_OUT ..

Global variables are


known in each block
within a project.
Keywords
VAR_GLOBAL ..
END_VAR
VAR_CONFIG ..
END_VAR

END_VAR
VAR_OUTPUT ..
END_VAR

TwinCAT Training: Programmer

29.10.2013

17

Data types 1: I/O directly in an FB instance


In an FB the inputs and outputs to the periphery are to be created
directly as local variables

Implementation
TwinCAT Training: Programmer

29.10.2013

18

Data types 1: Access via the located variables


The variable locVar locally declared in Program B can be directly
accessed from Program A via address %MB2.

Project machine
PROGRAM A

PROGRAM B

VAR

VAR
locVar AT%MB2:WORD;
END_VAR

END_VAR

LD %MB2

TwinCAT Training: Programmer

29.10.2013

19

Data types 1: Overlaps in the scope


Project machine
VAR_GLOBAL
Var1:WORD;
END_VAR

PROGRAM A
VAR
Var1 :WORD;
END_VAR

Example
name:
Gvl1

As shown on the left, there is


an overlap in the scope.
In this case the locally
declared variable Var1 is
loaded into the accumulator.
The global variable can also
be accessed with
Namespaces.

LD Var1
LD Gvl1.Var1

TwinCAT Training: Programmer

29.10.2013

20

Data types 1: PERSISTENT attribute


Special properties of variables can be defined using attributes.
Example:
The variable(s) are saved when the PLC is shut down and loaded back on
restarting.

TwinCAT Training: Programmer

29.10.2013

21

Data types 1: Initialisation and CONSTANT


Initial values, the variables are to be preset with a certain value when
starting/resetting the PLC.
VAR
AccelerationTime : TIME := T#3s200ms;
END_VAR
Read-only:
VAR_GLOBAL CONSTANT
pi:REAL:=3.141592654;

Global

END_VAR
VAR CONSTANT
pi:REAL:=3.141592654;

Also locally possible

END_VAR
TwinCAT Training: Programmer

29.10.2013

22

Data Types 2: Derived data types


The user can create his own data types on the basis of the elementary
data types or data types that have already been created. The newlycreated data types are visible in the entire project.
The declaration starts with TYPE and ends with END_TYPE.

Parent type

Name

Data type

New value

Derivation

Name

Data type

Initial value

Area

Inherit
ance

Initial value

Area

TwinCAT Training: Programmer

29.10.2013

23

Data Types 2: References (alias types)


The purpose of the selfdefined data type Reference is
to generate an alternative
name for a variable, constant
or function block.
Create your references as
objects in the object organiser
on the Data Types tab.
They start with the keyword
TYPE and end with
END_TYPE.

Syntax:
TYPE
<Bezeichner>:<Zuweisungsausdruck>;
END_TYPE
Type

Declaration

TwinCAT Training: Programmer

29.10.2013

24

Data Types 2: References (alias types)


Example: A global string variable is transferred to various blocks. If
changes are made to the Global Variables, the declarations must also
be changed in every block

TwinCAT Training: Programmer

29.10.2013

25

Data Types 2: References (alias types)


If a type has been created for the string, further changes are made
only to the type

TwinCAT Training: Programmer

29.10.2013

26

Data Types 2: References (alias types)


If a type has been created for the string, further changes are made
only to the type

TwinCAT Training: Programmer

29.10.2013

27

Data Types 2: References (alias types)


If a type has been created for the string, further changes are made
only to the type

TwinCAT Training: Programmer

29.10.2013

28

Data Types 2: Enumerated type (Enum)


An enumerated type is a self-defined data type consisting of a number
of string constants. These constants are called enumeration values.
The enumeration values are always known in the entire project. It is
best to create your enumerated types as objects in the object
organiser on the Data Types tab. They start with the keyword TYPE
and end with END_TYPE.
Syntax:
TYPE <Bezeichner>:(<Enum_0> ,<Enum_1>, ...,<Enum_n>);
END_TYPE
Example:
TYPE Woche:(Mo, Di, Mi, Dn, Fr, Sa, So:=10);(*Mo = 0 Di = 1..
.. Sa = 6 So = 10*)
END_TYPE
TYPE Richtung:(Up, Dn);(*Up = 0 Dn = 1*)
END_TYPE

The same enumeration value can be used twice via Namespace.


Example: Woche.Dn Richtung.Dn
TwinCAT Training: Programmer

29.10.2013

29

Data Types 2: Enumerated type (Enum)


Example: signal light without Enum:
Declaration

Use

Online

TwinCAT Training: Programmer

29.10.2013

30

Data Types 2: Enumerated type (Enum)


Example: signal light with Enum:
Type

Use

Declaration

Online

TwinCAT Training: Programmer

29.10.2013

31

Data Types 2: Structure declaration


Example: KL5101 Encoder Terminal Structures are self-defined data types.
They are an important aid to better
administration of the process data. In
addition, the structures are suitable
for encapsulated data transfer to
function blocks. Structures can be
used like individual element variables.

TwinCAT Training: Programmer

29.10.2013

32

Data Types 2: Structures instancing

ST_KL5101In_1 AT%I* : KL5101_IN


ST_KL5101Out_1 AT%Q* : KL5101_OUT

TwinCAT Training: Programmer

29.10.2013

33

Data Types 2: Arrays


Arrays represent lists or data fields. All elements in the arrays are of the
same type. Naturally arrays can also consist of own data types (structures).
One, two and three-dimensional arrays are possible.
VAR
Feld_1 :ARRAY[0..9] OF BYTE;
Feld_2 :ARRAY[0..9, 0..1] OF UINT;
Feld_3 :ARRAY[0..9, 0..1,0..1] OF DINT;
END_VAR

1-dimensional
2-dimensional
3-dimensional

There is a possibility to place a data field in a directly addressed memory


location
VAR
Feld_1 AT%MB100:ARRAY[1..10] OF BYTE;
END_VAR
Access to the sub-elements of a data field
Feld_1[2] := 120; (* Expliziter Zugriff*)
Feld_2[i,j] := EXPT(i,j); (*Indizierter Zugriff*)

TwinCAT Training: Programmer

29.10.2013

34

Data Types 2: Limit transgressions


A dangerous state can arise if an area outside the data field is
accessed in the PLC program.
VAR
Feld_1 :ARRAY[1..10] OF BYTE;
Feld_2 :ARRAY[1..10, 2..5] OF UINT;
END_VAR

i:= 9
Feld_1[i+2] := 120;

Feld_1[9];

Feld_2[1,2];

120

TwinCAT Training: Programmer

29.10.2013

35

Data Types 2: CheckBounds (FUN)


The access can be monitored by
the PLC at PLC runtime
This function enables a limit
transgression occurring in the
program to be recognised and
rectified.

FUNCTION CheckBounds :DINT


VAR_INPUT
I,L,U : DINT;
END_VAR

IF I< L THEN
Error case

CheckBounds := L;

ELSIF I > U THEN


Error case
i
Min
Max

Limited
value

CheckBounds := U;

ELSE
OK case

CheckBounds := I;

END_IF

TwinCAT Training: Programmer

29.10.2013

36

Data Types 2: Adding CheckBounds 1 (FUN)


Adding Checkbounds:

TwinCAT Training: Programmer

29.10.2013

37

Data Types 2: Adding CheckBounds 2 (FUN)

TwinCAT Training: Programmer

29.10.2013

38

Data Types 2: Method of operation of CheckBounds (FUN)


Example: user error
source code

Checkbounds is compiled-in
in XAR

10

Do not write,
it is called
automaticall
y
(not visible
in the code)

10
Can be checked with call build:
TwinCAT Training: Programmer

29.10.2013

39

Note about further checker functions


The following further checker functions are possible from
TwinCAT 2.8:
Check for division by 0
CheckDivByte
CheckDivWord
CheckDivDWord
CheckDivReal
Check value ranges
CheckRangeSigned
CheckRangeUnsigned
(see appendix)

TwinCAT Training: Programmer

29.10.2013

40

Data Types 2: Combination: structures and arrays


An array can consist of structures:
Structure:
TYPE DrillPos :
STRUCT
XPos:
FeedrateX:
AccelerationX:
DeccelerationX:
JerkX:
YPos:
FeedrateY:
AcceleartionY:
DeccelerationY:
JerkY:
FeedDrill:
Kuehlen:
END_STRUCT
END_TYPE

LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
LREAL;
BOOL; (*Pumpe ?*)

Declaration of the array:

Positions :ARRAY[0..100] OF DrillPos;


TwinCAT Training: Programmer

29.10.2013

41

Data Types 2: Combination: structures and arrays


Access to Drillpos 55:
Access:
MoveXAx (*FB Instanz*)
(
Execute:=

TRUE,

Position:=

Positions[55].XPos ,

Velocity:=

Positions[55].FeedrateX

Acceleration:=

Positions[55].AccelerationX,

Deceleration:=

Positions[55].DeccelerationX,

Jerk:=

Positions[55].JerkX,

Direction:=

.........,

Axis:=

.............,

);
TwinCAT Training: Programmer

29.10.2013

42

Block types
In IEC61131-3 there are three types of block covered by the generic
term POU (PROGRAM ORGANISATION UNIT):
Program
Function Block
Function

TwinCAT Training: Programmer

29.10.2013

43

Block types: program PRG


Program PRG
Called by a task (one program can call another)
Calls: FBs, functions, (programs)
Local variable: static, i.e. the local data are available again in the next cycle.
Inputs: usually 0, but VAR_INPUT possible
Outputs: usually 0, but VAR_OUTPUT possible
Transfer by reference VAR_IN_OUT likewise possible
Monitoring: Local data are immediately visible in the online mode of the PLC
control
Use: Main programs, Main, Manual, Automatic, etc.

TwinCAT Training: Programmer

29.10.2013

44

Block types: function block FB


Function block FB
Called by programs or other FBs
Calls: FBs, functions,
Local variable: static, i.e. the local data are available again in the next cycle.
Can be instanced in case of multiple calls (multipliable). Each FB call can have
its own local data.
Inputs: 0,1,2,3VAR_INPUT
Outputs 0,1,2,3.. VAR_OUTPUT
Transfer by reference 0,1,2,3.. VAR_IN_OUT
Monitoring: In the online mode of the PLC control the instance of the call
concerned must first be specified. The local data are then visible for each call.
Use: multiple-used modules, each of which requires its own data area. Step
chains...

TwinCAT Training: Programmer

29.10.2013

45

Block types: Function: FC


Function: FC
Called by programs, function blocks and other functions
Calls: Functions
Local variable: temporary, i.e. the local data are available only for the
processing time of the function. Afterwards this data area is used by
other functions.
Inputs:

1,2,3........ VAR_INPUT

Outputs:
precisely 1!, but structure variable possible. The name of
the output is at the same time the name of the function.
Transfer by reference

1,2,3........ VAR_IN_OUT ,

Monitoring: In the online mode of the PLC control only ??? are visible
for the local variables, since this data area is used by all functions in the
cycle and monitoring (debug) takes place only at the cycle limits.
Remedy: program development with breakpoints
Use: algorithms where the result is available after a run. Scaling,
comparison, etc.

TwinCAT Training: Programmer

29.10.2013

46

ST Structured Text: Operators


Operation
put in parentheses
Function call
Exponentiation
Negate
Build. complements
Multiply
Divide
Modulo
Add
Subtract
Compare
Equal to
Not Equal to
BOOL AND
BOOL XOR
BOOL OR

Symbol
Binding strength
(expression)
Strongest binding
Function name (parameter list)
EXPT
NOT
*
Same binding strength,
processing from left to right
/
(10/2*5 = 25 )
MOD
+
Same binding strength
<,>,<=,>=
=
<>
AND
XOR
OR

Weakest binding
TwinCAT Training: Programmer

29.10.2013

47

ST Structured Text: Instructions


Instruction

Example

Assignment:=

PosWert := 10;

Calling a Function Block

Ton1(IN:=Start, PT:=T2s); Output:= Ton1.Q;

RETURN

RETURN;

IF

more precise explanations and examples on the


following pages

CASE
FOR
WHILE
REPEAT
EXIT
Empty instruction

TwinCAT Training: Programmer

29.10.2013

48

ST: IF instruction
Is needed to branch in a program, depending on conditions. With the
IF instructions its not possible to jump back in the PLC cycle.
GOTO is also not available

Keywords:

IF

THEN

ELSIF
ELSE
END_IF

TwinCAT Training: Programmer

29.10.2013

49

ST: IF instruction

IF Condition THEN
Instruction block;
END_IF

No
Condition
Yes

Instruction block

TwinCAT Training: Programmer

29.10.2013

50

ST: IF instruction

IF a>b THEN
Instruction block A;
ELSE
Instruction block B;
END_IF

Condition

No

Yes

Instruction block A

Instruction block B

TwinCAT Training: Programmer

29.10.2013

51

ST: IF instruction
IF Condition1 THEN
Instruction block A;
ELSE
IF Condition2 THEN
Instruction block B;
Condition 1
ELSE
No
Yes
IF Condition3 THEN
Condition 2
Instruction block C;
ELSE
Yes
Instruction block D;
END_IF
END_IF
END_IF
Instruction
Instruction
block A

block B

No
Condition 3
No
Yes
Instruction
block C

Instruction
block D

TwinCAT Training: Programmer

29.10.2013

52

ST: IF instruction

IF Condition1 THEN
Instruction block A;
ELSIF Condition2 THEN
Instruction block B;
ELSIF Condition3 THEN
Instruction block C;
ELSE
Instruction block D;
END_IF

Condition 1
No
Yes
Condition 2
No
Yes
Condition 3
No
Yes
Instruction
block A

Instruction
block B

Instruction
block C

Instruction
block D

TwinCAT Training: Programmer

29.10.2013

53

ST: IF instruction
What can the BOOLEAN EXPRESSION be?
Conditions:
BOOLEAN variable
Comparison
Function calls

Querying of FB instances
NO FB call!

IF bVar THEN
.
IF a>b THEN
.
IF LEFT(STR:= strVar, SIZE:=7) = 'TwinCAT'
THEN
.
IF Ton1.Q THEN
.
IF Ton1(IN:=bVar, PT:=T#1s ) THEN

TwinCAT Training: Programmer

29.10.2013

54

ST CASE Instruction
CASE Selection criterion OF
1:

Selection criterion = 2
or 4 or 6

Instruction 1

2, 4, 6: Instruction 2

Selection
criterion = 1

7..10: Instruction 3

Selection criterion = 7
or 8 or 9 or 10?

No

Yes

..

No

ELSE

Yes

Default
instructions

No
Yes

END_CASE;
Two identical values
may not be available
for selection in the list.

Instruction 1

Instruction 2

Instruction 3

Default
instructions

TwinCAT Training: Programmer

29.10.2013

55

ST: CASE instruction: possibility for a step chain /


state machine
Instructions for the step
(Actions)

CASE State OF
0:

Q0:=TRUE;
IF Transition THEN state := 1; END_IF

1:

Q1:=TRUE;
IF Transition THEN state := 2; END_IF

2:

Step-further condition
(Transition)

Q2:=TRUE;
IF Transition THEN state := 3; END_IF

3:

Q3:=TRUE;
IF Transition THEN state := 0; END_IF

END_CASE

TwinCAT Training: Programmer

29.10.2013

56

ST: CASE instruction Integer Selector Value


with constants
CASE State OF
0:

Instructions;(*State=0*)

Instructions
if state = 0

IF THEN
1:

Instructions;(*State=1*)

Instructions
if state = 1

2:

Instructions;(*State=2*)

Instructions
if state = 2

3:

Instructions;(*State=3*)

Instructions
if state = 3

END_CASE

TwinCAT Training: Programmer

29.10.2013

57

ST: CASE instruction Integer Selector Value


with Enum types

Enum-Typ:
TYPE Schritte :
(INIT:=0, START, AUTOMATIK, ENDE);
END_TYPE

CASE State OF
INIT:

Instructions;(*State=0*)

START:

Instructions;(*State=1*)

AUTOMATIK:

Instructions;(*State=2*)

ENDE:

Instructions;(*State=3*)

END_CASE

TwinCAT Training: Programmer

29.10.2013

58

ST: CASE instruction: suggestion for a step chain /


state machine
TYPE Schritte :
( INIT:=0, START, AUTOMATIK, ENDE);
END_TYPE

Instructions for the step


(Actions)

CASE State OF
INIT:

Q0:=TRUE;
IF Transition THEN state := START; END_IF

START:

Step-further condition
(Transition)

Q1:=TRUE;
IF Transition THEN state := AUTOMATIK; END_IF

AUTOMATIK: Q2:=TRUE; Step


IF Transition THEN state := ENDE; END_IF
ENDE:

Q3:=TRUE;
IF Transition THEN state := INIT; END_IF

END_CASE
TwinCAT Training: Programmer

29.10.2013

59

ST: CASE instruction Integer Selector Value


with constants
CASE State OF

VAR CONSTANT
Step1 : INT:=

0;

Step1:

Instructions;(*State=0*)

Step2 : INT:=

1;

Step2:

Instructions;(*State=1*)

Step3 : INT:=

2;

Step3..Step4:

Instructions;(*State=2 oder 3*)

Step4 : INT:=

3;

END_CASE

END_VAR

VAR
State:INT;
END_VAR

TwinCAT Training: Programmer

29.10.2013

60

ST: Repeat instructions


The process sequence often requires the multiple processing of
precisely the same program sequences, where their number is known
only at runtime.
Disadvantage of loops:
In the case of wrong programming, an infinite number of repetitions
takes place.
If a continuous loop is executed, this does not impair the start of the
time slices (real-time). Tasks with a higher priority will still be executed
on time. Tasks with a lower priority will no longer be executed.
Forced switchover
to Win NT

1
e.g.: 1ms

2
2ms

3
3ms

Begin of a new
time slice

4
4ms

1
5ms

TwinCAT Training: Programmer

29.10.2013

61

ST: Loops (overview)


All loops can be terminated with the help of the EXIT instruction,
regardless of the abort condition.

Expression

Processing

n cycle fix

FOR

SINT/INT/DINT

Yes
Instructions
follow condition

WHILE

BOOL

Instructions
No
follow condition

REPEAT

BOOL

Condition
follows
instructions

No

TwinCAT Training: Programmer

29.10.2013

62

ST: FOR loop


At the beginning of the loop the
Cycle n
control variable i is set to the
starting value (see example).
The control variable is
decremented or incremented in
each loop, depending on the step
size (value after the keyword BY).
If i exceeds the end value
(after TO), the loop is no longer
processed.

Start i:=StartValue

Yes

i
>EndValue
No
Instruction block

i: = i + step size

FOR i:=1 TO 12 BY 2 DO
Feld[i]:=i*2;(*Anweisung*)
END_FOR
Cycle n
TwinCAT Training: Programmer

29.10.2013

63

ST: WHILE loop


The instruction block of a WHILE loop
is executed continuously until the
Boolean expression returns TRUE.
The abort condition can contain
variables that can be changed in the
instruction block.
If the Boolean expression is FALSE at
the beginning, then the instruction
block of the WHILE loop is not
processed.
i:=0;
WHILE i<100 DO
Feld[i]:=i*2;(*Anweisung*)
i:=i+1;
END_WHILE

Cycle n

Yes

Boolean
expression

No

Instruction block
i: = i + step size

Cycle n
TwinCAT Training: Programmer

29.10.2013

64

ST: REPEAT loop


Cycle n

The instruction block of a REPEAT


loop is executed as long as the
Boolean expression is satisfied.
The instruction block is processed at
least once.

Instruction block
i:= i + step size

i:=0;
REPEAT
Feld[i]:=i*2;(*Anweisung*)
i:=i+1;
UNTIL i>100

No

Boolean
expression
Yes

END_REPEAT
Cycle n
TwinCAT Training: Programmer

29.10.2013

65

ST: FB calls in ST
VAR
TON1:TON;
END_VAR
TON1 (IN:= NOT TON1.Q , PT:=T#1s );
Q0:= TON1.Q;

TON1(IN:= NOT TON1.Q, PT:=T#1s , Q=>Q0 );

TwinCAT Training: Programmer

29.10.2013

66

ST: FB calls in ST (alternative)


VAR
TON1:TON;
END_VAR

TON1.IN:= NOT TON1.Q;


TON1. PT:=T#1s;
TON1();
Q0:= TON1.Q;

TwinCAT Training: Programmer

29.10.2013

67

ST: FC calls in ST
Result:=Scale (x:=Eingang, xug:=0.0, xog:=32767.0, yug:=0.0,yog:=100.0);
(* Gleichwertig:*)
Result:=Scale (Eingang, 0.0, 32767.0, 0.0, 100.0);
(* Gleichwertig:*)
Result:=Scale (

xog:=

x:=

Eingang,

xug:=

0.0,

32767.0,
yug:=

0.0,

yog:=

100.0

);
In case of functions, all inputs must be occupied
TwinCAT Training: Programmer

29.10.2013

68

ST: FC calls in ST
Result := Scale (x:=Eingang, xug:=0.0, xog:=32767.0, yug:=0.0,yog:=100.0);

Result

CALL

Input parameters

(* Gleichwertig:*)
Result:=Scale

(
x:=
xug:=
xog:=
yug:=
yog:=
);

Eingang,
0.0,
32767.0,
0.0,
100.0

TwinCAT Training: Programmer

29.10.2013

69

Das könnte Ihnen auch gefallen