IEC 61131-3 - Programming Languages
IEC 61131-3 - Programming Languages
IEC 61131-3 - Programming Languages
Jerzy Kasprzyk
Contents
1. 2. 3. 4. 5. 6. 7. 8. 9. Standard IEC 1131: Programmable Controllers ........................................................................... 2 Common elements ............................................................................................................................. 3 Literals ............................................................................................................................................... 4 Data types .......................................................................................................................................... 6 Variables ............................................................................................................................................ 9 Program organization units (POU) ............................................................................................... 13 Programming languages................................................................................................................. 36 Sequential Function Chart (SFC) .................................................................................................. 49 Configuration elements................................................................................................................... 61
2. Common elements
Delimiters Keywords Declarations: PROGRAM, FUNCTION, VAR_INPUT, END_PROGRAM Elementary data types: BOOL, INT, REAL, TIME Standard Functions and FBs: AND, ADD, MOVE, SHL, TON, CTU etc. and names of input/output parameters IL operators, ST instructions and operators, SFC elements, EN/ENO Literals (e.g. True, 15, 16#ff, t#5s) Identifiers: Configurations, global variables, resources, tasks, access paths, Functions, Function Blocks, Programs, Variables and Data types, Steps, transitions and actions in SFC, Jump and network labels, Constants in enumerated data types Comments (* *) + - $ = := # ; ( ) * space
3. Literals
Table 1. Numeric literals Feature description Integer literals Real literals Real literals with exponents Base 2 literals Base 8 literals Base 16 literals Boolean zero and one Boolean FALSE and TRUE -12 -12.0 0 123_456 0.0 0.456 Examples +986 3.14159_26
-1.34E-12 or -1.34e-12 1.234E6 or 1.234e6 2#1111_1111 (255) 8#377 (255) 2#1110_0000 (240) 8#340 (240)
FALSE TRUE
Table 2. Character string literal feature No. 1 2 3 4 5 6 Example A $ $R$L or $0D$0A $$1.00 Explanation Empty string (length zero) String of length one containing the single character A String of length one containing the space character String of length one containing the single quote character String of length two containing CR and LF characters String of length five which should be printed as $1.00
Table 3. Duration literal features Feature description Duration literals without underlines short prefix Duration literals without underlines long prefix Duration literals with underlines short prefix Duration literals with underlines long prefix Examples T#14ms T#14.7s T#14.7m T#14.7h t#14.7d t#5d14h12m18s3.5ms TIME#14ms time#14.7s T#14ms T#14.7s T#14.7m T#14.7h t#25h_15m t#5d_14h_12m_18s_3.5ms TIME#25h_15m time#5d_14h_12m_18s_3.5ms
Table 4. Date and time of day literals No. 1 2 3 Feature description Date literals Time of day literals Date and time literals DATE# Prefix keyword D# TOD# DT#
TIME_OF_DAY# DATE_AND_TIME#
Table 5. Examples of date and time of day literals Long prefix notation DATE#1984-06-25 date#1984-06-25 TIME_OF_DAY#15:36:55.36 time_of_day#15:36:55.36 DATE_AND_TIME#1984-06-25-15:36:55.36 date_and_time#1984-06-25-15:36:55.36 Short prefix notation D#1984-06-25 d#1984-06-25 TOD#15:36:55.36 tod#15:36:55.36 DT#1984-06-25-15:36:55.36 dt#1984-06-25-15:36:55.36
4. Data types
Table 6. Elementary data types No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 BOOL SINT INT DINT LINT USINT UINT UDINT ULINT REAL LREAL TIME DATE TIME_OF_DAY, TOD DATE_AND_TIME, DT STRING BYTE WORD DWORD LWORD Keyword Boolean Short integer Integer Double integer Long integer Unsigned short integer Unsigned integer Unsigned double integer Unsigned long integer Real numbers Long real numbers Duration Date (only) Time of day (only) Date and time of day Variable-length character string Bit string of length 8 Bit string of length 16 Bit string of length 32 Bit string of length 64 8 16 32 64 Data type Bits 1 8 16 32 64 8 16 32 64 32 64
Table 7. Generic data types ANY ANY_BIT ANY_NUM ANY_INT BOOL BYTE WORD DWORD LWORD INT SINT DINT LINT UINT USINT UDINT ULINT ANY_REAL REAL LREAL DATE TIME_OF_DAY DATE_AND_TIME TIME STRING and derived data types ANY_DATE
Table 8. Data type declaration feature No. 1 Feature/textual example Direct derivation from elementary types, e.g.: TYPE FLOATING : REAL; END_TYPE 2 Enumerated data type, e.g.: TYPE ANALOG_SIGNAL_TYPE : (SINGLE_ENDED, DIFFERENTIAL); END_TYPE 3 Subrange data type, e.g.: TYPE ANALOG_DATA : INT ( -4095..4095); END_TYPE 4 Array data type, e.g.: TYPE ANALOG_16_INPUT: ARRAY [1..16] OF ANALOG_DATA; ANALOG_ARRAY: ARRAY[1..4,1..16] OF ANALOG_DATA; END_TYPE 5 Structured data type, e.g.: TYPE ANALOG_CHANNEL_CONFIG : STRUCT RANGE : ANALOG_RANGE; MIN_SCALE : ANALOG_DATA; MAX_SCALE : ANALOG_DATA; END_STRUCT; ANALOG_16_INPUT_CONFIG : STRUCT SIGNAL_TYPE : ANALOG_SIGNAL_TYPE; FILTER_PARAMETER : SINT (0..99); CHANNEL : ARRAY [1..16] OF ANALOG_CHANNEL_CONFIG; END_STRUCT; END_TYPE
Table 9. Default initial values Data type(s) BOOL, SINT, INT, DINT, LINT USINT, UINT, UDINT, ULINT BYTE, WORD, DWORD, LWORD REAL, LREAL TIME DATE TIME_OF_DAY DATE_AND_TIME STRING Initial value 0 0 0 0.0 T#0S D#0001-01-01 TOD#00:00:00 DT#0001-01-01-00:00:00 (the empty string)
Table 10. Data type initial value declaration features No. 1 2 Feature/textual example TYPE NEW_BOOL : BOOL := 1; END_TYPE TYPE ANALOG_RANGE : (BIPOLAR_10V, UNIPOLAR_1_10V, UNIPOLAR_0_10V, UNIPOLAR_1_5V, UNIPOLAR_0_5V, UNIPOLAR_4_20MA, UNIPOLAR_0_20MA, ) := UNIPOLAR_1_5V ; END_TYPE (* -10 to +10 VDC *) (* +1 to+10 VDC *) (* 0 to+10 VDC *) (* +1 to+10 VDC *) (* 0 to+10 VDC *) (* +4 to+20 mADC *) (* 0 to+20 mADC *)
3 4
TYPE ANALOG_DATAZ : INT (-4095..4095) := 0 ; END_TYPE TYPE ANALOG_16_INPUT: ARRAY [1..16] OF ANALOG_DATA := 8(-4095), 8(4095); END_TYPE
5. Variables
Table 11. Variable declaration keywords Keyword VAR VAR_INPUT VAR_OUTPUT VAR_IN_OUT VAR_EXTERNAL VAR_GLOBAL VAR_ACCESS Internal to POU variables Externally supplied, not modifiable within POU Supplied by POU to external entities Supplied by external entities, can be modified within POU Supplied by configuration via VAR_GLOBAL, can be modified within POU Global variable declaration Access path declaration Description
Table 12. Attributes (qualifiers) of variables Attribute AT RETAIN CONSTANT R_EDGE F_EDGE READ_ONLY READ_WRITE Meaning Location assignment Retentive variable (battery back-up) Constant variable (cannot be modified) Rising edge Falling edge Write-protected Can be read and written to
FST SCN %Q5 MOVE (R) WORD 10IN Q%R100 LEN 00001 Example 2. Initial values declaration in GE Fanuc Table 13. Use of attributes (qualifiers) for variable types Variable type VAR VAR_INPUT VAR_OUTPUT VAR_IN_OUT VAR_EXTERNAL VAR_GLOBAL VAR_ACCESS RETAIN + + + CONSTANT + + R_EDGE F_EDGE + READ_ONLY READ_WRITE +
VAR CONSTANT (* constant *) PI : REAL := 3.1415925; One1 : INT := 1; One2 : DINT :=1; END_VAR VAR_OUTPUT RETAIN (* output retentive variable *) Q : WORD; END_VAR VAR_INPUT (* input variables *) IN1 : BOOL R_EDGE; (* input variable rising edge *) IN2 : BOOL F_EDGE; (* input variable falling edge *) END_VAR VAR_ACCESS (* access paths *) CSX : P1.Z : REAL READ_ONLY; END_VAR Example 3. Attributes declaration for variables
10
Single-element variables
%QX75 or %Q75 %IW215 %QB7 %MD48 %IW2.5.7.1 Output bit 75 Input word location 215 Output byte location 7 Double word at memory location 48 hierarchical addressing Example 4. Directly represented variables Table 14. Location and size prefix features for directly represented variables No. 1 2 3 4 5 6 7 8 9 VAR (* Directly represented variables *) AT %IW1 : INT; (* Input word location 1, variable of type INT *) AT %QD8 : DINT; (* Output double word location 8, variable of type DINT *) (* located variables with symbolic representation *) XXX AT %QW3 : INT; (* Output word location 3, variable of type INT *) YYY AT %QX16 : BOOL; (* Output bit location 16 *) (* unlocated variables with symbolic representation *) A,B,C : INT; (* 3 variables of type INT in 3 consecutive words in memory *) DDD : DINT (* a variables of type DINT in 2 consecutive words in memory *) RRR : REAL; (*a variables of type REAL in 2 consecutive words in memory *) END_VAR ... LD %IW1 (* using of directly represented variable *) ST XXX LD %IW1 (* load a variable of type INT *) ST DDD (* store of CR in a variable of type DINT error!!!*) Example 5. Declaration and using of single variables Prefix I Q M X none B W D L Input location Output location Memory location Single bit size Single bit size Byte (8 bits) size Word (16 bits) size Double word (32 bits) size Long word (64 bits) size Meaning
11
Multi-element variables
VAR MY_INPUTS AT %IW1 : ARRAY[0..3] OF INT; INPUT_TAB : ANALOG_ARRAY; MODULE_CONFIG : ANALOG_16_INPUT_CONFIG; END_VAR
Example 6. Multi-element variable declaration INPUT_TAB[4,%MB6] MODULE_CONFIG.SIGNAL_TYPE:=SINGLE_ENDED; MODULE_CONFIG.CHANNEL[5].RANGE:=BIPOLAR_10V; Example 7. Element addressing for multi-element variables
Example 8. Graphical and textual declaration of input and output variables (external interface)
12
Negated output
Table 16. Use of EN input and ENO output No. 1 Feature Use of EN and ENO required for LD Example FUN FUN EN FUN OK EN ENO( ) A C B FUN FUN EN EN ENO FUN OK A C B FUN A C B
13
Function declaration
FUNCTION MY_FUN : REAL VAR INPUT A, B : REAL; C : REAL:=1.0 ; END_VAR MY_FUN:=A*B/C ; END_FUNCTION Example 9. Function declaration in ST (* Function name and type *) (* Input parameters *) (* Initial value *) (* Output assignment *)
FUNCTION (* Function name and external interface *) MY FUN REAL A REAL REAL B REAL C (* Function body *) A * B / MOJA FUN C END FUNCTION Example 10. Function declaration in FBD.
14
Table 18. Type conversion functions No. 1 2 3 4 Graphical form * * TO ** ** ANY REAL TRUNC ANY INT ANY BIT BCD TO ** ANY INT ANY INT * TO BCD ANY BIT Usage example in ST A:=REAL_TO_INT(B); A:=TRUNC(B); A:=BCD_TO_INT(C); C:=INT_TO_BCD(A);
(* Variable declaration for the 1st example *) VAR A :INT; B :REAL; C :WORD; END_VAR (* Example 1 in IL language *) LD B REAL_TO_INT ST A
15
Table 19. Examples of explicit type conversion with overloaded functions No. 1 Type declaration (ST) VAR A : INT ; B : INT ; C : INT ; END_VAR VAR A : INT ; B : REAL ; C : REAL ; END_VAR VAR A : INT ; B : INT ; C : REAL ; END_VAR Examples in FBD and ST ADD A C B C:= A + B; ADD A INT TO REAL C B C:=INT_TO_REAL(A) + B; ADD A INT TO REAL C B C:=INT_TO_REAL(A + B);
Table 20. Examples of explicit type conversion with typed functions No. 1 Type declaration (ST) VAR A : INT ; B : INT ; C : INT ; END_VAR VAR A : INT ; B : REAL ; C : REAL ; END_VAR VAR A : INT ; B : INT ; C : REAL ; END_VAR Examples in FBD and ST ADD INT A C B C:=ADD_INT( A, B); ADD REAL A INT TO REAL C B C:=ADD_REAL(INT_TO_REAL(A), B); ADD INT A INT TO REAL C B C:=INT_TO_REAL(ADD_INT(A, B));
16
Standard functions
Table 21. Standard functions of one numeric variable Graphical form * ** *
* I/O type ** Function name
Description
1 2
ABS SQRT
3 4 5
LN LOG EXP
6 7 8 9 10 11
Sine of input in radians Cosine of input in radians Tangent of input in radians Principal arc sine Principal arc cosine Principal arc tangent
17
Table 22. Standard arithmetic functions Graphical form *** ANY NUM
*** Name or symbol
No.
Name
Symbol
12 13
ADD MUL
+ *
14 15 16 17 18
OUT := IN1 IN2 OUT := IN1 / IN2 OUT := IN1 modulo IN2
** :=
18
Table 23. Standard bit shift functions Graphical form *** ANY BIT IN ANY BIT ANY INT N
*** Function Name
No. 1 2 3 4
OUT:= IN left-shifted by N bits, zero-filled on right OUT:= IN right-shifted by N bits, zero-filled on left OUT:= IN right-rotated by N bits, circular OUT:= IN left-rotated by N bits, circular
Table 24. Standard bitwise Boolean functions Graphical form *** ANY BIT Usage example A:=AND(B,C,D); or A:=B&C&D; LD B AND C AND D ST A (* ST *)
(* IL *)
No. 5 6 7 8
Description OUT:=IN1 & IN2 & & INn OUT:=IN1 OR IN2 OR OR INn OUT:=IN1 XOR IN2 XOR XOR INn OUT:=NOT IN1
19
Table 25. Standard selection functions No. 1 Graphical form SEL BOOL G ANY ANY IN0 ANY IN1 MAX ANY MIN ANY Explanation / example in ST Binary selection: OUT:=IN0 if G=0 OUT:=IN1 if G=1
Example: A:=SEL(G:=0, IN0:=X, IN1:=255); Result: A:=X. Extensible maximum function: OUT:=MAX{IN1, IN2, , INn} Example: A:=MAX(B, C, D); Extensible minimum function: OUT:=MIN{IN1, IN2, , INn} Example: A:=MIN(B, C, D); Limiter: OUT:=MIN{MAX{IN, MN}, MX} Example: A:=LIMIT(IN:=B, MN:=0, MX:=255); Result: A:=0 if B < 0, A:=255 if B > 255, else A:=B.
MUX K ANY
Extensible multiplexer: select one of N inputs depending on input K. Example: A:=MUX(K:=0, IN0:=B, IN1:=C, IN2:=D); Result: A:=B.
Example of MUX in LD: LD 0 MUX B, C, D ST A (Run-time error if K < 0 or > the number of the remaining inputs)
20
Table 26. Standard comparison functions Graphical form *** ANY BOOL : ANY
*** Function Name or symbol
Description =1 if decreasing sequence: OUT:=(IN1 > IN2) & (IN2 > IN3) & & (INn-1 > INn) =1 if monotonic sequence: OUT:=(IN1 >= IN2) & (IN2 >= IN3) & & (INn-1 >= INn) =1 if equality: OUT:=(IN1 = IN2) & (IN2 = IN3) & & (INn-1 = INn) =1 if monotonic sequence: OUT:=(IN1 <= IN2) & (IN2 <= IN3) & & (INn-1 <= INn) =1 if increasing sequence: OUT:=(IN1 < IN2) & (IN2 < IN3) & & (INn-1 < INn) =1 if inequality (non-extensible): OUT:=(IN1 <> IN2) & (IN2 <> IN3) & & (INn-1 <> INn)
>=
<=
<
<>
Example in IL (A is of type BOOL, B,C,D are of type ANY, but the same): LD B GT C AND( LD C GT D ) ST A (*CR := Boolean result of comparison (B > C) *) (* CR is stored *) (* CR := Boolean result of comparison (C > D) *) (* End of nesting CR := CR AND CR stored *)
21
Table 27. Standard character string functions No. 1 Graphical form STRING LEN INT LEFT STRING IN STRING ANY INT L RIGHT STRING IN STRING ANY INT L MID IN STRING L P CONCAT STRING INSERT IN1 STRING IN2 P DELETE IN1 STRING L P REPLACE IN1 STRING IN2 L P FIND IN1 ANY INT IN2 Explanation / example in ST String length function, e.g.: A:=LEN(ASTRING); Result: A:=7. Leftmost L characters of IN, e.g.: A:=LEFT(IN:=ASTR, L:=3) Result: A:=AST. Rightmost L characters of IN, e.g.: A:=RIGHT(IN:=ASTR, L:=3); Result: A:=STR. L characters of IN beginning at the P-th, e.g.: A:=MID(IN:=ASTR, L:=2, P:=2); Result: A:=ST. Extensible concatenation, e.g.: A:=CONCAT(AB,CD,E); Result: A:=ABCDE. Insert IN2 into IN1 after the P-th position, e.g.: A:=INSERT(IN1:=ABC, IN2:=XY,P:=2); Result: A:=ABXYC. Delete L characters of IN beginning at the P-th character position, e.g.: A:=DELETE(IN:=ABXYC, L:=2, P:=3); Result: A:=ABC. Replace L characters of IN1 byIN2 starting at the P-th character position, e.g.: A:=INSERT(IN1:=ABCD,IN2:=X,L:=2,P:=2); Result: A:=AXD. Find the character position of the beginning of the first occurrence of IN2 in IN1, e.g.: A:=FIND(IN1:=ABCBC, IN2:=BC); Result A:=2.
4 STRING ANY INT ANY INT 5 STRING : STRING 6 STRING STRING ANY INT 7 STRING ANY INT ANY INT
22
Table 28. Standard functions of time data type Numeric and concatenation functions No. 1 2 3 4 5 6 7 8 9 10 11 12 Name ADD Symbol + IN1 TIME TIME_OF_DAY DATE_AND_TIME TIME DATE TIME_OF_DAY TIME_OF_DAY DATE_AND_TIME DATE_AND_TIME TIME TIME DATE IN2 TIME TIME TIME TIME DATE TIME TIME_OF_DAY TIME DATE_AND_TIME ANY_NUM ANY_NUM TIME_OF_DAY OUT TIME TIME_OF_DAY DATE_AND_TIME TIME TIME TIME_OF_DAY TIME DATE_AND_TIME TIME TIME TIME DATE_AND_TIME
SUB
* /
23
Data type or FB type *) BOOL; (* variable declaration instance of data type BOOL *) CTU; (* instance of FB type CTU*) CTU; (* the other instance of FB type CTU *) Example 11. Variable declaration and FB instantiation Table 29. Instantiation and invocation of FB
Textual form (ST) (* Instantiation *) VAR FF75: SR; END_VAR (* Invocation *) FF75(S1:=%IX1, R:=%IX2); (* Output assignment *) %QX3:= FF75.Q1;
%I0001 %M0010 > UPCTR ( ) %M0010 R CONST PV +00100 %R0100 Example 12. Usage of FB UPCTR in LD (GE Fanuc)
24
SP
1s
Q
1
t
T2 T1 T2 T1 T2
Figure 1. Timing diagrams for on-off controller with time hysteresis FUNCTION_BLOCK On_Off (* External interface *) VAR_INPUT I1 : BOOL; Y : INT; SP : INT; T1 : TIME := t#100ms; T2 : TIME := t#100ms; END_VAR VAR_OUTPUT Q : BOOL; END_VAR VAR Timer_ON : TON; Timer_OFF : TON; FlipFlop : SR; END_VAR (* FB body *) IF I1 THEN IF (Y < SP) THEN Timer_ON(IN:=I1, PT:=T1); END_IF
(* Input enables FB, default = 0 *) (* Process Variable *) (* Set Point *) (* On delay, default = 100ms *) (* Off delay, default = 100ms *)
IF (Y > SP) THEN Timer_OFF(IN:=I1, PT:=T2); (* set time delay for OFF *) END_IF FlipFlop(S:=Timer_ON.Q, R1:=Timer_OFF.Q); Q:=FlipFlop.Q1; (* output assignment *) END_IF END_FUNCTION_BLOCK Example 13. FB declaration in ST
25
I1 Y SP
I1 Y SP
Timer ON FlipFlop LT INT EN ENO TON RS IN QS Q1 Q T1 PT ET R1 Timer OFF GT INT EN ENO TON IN Q T2 PT ET
26
in LD: ( S ) and ( R ) or ( SM ) and ( RM ) coils (* FBD *) FlipFlop (* instance name *) RS START S Q1 %Q1 STOP R1 (* textual languages *) VAR FlipFlop : RS; END_VAR (* ST *) FlipFlop(S:=START, R1:=STOP); %Q1:=FlipFlop.Q1; (* IL *) CAL FlipFlop(S:=START, R1:=STOP); LD FlipFlop.Q1 ST %Q1 (* invocation *) (* output assignment *) (* invocation *) (* output assignment *) (* instantiation *)
27
Table 31. Standard edge detection FBs No. 1 Graphical form R TRIG BOOL CLK Q BOOL F TRIG BOOL CLK Q BOOL Description Rising edge detector CLK tested input
in LD: | P | and | N | contacts or ( P ) and ( N ) coils (* FBD *) Detector (* instance name *) R TRIG %I1 CLK Q %M1 (* textual languages *) VAR Detector : R_TRIG; END_VAR (* ST *) Detector(CLK:=%I1); %M1:= Detector.Q; (* IL *) CAL Detector (CLK:=%I1); (* invocation *) LD Detector.Q ST %M1 (* output assignment *) Example 16. Rising edge detection in FBD, ST and IL. (* invocation *) (* output assignment *) (* instantiation *)
28
Table 32. Standard counter FBs No. 1 Graphical form CTU BOOL >CU Q BOOL BOOL R INT PV CV INT CTD BOOL >CD Q BOOL BOOL LD INT PV CV INT CTUD >CU QU BOOL >CD QD BOOL R LD PV CV INT Description Up counter CU input (rising edges are counted up) R reset counter (CV:=0) PV preset value Q output (:=1 if CV >= PV) CV current value Down counter CD input (rising edges are counted down) LD load counter with the PV PV preset value Q output (:=1 if CV <= 0) CV current value Up-down counter CU input (rising edges are counted up) CD input (rising edges are counted down) R reset counter (CV:=0) LD load counter with the PV PV preset value QU output (:=1 if CV >= PV) QD output (:=1 if CV <= 0) CV current value
(* textual languages *) VAR MyCounter : CTUD; (* Instance name of FB *) END_VAR (* ST *) MyCounter(CU:=%I1, CD:=%I2, R:=%I3, LD:=%I4); (* Invocation *) %Q1:= MyCounter.QU; (* Output assignment to %Q1 *) %Q2:= MyCounter.QD; (* Output assignment to %Q2 *) (* IL *) CAL MyCounter(CU:=%I1, CD:=%I2, R:=%I3, LD:=%I4); (* Invocation *) LD MyCounter.QU ST %Q1 (* Output assignment to %Q1 *) LD MyCounter.QD ST %Q2 (* Output assignment to %Q2 *) Example 17. Usage of CTUD in FBD, ST and IL
29
Table 33. Standard timer FBs No. 1 2 3 Graphical form *** BOOL IN Q BOOL TIME PT ET TIME *** denotes: TP, TON, TOF Description TP (Pulse generator) TON (Timer ON delay) TOF (Timer OFF delay) IN Input turning on the timer PT Preset Time ET Expired Time
Real Time Clock RTC PDT Preset Date and Time (loaded at rising BOOL EN Q BOOL edge of EN) DT PDT CDT DT CDT Current Date and Time (while EN = 1) Q copy of EN
(* FBD *) Timer (* Instance name *) RTC %I1 EN Q %M1 dt#2002-01-05-10:00:00.00 PDT CDT DateTime (* textual languages *) VAR Timer : RTC; END_VAR (* ST *) Timer(IN:=%I1, PDT:= dt#2002-01-05-10:00:00.00); (* Invocation *) %M1:= Timer.Q; (* Output assignment to %M1 *) DateTime:= Timer.CDT; (* Output assignment to DateTime *) (* IL *) CAL Timer(IN:=%I1, PDT:= dt#2002-01-05-10:00:00.00) (* Invocation *) LD Timer.Q ST %M1 (* Output assignment to %M1 *) LD Timer.CDT ST DateTime (* Output assignment to DateTime *) Example 18. Usage of RTC in FBD, ST and IL (* Instance name *)
30
IN
1 0 t0 1 t1 t2 t3 t4 t5 t
Q 0 t0 +PT t1 PT ET 0 t0 t1 t2 t3 t4 t5 t4+PT t5
1 IN 0 t0 1 Q 0 t0 PT ET 0 t1 t2 t3 t4 t5 t t1+PT t2 t5+PT t t1 t2 t3 t4 t5 t
31
(* FBD *) Timer TON %I3 IN Q %Q1 t#10s PT ET (* Textual languages *) VAR Timer : TON; END_VAR (* ST *) Timer(IN:=%I3, PT:=t#10s); %Q1:= Timer.Q; (* IL *) CAL Timer(IN:=%I3, PT:=t#10s) LD Timer.Q ST %Q1 (* Invocation *) (* Output assignment to %Q1 *) (* Invocation *) (* Output assignment to %Q1 *) (* Instance name of FB *) (* instance name *)
(* ST *) (* Timer.PT:= t#10s;
setting delay
(* IL *) *) LD t#10s ST Zegar.PT
..... (* timer invocation *) Timer(IN:=%I3); LD %I3 ST Timer.IN CAL Timer ..... (* output assignment to %Q1 *) %Q1:= Zegar.Q; LD Timer.Q ST %Q1
32
33
(* Declaration of function FF1 *) FUNCTION FF1 : BOOL; VAR_INPUT X : INT; END_VAR VAR Y : REAL; END_VAR ... Y:=FF2(0.0); ...
(* Declaration of function FF2 *) FUNCTION FF2 : REAL; VAR_INPUT X : REAL; END_VAR ... IF FF1(5) THEN ...... (* Forbidden call of FF1 causing the recurrence *) ... END_IF ... END_FUNCTION Example 23. Invalid recursion by mutual invocation in ST
Textual (ST) use of ADD function no formal parameter names Graphical (FBD) use of SHL function formal parameter names
34
FUNCTION_BLOCK Fblock VAR_INPUT Par1 : BOOL; Par2: TIME; Par3 : INT; END_VAR (* FB body *) END_FUNCTION_BLOCK
(* Declaration of FB *)
Example 24. FB declaration with three input parameters VAR FB1, FB2, FB3 : Fblock; (* Three instances of Fblock *) AT %I1 : BOOL; AT %IW1 : INT; END_VAR (* Complete FB call *) CAL FB1(Par1 := %I1, Par2 := t#10s, Par3:= %IW1) (* Complete FB call with parameters in changed order *) CAL FB2(Par3 := %IW1, Par1 := %I1, Par2:= t#10s) (* Incomplete FB call with parameters in changed order *) CAL FB1(Par2 := t#10s, Par1:= %I1) Example 25. FB call with parameters omitted and in a different order, written in IL VAR C1, C2 : CTU; END_VAR (* instructions *) C2(CU := C1.Q, PV := 100, R:= %I1); (* Invocation of C2 *) %QW10 := INT_TO_BCD(C2.CV); (* Instance name of C2 as actual parameter*) Example 26. Using the FB instance name as actual parameter
(* 2 instances of CTU *)
35
7. Programming languages
Common elements
TYPE...END_TYPE; VAR...END_VAR; VAR_INPUT...END_VAR; VAR_OUTPUT...END_VAR; VAR_IN_OUT...END_VAR; VAR_EXTERNAL...END_VAR; FUNCTION...END_FUNCTION; FUNCTION_BLOCK...END_FUNCTION_BLOCK; PROGRAM...END_PROGRAM; STEP...END_STEP; TRANSITION...END_TRANSITION; ACTION...END_ACTION.
VAR Operand1, Operand2, Result : INT :=0; END_VAR Et1: LD Operand1 (* CR Operand1, here value 0 *) ADD 10 (* CR CR + 10, here CR = 10 *) ST Result (* Result CR, value of CR remains *) GT 0 (* CR > 0? yes, so CR := TRUE *) JMPC Et2 (* jump to label Et2, if CR = TRUE, value of CR remains *) ADD Operand2 (* Add Operand2 to CR Error !!! data type mismatch *) Et2: Example 27. The universal accumulator of IL (CR Current Result)
36
Example 28. Computation of nested expressions in parentheses FUNCTION MyFun : INT; (* Function declaration *) VAR_INPUT IN1, IN2, IN3 : INT; (* Input parameters *) END_VAR (* Function body *) LD IN1 ADD IN2 ADD IN3 ST MyFun (* Function output *) END_FUNCTION (* calling MyFun in POU *) VAR Par1, Par2, Par3, Result : INT; END_VAR LD Par1 MyFun Par2, Par3 ST Result
(* Variable declaration *)
(* First input parameter *) (* Function call with remaining parameters *) (* Store the result of MyFun in location Result *) Example 29. Function call in IL
37
Table 36. IL operators No. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Operator LD ST S R AND OR XOR ADD SUB MUL DIV GT GE EQ NE LE LT JMP CAL RET ) N, (, N( N, (, N( N,(, N( ( ( ( ( ( ( ( ( ( ( C, CN C, CN C, CN Modifiers N N Operand * * BOOL BOOL BOOL BOOL BOOL * * * * * * * * * * LABEL NAME Semantics Set CR equal to operand (LoaD) Store CR to operand location (STore) If CR = 1 then set Boolean operand to 1 (Set) If CR = 1 then reset Boolean operand (Reset) Boolean AND Boolean OR Boolean (eXclusive OR) ADDition SUBtraction MULtiplication DIVision Comparison: CR > operand (Greater Than) Comparison: CR >= operand (Greater than or Equal) Comparison: CR = operand (Equal) Comparison: CR <> operand (Not Equal) Comparison: CR <= operand (Less than or Equal) Comparison: CR < operand (Less Than) JuMP to label CALl function block NAME RETurn from called function or FB Evaluate deferred operation
* these operators are either overloaded (type ANY) or typed, the CR and the operand shall be of the same type
38
Table 37. Function block invocation feature for IL assuming it was declared VAR C10 : CTU; END_VAR No. 1 CAL with input list: CAL C10(CU:=%I10, R:= %I11, PV:=15) 2 CAL with load/store of inputs: LD 15 ST C10.PV LD %I10 ST C10.CU LD %I11 ST C10.R CAL C10 3 Use of input operators (see Table 38. ) LD 15 PV C10 LD %I1 CU C10 LD %I11 R C10 CAL C10 Description/Example
Output assignment in the example presented above: LD C10.Q ST %QX1 (*Output of the counter fires the output %QX1 *) LD C10.CV ST CurrentValue (* Count value stored at variable CurrentValue that has to be declared as INT type *)
39
Table 38. Standard function block input operators for IL language No. 1 2 3 4 5 6 7 8 9 10 Operators S1, R S, R1 CLK CLK CU, R, PV CD, LD, PV CU, CD, R, LD, PV IN, PT IN, PT IN, PT Function blocks SR RS R_TRIG F_TRIG CTU CTD CTUD TP TON TOF
40
12 13 14 15 16
41
Table 40. ST language statements No. 1 2 3 4 Statement Assignment FB invocation FB output usage RETURN IF A:=B; Examples CV:=CV+1; Y:=SIN(X); D:=INT_TO_REAL(C)
My_TMR(IN:=%IX5, PT:=T#300ms); A:=My_TMR.Q; RETURN; D:=B*B-4*A*C; IF D < 0.0 THEN NROOTS:=0; (* no roots *) ELSIF D = 0.0 THEN (* one root *) NROOTS := 1; X1 := B / (2.0 * A); ELSE (* two roots *) NROOTS := 2; X1 := (B + SQRT(D) ) / (2.0 * A); X2 := (B SQRT(D) ) / (2.0 * A); END_IF; ERROR:=0; XW:=BCD_TO_INT(Y); CASE XW OF 1,4: DISPLAY := TEKST1; 2: DISPLAY := TEKST2; Y := SIN(Z); 3,5..10: DISPLAY := STATUS (XW 3); ELSE DISPLAY := ; (* XW outside 1..10 *) ERROR := 1; END_CASE; J := 101; FOR I := 1 TO 100 BY 2 DO IF WORDS(I) = KEY THEN J := I; EXIT; END_IF; END_FOR; J := 1; WHILE J <= 100 AND WORDS(J) <> KEY DO J := J+2; END_WHILE; J := 1; REPEAT J := J+2; UNTIL J = 101 OR WORDS(J) = KEY END_REPEAT; EXIT;
CASE
FOR
WHILE
REPEAT
EXIT
42
SUM:=0 FOR I:=1 TO 3 DO (* the first loop *) FOR J:=1 TO 2 DO (* the nested loop *) IF FLAG THEN EXIT; END_IF (* exit from the nested loop *) SUM:=SUM+J; END_FOR; (* end of the nested loop *) SUM:=SUM+I; END_FOR; (* end of the first loop *) Example 30. EXIT statement example
Jump target
43
Table 42. Power rails and link elements No. 1 Symbol >From_TO> > From_TO> Description Left power rail (with attached horizontal link)
2 3 4
Right power rail (with attached horizontal link) Horizontal link Vertical link (with attached horizontal links)
Table 43. Contacts Contact Symbol *** Static contacts Description Normally open contact The state of the left link is copied to the right link if the state of the associated Boolean variable (indicated by ***) is ON. Otherwise, the state of the right link is OFF. Normally closed contact The state of the left link is copied to the right link if the state of the associated Boolean variable (indicated by ***) is OFF. Otherwise, the state of the right link is OFF. Positive transition-sensing contact The state of the right link is ON from one evaluation of this element to the next when a transition of the associated Boolean variable from OFF to ON is sensed at the same time that the state of the left link is ON. The state of the of the right link shall be OFF at all other times. Negative transition-sensing contact The state of the right link is ON from one evaluation of this element to the next when a transition of the associated Boolean variable from ON to OFF is sensed at the same time that the state of the left link is ON. The state of the of the right link shall be OFF at all other times.
*** /
44
Table 44. Coils Coils Symbol *** ( ) Momentary coils *** (/) Description Coil The state of the left link is copied to the associated Boolean variable and to the right link. Negated coil The state of the left link is copied to the right link. The inverse of the state of the left link is copied to the associated Boolean variable. Set coil, Latch coil The associated Boolean variable is set to the ON state when the left link is in the ON state, and remains set until reset by a (R) coil. Reset coil, Unlatch coil The associated Boolean variable is reset to the OFF state when the left link is in the ON state, and remains reset until set by a (S) coil. Retentive coil, Memory coil Set retentive coil
Latched coils
*** (S)
*** (R)
Retentive coils
Positive transition-sensing coil The state of the associated Boolean variable is ON from one evaluation of this element to the next when a transition of the left link from OFF to ON is sensed. The state of the left link is always copied to the right link. Negative transition-sensing coil The state of the associated Boolean variable is ON from one evaluation of this element to the next when a transition of the left link from ON to OFF is sensed. The state of the left link is always copied to the right link.
45
%IX1 SHL EN ENO %MW10IN %MW10 2N Example 31. Call of function SHL MyTON (* instance name *) TON %IX1 IN Q T#10sPT ET Example 32. Invocation of function block TON (* latch *) %IX1 %IX2 %QX1 /( ) %QX1 Example 33. Feedback in LD language (* computing *) (* saving *) %I1 %Q1 ( ) %I1 %Q3 %Q2 /( ) %I2 %I1 %Q2 %Q3 /( ) %I2 Example 34. Structure of LD network %I1 %Q1 %Q3 %Q2 ( )/( ) %I2 %Q2 %Q3 /( ) Example 35. A permitted but unusual network with the same functionality as in Example 34.
46
Table 46. Graphical elements of execution control in FBD No. 1 Symbol 1>>LABELA X>>LABELB AND A >>NEXT B NEXT: OR %I1 %Q1 %Q1 X<RETURN> END_FUNCTION END_FUNTION_BLOCK Conditional return from function or FB Unconditional return from function Unconditional return from FB Description Unconditional jump to LABELA Conditional jump (if X = 1) to LABELB Example of conditional jump: Jump condition (both A and B shall be equal to 1)
47
wired-OR in LD A C ( ) B
Function OR in FBD OR A C B
Example 36. Boolean OR examples (* example 1 - explicit connection *) AND A RUN OR START1 START2 (* example 2 - implicit connection *) AND A RUN OR START1 START2 RUN (* example 3 implicit connection *) AND A RUN C OR START1 C START2 Example 37. FBD network with feedback explicit and implicit connections
48
Table 48. Transition and transition conditions in graphical form No. 1 Example Step2 %I1 & %I2 Step3 Step2 %I1 %I2 Step3 Step2 & %I1 %I2 Step3 Description Predecessor step Transition condition in ST Successor step Predecessor step Transition condition in LD Successor step
49
No. 4
Example Step2 >TRAN2 3> Step3 %I1 %I2 >TRAN2 3> & %I1 >TRAN2 3> %I2 Step2 TRAN23 Step3 TRANSITION TRAN23: %I1 %I2 TRAN23 END_TRANSITION TRANSITION TRAN23: & %I1 >TRAN23> %I2 END_TRANSITION TRANSITION TRAN23: LD %I1 AND %I2 END_TRANSITION TRANSITION TRAN23:= %I1 & %I2; END_TRANSITION
4a
Transition condition in LD
4b
Use of transition name: Predecessor step Transition name Successor step Transition condition in LD
5a
5b
5c
Transition condition in IL
5d
Transition condition in ST
Table 36 continued
50
STEP STEP2: (* Declaration of STEP2 *) END_STEP (* Declaration of transition from STEP2 to STEP3 *) TRANSITION FROM STEP2 TO STEP3:= (* Transition condition in ST *) %I1 & %I2; (* Transition condition in IL *) LD %I1 AND %I2 END_TRANSITION (* End of transition declaration *) STEP STEP3: (*Declaration of STEP3 *) END_STEP Example 38. Transition and transition condition in textual form
TRANSITION FROM (S1, S2, S3) TO (S4,S5):= (* Boolean expression for transition conditions *) END_TRANSITION Example 39. Transition in textual form for greater number of steps (predecessors or successors)
51
Sequence evolution
Table 49. Sequence evolution No. 1 Example S2 a S3 S3 * b c S4 S5 S3 * 2 1 b c S4 S5 S3 b NOT b & c S4 S5 S6 S7 d e S8 Rule Single sequence: The alternation step transition is repeated in series. Example: An evolution from S2 to S3 shall take place if and only if step S2 is active and the transition condition a is true. Divergence of sequence selection: A selection between several sequences is represented by as many transition symbols, under the horizontal line, as there are different possible evolutions. The asterisk denotes left-to-right priority. Example: An evolution shall take place from S3 to S4 only if S3 jest active and the transition condition b is TRUE, or from S3 to S5 only if S3 is active and c is TRUE and b is FALSE. Divergence of sequence selection: The asterisk follow by numbered branches, indicates a user-defined priority of transition evaluation, with the lowest-numbered branch having the highest priority. Example: An evolution shall take place from S3 to S5 only if S3 jest active and the transition condition c is TRUE, or from S3 to S4 only if S3 is active and b is TRUE and c is FALSE. Divergence of sequence selection: The connection of the branch indicates that the user must assure that transition conditions are mutually exclusive. Example: An evolution shall take place from S3 to S4 only if S3 jest active and the transition condition b is TRUE, or from S3 to S5 only if S3 is active and b is FALSE and c is TRUE. Convergence of sequence selection: The end of sequence selection is represented by as many transition symbols, above the horizontal line, as there are selection paths to be ended. Example: An evolution shall take place from S6 to S8 only if S6 jest active and the transition condition d is TRUE, or from S7 to S8 only if S7 is active and e is TRUE.
2a
2b
2c
52
No. 4
Rule Simultaneous sequence - divergence: Only one common transition symbol shall be possible, immediately above the double horizontal line of synchronization. Example: An evolution shall take place from S9 to S10, S11, only if S9 jest active and the transition condition f associated to the common transition is TRUE. After the simultaneous activation of S10, S11,etc., the evolution of each sequence proceeds independently. Simultaneous sequence - convergence: Only one common transition symbol shall be possible, immediately under the double horizontal line of synchronization. Example: An evolution shall take place from S12, S13, to S14 to only if all steps above and connected to the double horizontal line are active and the transition condition g associated to the common transition is TRUE. Sequence skip: A sequence skip is a special case of sequence selection (feature 2) in which one or more of the branches contain no steps. Feature 6a, 6b and 6c correspond to the representation options given in features 2a, 2b and 2c, respectively. Example (feature 2a shown): An evolution shall take place from S15 to S18 if a is FALSE and b is TRUER, that is the sequence (S16, S17) will be skipped.
S12 S13 g S14 S15 * a b S16 c S17 d S18 S19 a < S20 b S21 * c d > S22
6a 6b 6c
7a 7b 7c
Sequence loop: A sequence loop is a special case of sequence selection (feature 2) in which one or more of the branches returns to a preceding step. Feature 7a, 7b and 7c correspond to the representation options given in features 2a, 2b and 2c, respectively. Example (feature 7a shown): An evolution shall take place from S21 to S20 if c is FALSE and d is TRUER, that is the sequence (S20, S21) will be repeated.
53
a)
S10 x S11
S12 S13 S14 x S15 S16 S12 S13 S14 x S15 S16 S12 S13 S14 x S15 S16 Example 40. Rules of evolution
b)
S10 x S11
c)
S10 x S11
54
Actions
Mechanisms for the declaration of actions: Any Boolean variable declared in a VAR or VAR_OUTPUT block, or their graphical equivalents, can be an action. see Table 50.
55
Feature
Graphical declaration in LD
Textual declaration in ST
Textual declaration in IL
56
Table 51. Step/action association No. 1 Example S8 L ACTION 1 DN1 t#10s DN1 S8 L ACTION 1 DN1 t#10s DN1 N ACTION 2 P ACTION 3
STEP S8: ACTION_1(L, t#10s, DN1); ACTION_2(N); ACTION_3(P); END_STEP
Description
Action block
S8 N ACTION 4 %Q2 = %I1 & %M3 & S8.X; FF1(S1 := (C < D)); %M10 := FF1.Q;
qualifier action name flag Action definition (optional) Figure 5. Graphical form of action block definition (* Use of action block in LD *) S8.X %I1 OK1 N ACTION 1 DN1( ) (* Use of action block in FBD *) S8.X & N ACTION 1 DN1 OK1 %I1 Example 43. Use of action block in graphical languages
57
Table 52. Action qualifiers No. 1 Qualifier None Description Non-stored (null qualifier). An action is executed only if associated step is active. Non-stored description as above. Overriding Reset Set (stored) time Limited time Delayed Pulse Stored and time Delayed (combination of S and D) time Delayed and Stored (combination of D and S) Stored and time Limited (combination of S and L)
2 3 4 5 6 7 8 9 10
N R S L D P SD DS SL
58
Examples of SFC
MAIN TANK
V1 H1 TANK 1 B1 B2 H2
V2
TANK 2
W1
W2
Figure 6. Two tanks filling. The initial state both tanks are empty. If push button m is pressed, both tanks are filled by opening valves. When a tank is full, filling stops and its contents start to be used. Filling may only start up again when both tanks are empty and if the button m is pressed. > S1 Rw1 Rw2 m S2 Sv1 S5 Sv2 h1 h2 S3 Rv1 S6 Rv2 Sw1 Sw2 NOT(b1 OR b2) < Example 45. SFC to control of two tanks filling
59
Example 46. Two SFC networks synchronized by the Boolean action Lock using the qualifiers S and R
60
9. Configuration elements
Overall software structure according to IEC 61131-3:
CONFIGURATION
RESOURCE
Configuration elements
TASK
PROGRAM
FUNCTION_BLOCK
FUNCTION_BLOCK
FUNCTION_BLOCK
FUNCTION
FUNCTION
Configuration elements assign properties to POUs: PROGRAMs and FBs are assigned run-time properties on a particular hardware; Program variables are assigned to PLC hardware; Communication relationships are defined between configurations. Configuration elements match the elements in real-world PLC systems: Configuration a PLC system, e.g. a controller in a rack with multiple CPUs, controlling a cell of machines; Resource one CPU in a PLC, possibly capable of multitasking; Task run-time properties for programs and FBs (type of a PLC program); Run-time program unit consisting of a PROGRAM or FUNCTION_BLOCK and the TASK associated with it.
61
TASK TaskName
INTERVAL ... PRIORITY ...
. . . . . .
. . . . . .
Configuration:
- Definition of global variables (valid within this configuration) - Combination of all resources of a PLC system - Definition of access paths between configurations - Declaration of directly represented variables
Resource:
- Definition of global variables (valid within this resource) - Assignment of tasks and programs to a resource - Invocation of run-time programs with input and output parameters - Declaration of directly represented variables
Task:
62
Table 53. Configuration and resource declaration features No. 1 2 3 4 5a 5b 6a 6b 6c 7 8a 8b 9a 9b Description CONFIGURATION...END_CONFIGURATION construction VAR_GLOBAL...END_VAR construction within CONFIGURATION RESOURCE...ON...END_RESOURCE construction VAR_GLOBAL...END_VAR construction within RESOURCE Periodic TASK construction within RESOURCE Non-periodic TASK construction within RESOURCE PROGRAM declaration with PROGRAM to TASK association PROGRAM declaration with FB to TASK association PROGRAM declaration with no TASK association Declaration of directly represented variables in VAR_GLOBAL Connection of directly represented variables to PROGRAM inputs Connection of GLOBAL variables to PROGRAM inputs Connection of PROGRAM outputs to directly represented variables Connection of PROGRAM outputs to GLOBAL variables
10a VAR_ACCESS...END_VAR construction 10b Access paths to directly represented variables 10c Access paths to PROGRAM inputs 10d Access paths to GLOBAL variables in RESOURCES 10e Access paths to GLOBAL variables in CONFIGURATIONS 10f Access paths to PROGRAM outputs
63
CONFIGURATION Configuration_name TYPE ... END_TYPE VAR_GLOBAL ... END_VAR RESOURCE Resource_name_1 ON Hardware_name_1 RESOURCE Resource_name_2 ON Hardware_name_2 ... VAR_ACCESS ... END_VAR END_CONFIGURATION
(* Resources declaration *)
TASK Task_name_1 (* Tasks declaration ) TASK Task_name 2 ... (* Programs instantiation and program to task association *) PROGRAM Program_instance_name_1 WITH Task_name_ : Program_name PROGRAM ... WITH ... ... END_RESOURCE Example 48. Consecutive elements in RESOURCE declaration
64
Tasks
Table 54. Graphical representation of TASK within RESOURCE Graphical form Task name TASK BOOL SINGLE TIME INTERVAL UINT PRIORITY Slow Task TASK SINGLE t#50msINTERVAL 3 PRIORITY Fast Task TASK SINGLE t#10msINTERVAL 2 PRIORITY Description
(INTERVAL:= t#50ms, PRIORITY:=3); (INTERVAL:= t#5ms, PRIORITY:=2); (SINGLE:= %IX1, PRIORITY:=1); Example 49. Textual representation for examples in Table 55.
Start of RESOURCE within CONFIGURATION TASK is enabled; Stopping RESOURCE within CONFIGURATION TASK is disabled.
65
Table 55. Graphical representation of tasks - POUs association Graphical form RESOURCE STATION Prog 1 Prog A Task 1 END RESOURCE 1 Prog 4 Prog B Task 2 (* Resource name *) (* Instances of programs *) (* Programs *) Description
(* Associated tasks *)
RESOURCE STATION 2 (* Resource name Prog 1 (* Program instance Prog C (* Program FB 1 FB 2 (* FB instances FB A FB B Task 1 Task 2 ( * Associated tasks END RESOURCE
*) *)
*)
PROGRAM Prog_1 WITH Task_1 : Prog_A; PROGRAM Prog_4 WITH Task_2 : Prog_B; PROGRAM Prog_1 : Prog_C(FB_1 WITH Task_1, FB_2 WITH Task_2); Example 50. Textual representation of tasks - POUs association
66
Communication model
Data flow connection within a PROGRAM:
PROGRAM A FB1 FB_X a b FB2 FB_Y
CONFIGURATION C PROGRAM A send1 C_D FB1 FB_X a SEND ID connection SD1 C_D
67
Y C CSX
: :
VAR_ACCESS Variables accessible from other resources STATION_1.%IX1.1 : STATION_1.PROG1.X2 : Example 52. Access paths declaration
Data type and optionally read/write qualifier *) BOOL READ_ONLY; UINT READ_WRITE;
68
Example of configuration
CONFIGURATION CELL_1
RESOURCE STATION_1 TASK SLOW_1 P1 PROGRAM F x1 x2 y1 TASK FAST_1 P2 PROGRAM G out1 FB1 FB2 B A y1 b1 y2 b2
SLOW_1 FAST_1
RESOURCE STATION_2 TASK PER_2 P1 PROGRAM F x1 x2 y1 TASK INT_2 P2 PROGRAM H FB1 FB2 C D c1 d1 y2
PER_2
HOUT1
SLOW_1 %IX1.1 z1 w z2
PER_2
INT_2 %QW5
XX
ALA
OLA
AS
ALPHA
BETA
GAMMA
ACCESS PATHS
69
PROGRAM F VAR_INPUT x1:BOOL; x2:UINT; END_VAR VAR_OUTPUT y1:BYTE; END_VAR END_PROGRAM PROGRAM G VAR_OUTPUT out1:UINT; END_VAR VAR_EXTERNAL z1:BYTE; END_VAR VAR FB1:A; FB2:B; END_VAR FB1(); out1 := FB1.y1; z1 := FB1.y2; FB2(b1 := FB1.y1, b2 := FB1.y2); END_PROGRAM
(* Inputs *)
(* Output *) (* Declaration of global variable usage *) (* FB instances *) (* Other declarations and program body *) (* FB1 invocation *) (* y1 from FB1 to program output *) (* y2 from FB1 assigned to a global variable *) (* FB2 invocation *)
PROGRAM H VAR_OUTPUT HOUT1:INT; END_VAR VAR FB1:C; FB2:D; END_VAR FB1(); FB2(d1 := FB1.c1); HOUT1 := FB2.y2; END_PROGRAM
(* Output *) (* FB instances *) (* Other declarations and program body *) (* FB1 invocation *) (* FB2 invocation *) (* y2 from FB2 to program output *)
Example 53. Outline of programs declaration for example of CONFIGURATION (see Figure 7)
70
FUNCTION_BLOCK A VAR_OUTPUT y1:UINT; y2:BYTE; END_VAR END_FUNCTION_BLOCK FUNCTION_BLOCK B VAR_INPUT b1:UINT; b2:BYTE; END_VAR END_FUNCTION_BLOCK FUNCTION_BLOCK C VAR_OUTPUT c1:BOOL; END_VAR END_FUNCTION_BLOCK FUNCTION_BLOCK D VAR_INPUT d1:BOOL;END_VAR VAR_OUTPUT y2:INT;END_VAR END_FUNCTION_BLOCK
(* Output *)
(* Inputs *)
71
CONFIGURATION CELL_1 VAR_GLOBAL w : UINT; END_VAR RESOURCE STATION_1 ON PROCESSOR_1 VAR_GLOBAL z1 : Byte; END_VAR TASK SLOW_1(INTERVAL := t#20ms, PRIORITY := 2); TASK FAST_1(INTERVAL := t#10ms, PRIORITY := 1); PROGRAM P1 WITH SLOW_1 : F(x1 := %IX.1); PROGRAM P2 : G( OUT1 => w, FB1 WITH SLOW_1, FB2 WITH FAST_1); END_RESOURCE RESOURCE STATION_2 ON PROCESSOR_2 VAR_GLOBAL z2 : BOOL; AT %QW5 : INT; END_VAR TASK PER_2(INTERVAL := t#50ms, PRIORITY := 2); TASK INT_2(SINGLE := z2, PRIORITY := 1); PROGRAM P1 WITH PER_2 : F(x1 := z2, x2 := w); PROGRAM P2 WITH INT_2 : H(HOUT1 => %QW5, FB1 WITH PER_2); END_RESOURCE VAR_ACCESS XX : STATION_1.P1.x2 : UINT READ_WRITE; ALA : STATION_1.%IX1.1 : BOOL READ_ONLY; OLA : STATION_1.z1 : BYTE; AS : w : UINT READ_ONLY; ALPHA : STATION_2.z2 : BOOL READ_WRITE; BETA : STATION_2.P1.y1 : BYTE READ_ONLY; GAMMA : STATION_2.P2.HOUT1 : INT READ_ONLY; END_VAR END_CONFIGURATION
(* feature 1 in Table 53. *) (* feature 2 in Table 53. *) (* feature 3 in Table 53. *) (* feature 4 in Table 53. *) (* feature 5a in Table 53. *) (* feature 5a in Table 53. *) (* feature 6a in Table 53. *) (* feature 8a in Table 53. *) (* feature 6c in Table 53. *) (* feature 9b in Table 53. *) (* feature 6b in Table 53. *) (* feature 6b in Table 53. *) (* feature 3 in Table 53. *) (* feature 2 in Table 53. *) (* feature 4 in Table 53. *) (* feature 7 in Table 53. *) (* feature 5a in Table 53. *) (* feature 5b in Table 53. *) (* feature 6a in Table 53. *) (* feature 8b in Table 53. *) (* feature 6a in Table 53. *) (* feature 9a in Table 53. *) (* feature 6b in Table 53. *) (* feature 3 in Table 53. *) (* feature 10a in Table 53. *) (* feature 10c in Table 53. *) (* feature 10b in Table 53. *) (* feature 10d in Table 53. *) (* feature 10e in Table 53. *) (* feature 10d in Table 53. *) (* feature 10f in Table 53. *) (* feature 10f in Table 53. *) (* feature 10a in Table 53. *) (* feature 1 in Table 53. *)
Execution times: STATION_1: P1 2 ms, P2 8 ms (without execution times of FB1 and FB2 because FB1 and FB2 are associated with tasks SLOW_1 and FAST_1, while P2 is not associated with any task), FB1 and FB2 2 ms each; STATION_2: P1 30 ms, P2 5 ms (without execution time of FB1 because FB1 is associated with task PER_2, while P2 with task INT_2), FB1 in P2 10 ms.
72
Table 56. Task features examples of non-preemptive scheduling Example for STATION_1 t (ms) 0 2 4 6 10 14 16 20 24 26 28 30 32 40 Running P2.FB2@1 P1@2 P2.FB1@2 P2 P2 P2.FB2@1 P2 P2 P2.FB2@1 P1@2 P2.FB1@2 P2.FB2@1 P2 P2.FB2@1 P1@2, P2.FB1@2, P2 Waiting P1@2, P2.FB1@2, P2 P2.FB1@2, P2 P2 P2.FB2@1 P2 P2 yet not finished FB2 restarts with delay P2 restarts Comment
P2.FB2@1, P1@2, P2.FB1@2 P2 yet not finished P1@2, P2.FB1@2, P2 P2.FB1@2, P2 P2 P2 P2 restarts
Example for STATION_2 Task INT_2 is triggered at t = 25, 50, 90, ... ms t (ms) 0 25 30 35 45 50 55 85 90 95 Running P1@2 P1@2 P2@1 P2.FB1@2 P2@1 P1@2 P2.FB1@2 P2.FB1@2 P2@1 P2@1 z2:=1, FB1 yet not finished P2 starts Waiting P2.FB1@2 P2.FB1@2, P2@1 P2.FB1@2 P1@2, P2.FB1@2 P2.FB1@2 P2.FB1 finished z2:=1, P2 starts P1 starts with delay z2:=1, P1 yet not finished P2 starts Comment
73
100
P1@2
P2.FB1@2
Table 57. Task features examples of preemptive scheduling Example for STATION_1 t (ms) 0 2 4 6 10 12 16 20 Running P2.FB2@1 P1@2 P2.FB1@2 P2 P2.FB2@1 P2 P2 P2.FB2@1 Waiting P1@2, P2.FB1@2, P2 P2.FB1@2, P2 P2 P2 P1@2, P2.FB1@2, P2 P2 starts P2 interrupted P2 continued P2 restarts P2 interrupted Comment
Example for STATION_2 Task INT_2 is triggered at t = 25, 50, 90, ... ms t (ms) 0 25 30 35 45 50 55 85 90 95 100 Running P1@2 P2@1 P1@2 P2.FB1@2 P2@1 P1@2 P2.FB1@2 P2@1 P2.FB1@2 P1@2 P2.FB1@2 P2.FB1@2 P2.FB1 interrupted P2.FB1 continued Waiting P2.FB1@2 P1@2, P2.FB1@2 P2.FB1@2 P1@2, P2.FB1@2 P2.FB1@2 P2.FB1 finished z2:=1, P2 starts P1 interrupted P1 continued Comment
74