Arithmétique Step7
Arithmétique Step7
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
STL +I -I *I /I -
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
STL
FBD/LAD
SIN (Sine)
STL
FBD/LAD
TAN (Tangent)
SQR (Square)
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.
. 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
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:
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
BCD_TO_INT INT_TO_BCD INT_TO_REAL REAL_TO_INT TRUNC
Comment
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.
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.
Name
BCD_TO_INT BCD_TO_DINT INT_TO_BCD INT_TO_DINT DINT_TO_BCD DINT_TO_REAL REAL_TO_INT
STL
BTI BTD ITB ITD DTB DTR RND RND+
FBD/LAD Comment
BCD_I BCD_DI I_BCD I_DI DI_BCD DI_R ROUND CEIL 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.
RND-
FLOOR
TRUNC
TRUNC
3
16 15
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
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
-265
8 7 0
MW 32 (ACCU 1) 1111 1110 1111 0111 INT_TO_BCD MW 34 (ACCU 1) 1111 0010 0110 0101 BCD Fig. 0.5. Integer value conversion to BCD 2 6 5
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.
INT
Bit 15
-16533
0
MW 44 (ACCU 1)
DINT DINT
Bit 31
-16533 500
16 15 0
MD 50 (ACCU 1) 0000 0000 0000 0000 0000 0001 1111 0100 DINT_TO_REAL MD 54 (ACCU 1) 0 100 0011 1 111 1010 0000 0000 0000 0000
Bit 31 22 1615 0
E = 27 + 22 + 21 + 20 = 135 M = 2-1 +2-2 +2-3 +2-4 +2-6 = = 0.953125 REAL = +1.953125 * 2135-127= =500.0
+ REAL
135
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
RND - 0.51 - 0.5 - 0.49 +0.49 +0.5 +0.51 REAL (ACCU 1) -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 REAL (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
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
16 15
1
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
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
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
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.