Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
160
Добавлен:
11.10.2020
Размер:
2.81 Mб
Скачать

10 - APPENDIX

10 APPENDIX

Appendix A: IEC Operators and additional norm extending functions

CoDeSys supports all IEC operators. In contrast with the standard functions functions (see appendix D, Standard library), these operators are recognized implicitly throughout the project. Besides the IEC operators CoDeSys also supports the following operators which are not prescribed by the standard: INDEXOF and SIZEOF (see Arithmetic Operators), ADR and BITADR (see Address Operators).

Operators are used like functions in POU.

Attention: At operations with floating point variables the result depends on the currently used target system !

Arithmetic operators

Bitstring Operators

Bit-Shift Operators

Selection Operators

Comparison Operators

Address Operators

Calling Operators

Type Conversions

Numeric Operators

10.1Arithmetic Operators...

ADD

Addition of variables of the types: BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, REAL and LREAL.

Two TIME variables can also be added together resulting in another time (e.g., t#45s + t#50s = t#1m35s)

Example in IL:

LD 7

ADD 2,4,7

ST Var1

Example in ST:

var1 := 7+2+4+7;

Example in FBD:

CoDeSys V2.3

10-1

Appendix A: - IEC Operators and additional norm extending functions

MUL

Multiplication of variables of the types: BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, REAL and LREAL.

Example in IL:

LD 7

MUL 2,4,7

ST Var1

Example in ST:

var1 := 7*2*4*7;

Example in FBD:

SUB

Subtraction of one variable from another of the types: BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, REAL and LREAL.

A TIME variable may also be subtracted from another TIME variable resulting in third TIME type variable. Note that negative TIME values are undefined.

Example in IL:

LD 7

SUB 2

ST Var1

Example in ST:

var1 := 7-2;

Example in FBD:

DIV

Division of one variable by another of the types: BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT, REAL and LREAL.

Example in IL:

LD 8

DIV 2

ST Var1 (* Result is 4 *)

Example in ST:

var1 := 8/2;

Example in FBD:

10-2

CoDeSys V2.3

Appendix A: - IEC Operators and additional norm extending functions

Note: If you define functions in your project with the names CheckDivByte, CheckDivWord, CheckDivDWord and CheckDivReal, you can use them to check the value of the divisor if you use the operator DIV, for example to avoid a division by 0. The functions must have the above listed names.

Attention: Please regard, that different target systems may behave differently concerning a division by zero !

See in the following an example for the implementation of function CheckDivReal: Example for the implementation of the function CheckDivReal:

FUNCTION CheckDivReal : REAL

VAR_INPUT divisor:REAL;

END_VAR

IF divisor = 0 THEN CheckDivReal:=1;

ELSE

CheckDivReal:=divisor; END_IF;

Operator DIV uses the output of function CheckDivReal as divisor. In a program like shown in the following example this avoids a division by 0, the divisor (d) is set from 0 to 1. So the result of the division is 799.

PROGRAM PLC_PRG

VAR erg:REAL;

v1:REAL:=799;

d:REAL; END_VAR

erg:= v1 / d;

MOD

Modulo Division of one variable by another of the types: BYTE, WORD, DWORD, SINT, USINT, INT, UINT, DINT, UDINT. The result of this function will be the remainder of the division. This result will be a whole number.

Example in IL:

LD 9

MOD 2

ST Var1 (* Result is 1 *)

Example in ST:

var1 := 9 MOD 2;

Example in FBD:

MOVE

Assignment of a variable to another variable of an appropriate type. As MOVE is available as a box in the graphic editors FBD, LD, CFC, there the (unlocking) EN/EN0 functionality can also be applied on a variable assignment.

Example in CFC in conjunction with the EN/EN0 function: Only if en_i is TRUE, var1 will be assigned to var2.

Example in IL:

LD ivar1

MOVE ivar2

CoDeSys V2.3

10-3

Appendix A: - IEC Operators and additional norm extending functions

ST ivar2 (* Ergebnis: var2 erhält Wert von var1 *)

( ! you get the same result with:

LD ivar1 ST ivar2 )

Example in ST:

ivar2 := MOVE(ivar1);

( ! you get the same result with: ivar2 := ivar1; )

INDEXOF

This function is not prescribed by the standard IEC61131-3.

Perform this function to find the internal index for a POU.

Example in ST:

var1 := INDEXOF(POU2);

SIZEOF

This function is not prescribed by the standard IEC61131-3.

Perform this function to determine the number of bytes required by the given variable.

Example in IL: arr1:ARRAY[0..4] OF INT;

Var1 INT

LD arr1 SIZEOF

ST Var1 (* Result is 10 *)

Example in ST:

var1 := SIZEOF(arr1);

10.2Bitstring Operators...

AND

Bitwise AND of bit operands. The operands should be of the type BOOL, BYTE, WORD or DWORD.

Example in IL:

Var1 BYTE

LD 2#1001_0011

AND 2#1000_1010

ST Var1 (* Result is 2#1000_0010 *)

Example in ST:

var1 := 2#1001_0011 AND 2#1000_1010

Example in FBD:

Note: If you have a program step in the SFC like the following

10-4

CoDeSys V2.3

Appendix A: - IEC Operators and additional norm extending functions

and if you use 68xxxor C-code generators, please note the following: The allocation of the value of the second input variable at the AND operator module to variable z will not be executed ! This is due to the optmized processing in the SFC in case of value FALSE at the input variable.

OR

Bitwise OR of bit operands. The operands should be of the type BOOL, BYTE, WORD or DWORD.

Example in IL: var1 :BYTE;

LD 2#1001_0011

OR 2#1000_1010

ST var1 (* Result is 2#1001_1011 *)

Example in ST:

Var1 := 2#1001_0011 OR 2#1000_1010

Example in FBD:

Note: If you have a program step in the SFC like the following

and if you use 68xxxor C-code generators, please note the following: The allocation of the value of the second input variable at the OR operator module to variable z will not be executed ! This is due to the optmized processing in the SFC in case of value FALSE at the input variable.

XOR

Bitwise XOR of bit operands. The operands should be of the type BOOL, BYTE, WORD or DWORD.

Note: Regard the behaviour of the XOR function in extended form, that means if there are more than 2 inputs. The inputs will be checked in pairs and the particular results will then be compared again in pairs (this complies with the standard, but may not be expected by the user).

Example in IL:

Var1 :BYTE;

LD 2#1001_0011

XOR 2#1000_1010

ST Var1 (* Result is 2#0001_1001 *)

Example in ST:

Var1 := 2#1001_0011 XOR 2#1000_1010

Example in FBD:

CoDeSys V2.3

10-5

Appendix A: - IEC Operators and additional norm extending functions

NOT

Bitwise NOT of a bit operand. The operand should be of the type BOOL, BYTE, WORD or DWORD.

Example in IL:

Var1 :BYTE;

LD 2#1001_0011

NOT

ST Var1 (* Result is 2#0110_1100 *)

Example in ST:

Var1 := NOT 2#1001_0011

Example in FBD:

10.3Bit-Shift Operators...

Note: Please note, that the amount of bits, which is regarded for the arithmetic operation, is pretended by the data type of the input variable !. If the input variable is a constant the smallest possible data type is regarded. The data type of the output variable has no effect at all on the arithmetic operation.

SHL

Bitwise left-shift of an operand : erg:= SHL (in, n)

in gets shifted to the left by n bits. If n > data type width, for BYTE, WORD and DWORD will be filled with zeros. But if signed data types are used, like e.g. INT, then an arithmetic shift will be executed in such cases, that means it will be filled with the value of the topmost bit.

Note: See in the following example in hexadecimal notation that you get different results for erg_byte and erg_word depending on the data type of the input variable (BYTE or WORD), although the values of the input variables in_byte and in_word are the same.

Example in ST:

PROGRAM shl_st

VAR

in_byte : BYTE:=16#45;

in_word : WORD:=16#45; erg_byte : BYTE; erg_word : WORD;

n: BYTE :=2;

END_VAR

erg_byte:=SHL(in_byte,n); (* Result is 16#14 *) erg_word:=SHL(in_word;n); (* Result is 16#01141 *)

Example in FBD:

Example in IL:

LD 16#45

SHL 2

ST erg_byte

10-6

CoDeSys V2.3

Appendix A: - IEC Operators and additional norm extending functions

SHR

Bitwise right-shift of an operand: erg:= SHR (in, n)

in gets shifted to the right by n bits. If n > data type width, for BYTE, WORD and DWORD will be filled with zeros. But if signed data types are used, like e.g. INT, then an arithmetic shift will be executed in such cases, that means it will be filled with the value of the topmost bit.

See the following example in hexadecimal notation to notice the results of the arithmetic operation depending on the type of the input variable (BYTE or WORD).

Example in ST:

PROGRAM shr_st

VAR

in_byte : BYTE:=16#45;

in_word : WORD:=16#45; erg_byte : BYTE; erg_word : WORD;

n: BYTE :=2; END_VAR

erg_byte:=SHR(in_byte,n); (* Result is 11 *) erg_word:=SHR(in_word;n); (* Result is 0011 *)

Example in FBD:

Example in IL:

LD 16#45

SHR 2

ST erg_byte

ROL

Bitwise rotation of an operand to the left: erg:= ROL (in, n)

erg, in and n should be of the type BYTE, WORD or DWORD. in will be shifted one bit position to the left n times while the bit that is furthest to the left will be reinserted from the right.

See in the following example in hexadecimal notation that you get different results for erg_byte and erg_word depending on the data type of the input variable (BYTE or WORD), although the values of the input variables in_byte and in_word are the same.

Example in ST:

PROGRAM rol_st

VAR

in_byte : BYTE:=16#45;

in_word : WORD:=16#45; erg_byte : BYTE; erg_word : WORD;

n: BYTE :=2;

END_VAR

erg_byte:=ROL(in_byte,n); (* Result is 16#15 *) erg_word:=ROL(in_word;n); (* Result is 16#0114 *)

Example in FBD:

Example in IL:

LD 16#45

ROL 2

CoDeSys V2.3

10-7

Соседние файлы в папке 759-333