Chapter VII Math and conversion functions

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION Chapter VII Math and conversion functions In math functions only variables with data type numeric and/or ...
Author: Gyles Daniels
3 downloads 1 Views 560KB Size
APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

Chapter VII Math and conversion functions In math functions only variables with data type numeric and/or numbers (constants) are used. The numbers can be integer and real numbers. Integer numbers are whole binary numbers with a preceding sign (- or +). There are two types of integer numbers: integer INT and double integer DINT. Real numbers are floating-point numbers with a preceding sign (- or +). Real number variables are declared as data type REAL. In Table 0.1 the numerical data types, their range, bit number (how much memory it takes) and constant samples (how the numbers are written in a user program) are shown. Table 0.1. Integer and real numbers

Data type Allowed range

INT -32 768 to 32 768

Memory range Constant samples

16 bits = 1 word 234 -15 674

DINT -214 783 648 to +214 783 647 32 bits = 2 words L#25 000 L#-345 764

REAL ±1.75495e-38 to ±3.402823e+38 32 bits = 2 words 1.578 -2345.0 1.2e3 (1200) 3.5e-2 (0.035)

Arithmetic functions IEC 61131 offers the following arithmetic functions: addition (ADD), subtraction (SUB), multiplication (MUL), division (DIV), division with remainder (MOD) and exponentiation (EXPT). STEP 7 has four arithmetic functions for standard arithmetic calculations with integer (INT), double integer (DINT) and real (REAL) numbers and variables/operands, which are declared as data types INT, DINT or REAL. These are addition, subtraction, multiplication and division. The division with integers (DIV_I) and double integer (DIV_DI) numbers provides a quotient result only. However, double integer division can give out also the division remainder (fraction) as a result if the function MOD_DI is used for division. The integer, double integer and real arithmetic functions in STEP 7 are shown in Table 0.2. The arithmetic functions are performed only with the same data types (numbers, variables and operands) input operands and output operands have to be of the same data type. Table 0.2. Integer and real number arithmetic functions in STEP 7

Operation

Addition Subtraction Multiplication Division Division with remainder

Arithmetic functions using data type INT DINT REAL STL +I -I *I /I -

FBD/ LAD ADD_I SUB_I MUL_I DIV_I -

STL +D -D *D /D MOD

FBD/ LAD ADD_DI SUB_DI MUL_DI DIV_DI MOD_DI

STL +R -R *R /R -

FBD/ LAD ADD_R SUB_R MUL_R DIV_R -

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

STEP 7 uses CPU accumulators ACCU 1 and ACCU 2 for arithmetic operations. Read more about it in modules 4.6 and 5.1.

Some examples of the STEP 7 arithmetic functions are shown in Fig. 0.1. An addition function with integer variables is shown in STL, a multiplication function with real variables is shown in FBD and a division MOD function with double integer variables is shown in LAD.

Fig. 0.1. Some samples of arithmetic functions in STL, FBD and LAD languages

Numeric functions The numeric functions have only one input operand. The result is the functional value [6]. IEC 61131 defines the following functions as numeric functions: absolute value (ABS), square root (SQRT), natural logarithm (LN), logarithm (LOG), exponential value (EXP), sine (SIN), cosine (COS), tangent (TAN), arc sine (ASIN), arc cosine (ARCOS), and arc tangent (ARTAN). IEC 61131 requires that numeric functions must have the same operand types in the function input and output. In most numeric functions only the data type REAL can be used. Only exception is the absolute value function, which can use also work with data types INT and DINT. STEP 7 has most numeric functions defined in IEC 61131 - only the logarithm function is missing. In Table 0.3 STEP 7 numeric functions in STL and FBD languages are shown. In STEP 7 all numerical functions use real numbers in inputs (IN) and outputs (OUT). By numeric functions only accumulator ACCU 1 is used. Read more about it in modules 4.6 and 5.1. Table 0.3. STEP 7 numeric functions

Name LN (natural logarithm)

EXP (exponential value)

SIN (Sine)

STL

FBD/LAD

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

Name

STL

FBD/LAD

COS (Cosine)

TAN (Tangent)

ASIN (Arc Sine)

ACOS (Arc Cosine)

ATAN (Arc Tangent)

ABS (Absolute value)

SQR (Square)

SQRT (Square root)

Function LN calculates the natural logarithm (logarithm to base e = 2.718282) of a real number (IN). The following equation can be used to calculate logarithm to any other base (Logba) if natural logarithm is used.

. The logarithm to base 10 can be calculated with the following equation:

. Function EXP calculates the exponential value (exponential value for base e) of a real number. The following equation can be used to calculate exponential to any other base (ba).

Functions SIN, COS or TAN calculate the sine, cosine or tangent of an angle, which is given in radian. The angle value has to be from 0 to 2π (π = 3.14). If the angle radian value is

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

greater than 2π, then 2π has to be subtracted from it as long as the value becomes less than or equal to 2π. Functions ASIN, ACOS or ATAN calculate the arc sine, arc cosine or arc tangent of a real number. The result is an angle specified in a radian measure. In the case of functions ASIN and ACOS, the input operand IN value must be greater than or equal to -1 and less than or equal to +1. In Fig. 0.2 the possible values of functions ASIN, ACOS and ATAN in radian are shown. Function ASIN output value range:

Function ACOS output value range:

Function ATAN output value range:

Fig. 0.2 Range of possible values of functions ASIN, ACOS and ATAN in radians

Function ABS produces the absolute value of a real number, function SQR calculates the square of a real number and function SQRT calculates the square root of a real number. The function SQRT result is positive if the input operand value is greater than or equal to zero.

Conversion functions Conversion functions are used to convert data from one type to another [6]. For example, an operand in the BCD format has to be used in arithmetic functions. Firstly, the operand data has to be converted into an integer or real format. After that the converted data can be used in arithmetic calculations. If the arithmetic result is needed in the BCD format, then the result has to be converted back to the BCD format. In Table 0.4 some IEC 61131 conversion functions are shown. Table 0.4. IEC 61131 conversion functions

Name

Comment

BCD_TO_INT

The BCD format value in function input is converted to the data type integer value. The integer format value in function input is converted to the data type BCD value. The integer format value in function input is converted to the real format value. The real format value in function input is converted to the integer format value. When converting the number is rounded to the nearest integer number. The real format value in function input is converted to the integer format value. The result is the integer component of the real number. The real number decimal point is deleted.

INT_TO_BCD INT_TO_REAL REAL_TO_INT TRUNC

STEP 7 conversion functions in STL, FBD and LAD languages are listed in Table 0.5. STEP 7 uses only accumulator ACCU 1 for conversion functions. Read more about it in modules 4.6 and 5.1.

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

Table 0.5. STEP 7 conversion functions

Name

STL

BCD_TO_INT

BTI

BCD_I

BCD_TO_DINT

BTD

BCD_DI

INT_TO_BCD

ITB

I_BCD

INT_TO_DINT

ITD

I_DI

DINT_TO_BCD

DTB

DI_BCD

DINT_TO_REAL

DTR

DI_R

REAL_TO_INT

RND

ROUND

RND+

CEIL

RND-

FLOOR

TRUNC

TRUNC

INVI INVD NEGI NEGD NEGR

INV_I INV_DI NEG_I NEG_DI NEG_R

INVI INVD NEGI NEGD NEGR

FBD/LAD Comment BCD value is converted to 16 bit integer value. BCD value is converted to 32 bit (double) integer value. 16 bit integer value is converted to the BCD value. 16 bit integer value is converted to 32 bit (double) integer value. 32 bit (double) integer value is converted to the BCD value. 32 bit (double) integer value is converted to a real value. Real value is rounded to 32 bit (double) integer value. Real value is rounded to 32 bit (double) integer value, which is greater than or equal to the specified real number. Real value is rounded to 32 bit (double) integer value, which is less than or equal to the specified real number. Real value is truncate to 32 bit (double) integer value. The result is the integer component of the real number. Ones Complement integer (16 bit). Ones Complement double integer (32 bit). Twos Complement integer (16 bit). Twos Complement double integer (32 bit). Real value is negated.

Conversion of BCD values BCD values are converted to integer or to double integer values by using BCD_TO_INT or BCD_TO_DINT functions (Table 0.5). Only BCD values with 3 (integer) or 7 (double integer) decades are converted (Fig. 0.3). The BCD value preceding sign is stored in bit 15 or in 31 (“0” for positive, “1” for negative). An error can occur in the BCD conversion if in one of the BCD decade has a number from 10 to 15. With this error the PLC can go to the STOP mode. BCD +

0

Bit 31

2

3

5

16 15

7

2

1 0

MD 24 (ACCU 1) 0000 0000 0010 0011 0101 1000 0010 0001 BCD_TO_DINT MD 28 (ACCU 1) 0000 0000 0000 0011 1001 1000 1100 1001 INT Fig. 0.3. BCD value conversion to double integer

+235721

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

Some samples of the STEP 7 BCD value conversion functions are shown in Fig. 0.4. In STL and FBD language BCD_TO_INT function and in LAD a BCD_TO_DINT function is shown.

Fig. 0.4 Some samples of BCD value conversion in STL, FBD and LAD languages

Conversion of integer and double integer values Integer and double integer values can be converted to integer, double integer, real and BCD values by using INT_TO_BCD, INT_TO_DINT, DINT_TO_BCD, DINT_TO_INT and DINT_TO_REAL functions (Table 0.5). Functions INT_TO_BCD and DINT_TO_BCD are used to convert integer and double integer values into BCD values (Fig. 0.5). The (double) integer value preceding sign is stored from bit 12 to 15 (by double integer the bits are 28 to 31) (“0” = positive, “1” = negative). If the integer value is greater than 999 or less than -999, then the integer value conversion to BCD value is not carried out. The same thing happens with double integer value, which is greater than 9999999 or less than -9999999. -265

INT Bit 15

8 7

0

MW 32 (ACCU 1) 1111 1110 1111 0111 INT_TO_BCD MW 34 (ACCU 1) 1111 0010 0110 0101 BCD

-

2

6

5

Fig. 0.5. Integer value conversion to BCD

By INT_TO_DINT function integer value is converted to the double integer value. The first 15 bits of integer are transferred straight to the first 15 bits of double integer (Fig. 0.6). The last bit (15) of integer value is also transferred to double integer, from bit 15 upwards up to bit 31. For converting the double integer value to the integer value transfer functions (MOVE) have to be used in STEP 7. If the double integer value exceeds the integer value limits, then a random number is given as an integer value.

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

INT

-16533

Bit

15

MW 44 (ACCU 1)

0

1011 1111 0110 1011 INT_TO_DINT

MD 46 (ACCU 1) 1111 1111 1111 1111 1011 1111 0110 1011 Bit 31

16 15

DINT DINT

0

-16533 500

Bit 31

16 15

0

MD 50 (ACCU 1) 0000 0000 0000 0000 0000 0001 1111 0100

M = 2-1 +2-2 +2-3 +2-4 +2-6 = = 0.953125

DINT_TO_REAL MD 54 (ACCU 1) 0 100 0011 1 111 1010 0000 0000 0000 0000 Bit 31

+

22

135

REAL

1615

E = 27 + 22 + 21 + 20 = 135

0

REAL = +1.953125 * 2135-127= =500.0

0.953125 500.0

Fig. 0.6. Integer conversion to double integer and double integer conversion to real

In the DINT_TO_REAL function the double integer value is converted to a real value with the data type REAL (Fig. 0.6). Some samples of the STEP 7 integer and double integer value conversion functions are shown in Fig. 0.7. An INT_TO_BCD function is shown in STL, an INT_TO_DINT function is shown in FBD and a DINT_TO_REAL function shown in LAD.

Fig. 0.7. Some samples of integer and double integer value conversion in STL, FBD and LAD languages

Conversion of real values STEP 7 offers 4 functions to convert real values to double integer. These 4 conversion functions differ in the execution of rounding. The ROUND function (RND) rounds the result to the nearest whole number. If the fractional part of the converted number is midway between an even and an odd result, then the function chooses the even result (Fig. 0.8) [6].

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

RND - 0.51 - 0.5 - 0.49 +0.49 +0.5 +0.51 REAL (ACCU 1)

RND+ -1 0 0 0 0 +1

DINT (ACCU 1) RND-

- 1.1 - 0.51 - 0.49 +0.49 +0.51 +1.1 REAL (ACCU 1)

-2 -1 -1 0 0 +1 DINT (ACCU 1)

- 1.1 - 0.51 - 0.49 +0.49 +0.51 +1.1

-1 0 0 +1 +1 +2

REAL (ACCU 1)

DINT (ACCU 1)

TRUNC - 1.1 - 0.51 - 0.49 +0.49 +0.51 +1.1 REAL (ACCU 1)

-1 0 0 0 0 +1 DINT (ACCU 1)

Fig. 0.8. Real value conversion to double integer value by different STEP 7 conversion functions

The CEIL function (RND+) rounds the result to the smallest whole number, which is greater than or equal to the real number that is used for conversion (Fig. 0.8). The FLOOR function (RND-) rounds the result to the largest whole number, which is less than or equal to the real number that is used for conversion (Fig. 0.8). The TRUNC function (TRUNC) result is the integer number part of the converted real number (Fig. 0.8). The real value conversion does not take place if the value exceeds the double integer value limits. Some samples of the STEP 7 real value conversion functions are shown in Fig. 0.9. A ROUND function is shown in STL, a CEIL function is shown in FBD and a TRUNC function is shown in LAD.

Fig. 0.9. Some samples of real value conversion in STL, FBD and LAD languages

Conversion through complementation Two types of individual complementation are known in digital techniques. The Ones Complement inverts each register bit individually. The Twos Complement results from the Ones Complement by adding one. The formation of the Ones Complement is done by

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

function “NOT”. The formation of Twos Complements is done with a multiplication with -1 [6]. These functions are not included in IEC 61131. Both complementations exist in STEP 7 STL, FBD and LAD languages as inversion and negation functions.

DINT Bit 31

+235721

1

16 15

0

MD 86 (ACCU 1) 0000 0000 0000 0011 1001 1000 1100 1001 INVD MD 90 (ACCU 1) 1111 1111 1111 1100 0110 0111 0011 0110 DINT

-235722

Fig. 0.10. Ones Complement creation of double integer value

Ones Complement (INVI, INVD) function inverts the (double) integer value bit by bit, that is, “0” is replaced by “1” and “1” is replaced by “0” (Fig. 0.10). In the INVI function only the first 16 bits of ACCU 1 are inverted. DINT Bit 31

-1407009 16 15

1 0

MD 98 (ACCU 1) 1111 1111 1110 1010 1000 0111 1101 1111 NEGD MD 102 (ACCU 1) 0000 0000 0001 0101 0111 1000 0010 0001 DINT

+1407009

Fig. 0.11. Twos Complement creation of double integer value

The Twos Complement function (NEGI, NEGD) of (double) integer value is performed in two steps. In the first step the integer value bits are inverted (Ones Complement is created). In the second step +1 is added to the inverted value (Fig. 0.11). In NEG_INT functions only the first 16 bits of ACCU 1 are used by the Twos Complement creation. The real Twos Complement creation (NEGR) negates the real value. Only the bit 31(sign of the mantissa) is inverted with this function. Some samples of the STEP 7 Ones and Twos Complement functions are shown in Fig. 0.12. An Ones Complement function with integer variables is shown in STL, a Twos Complement function with double integer values is shown in FBD and a Twos Complement function with real variables is shown in LAD.

Fig. 0.12. Some samples of Ones and Twos Complement conversion in STL, FBD and LAD languages

APPLICATION OF PLC IN INDUSTRIAL AUTOMATION

Conversion of BYTE, WORD and DWORD STEP 7 transfer functions (MOVE) can be used for conversion of data types BYTE, WORD and DWORD into other data types like BYTE, WORD, INTE, DWORD, DINT etc. in STL, FBD and LAD languages. Sometimes, when the input value exceeds the output value limits, it can happen that a random number is given as the output in the transfer function.

Self check 1. Which is the allowed range of data type DINT (double integer)? a. -32 768 to 32 768 b. -214 783 648 to +214 783 647 c. ±1.75495e-38 to ±3.402823e+38 2. Which of the following functions cannot be performed with integer numbers (multiple answers)? a. Addition b. Multiplication c. Division with remainder d. Sine e. Square f. Absolute value 3. Which of these numeric functions is not possible in STEP 7 (multiple answers)? a. Tangent b. Cotangent c. Arc Tangent d. Arc Cotangent 4. How is the output of function ACOS given? a. In degrees b. In radians c. As a real number 5. Which of the following functions can be used in STEP 7 to convert a double integer value to an integer value? a. DINT_TO_INT b. INT_TO_DINT c. BCD_TO_INT d. MOVE 6. The result of the TRUNC function is… a. the integer number part of the real number. b. the largest whole number, which is smaller than or equal to the real number. c. the nearest whole number. d. the smallest whole number, which is greater than or equal to the real number. 7. Once Complementation allows one to … a. convert floating point numbers to the BCD code. b. invert integer numbers. c. negate integer numbers. d. invert real numbers.