04/09/1444
ADVANCED PLC
Dr. Tamer Fetouh
Mathematical Operations
• Tia portal provides different instructions for
Math functions that covers most of
calculations operations that may be needed in
control and monitoring.
• The complete list of math functions available
in S7-1200 PLC is shown in Fig.
• The data types of operand and the result
should be compatible with the data type of the
math function.
1
04/09/1444
Mathematical Operations
Calculate
• The "Calculate" instruction is used to define and execute an expression for
the calculation of mathematical operations or complex logic operations
depending on the selected data type.
• The following table shows the
instructions that, depending on the
selected data type, can be combined
and executed in the expression of the
"Calculate" instruction:
Mathematical Operations
2
04/09/1444
Mathematical Operations
Mathematical Operations
3
04/09/1444
Mathematical Operations
Mathematical Operations
4
04/09/1444
Mathematical Operations
• The mathematical instructions can be
included in the "Calculate" instruction or
have a separate blocks.
• For example, you can add two or more
inputs using the Calculate block or the
ADD block as shown below.
• To add more inputs to the Math operation,
use the * icon behind the IN2.
Mathematical Operations
• The MUL instruction shown in Fig. multiplies two inputs (IN1, IN2) and store the
result in (OUT).
• The data type of the MUL is set to Int
• The IN1 tag has a Real data type.
• The IN2 tag has an Int data type.
• The OUT tag has a Real data type.
• An Implicit conversion is carried out by default at
the OUT and IN1 to convert the data type from:
• Real to Int at the IN1
• Int to Real at the OUT
10
5
04/09/1444
Mathematical Operations
• The LIMIT operation guarantees the level of a specific signal is kept between
Minimum (MN) & Maximum (MX) levels.
1. If the value of IN < MN then: OUT = MN
2. If the value of IN > MX then: OUT = MX
3. If MN < IN < MX then: OUT = IN
11
Data Type Conversion
• If several operands are linked to an instruction, make sure that the data
types are compatible.
• If the operands are not the same data type, a conversion has to be carried
out.
• There are two options for the conversion:
1. Implicit conversion
• The conversion take place automatically when the instruction is executed.
2. Explicit conversion
• You use an explicit conversion instruction before the actual instruction is
executed.
12
6
04/09/1444
Data Type Conversion
1. Implicit conversion
2. Explicit conversion
13
Conversion Operations
• The operands data types may need to be converted
from type to another to suit the operation block
data type.
• The instruction CONVERT is used to convert
explicitly the data types.
• Other conversion instructions are available as
shown in Fig.
• In addition, operands may need to be Scaled or
normalized. Using the instructions (SCALE_X,
NORM_X)
14
7
04/09/1444
Conversion Operations
• Round ➔ convert the number to the nearest integer number
Round (1.2) = 1 Round (1.7) = 2
Round ( – 3.95) = – 4 Round (– 3.1) = – 3
• Trunc ➔ removes the fraction part of the number
Trunc (1.2) = 1 Trunc (1.7) = 1
Trunc ( – 3.95) = – 3 Trunc (– 3.1) = – 3
15
Conversion Operations
• Ceil ➔ convert the number to the higher integer number
Ceil (1.2) = 2 Ceil (1.7) = 2
Ceil ( – 3.95) = – 3 Ceil (– 5.49) = – 5
• Floor ➔ convert the number to the lower integer number
Ceil (1.2) = 1 Ceil (1.7) = 1
Ceil ( – 3.95) = – 4 Ceil (– 5.49) = – 6
16
8
04/09/1444
Conversion Operations
SCALE_X ➔ To scale the value at the VALUE input by mapping it to a specified
value range (0 to 1 are mapped to MIN to MAX).
• When the "Scale" instruction is executed, the
floating-point value at the VALUE input is
scaled to the value range that was defined by
the MIN and MAX parameters.
• The result of the scaling is stored in the OUT.
• MIN, MAX & OUT are of the same data type and
are assigned according to the target data type.
17
Conversion Operations
• The following figure shows how the values can be scaled.
𝑽𝒂𝒍𝒖𝒆 𝑶𝒖𝒕−𝑴𝑰𝑵
= MAX
𝟏−𝟎 𝑴𝑨𝑿−𝑴𝑰𝑵
Out
𝑶𝒖𝒕 = 𝑴𝑰𝑵 + 𝑽𝒂𝒍𝒖𝒆 ∗ (𝑴𝑨𝑿 − 𝑴𝑰𝑵)
MIN
• If the MIN = 0, then:
0 Value 1
𝑶𝒖𝒕 = 𝑽𝒂𝒍𝒖𝒆 ∗ 𝑴𝑨𝑿
18
9
04/09/1444
Conversion Operations
• If Min = 0, Max = 10, then:
𝑶𝒖𝒕 = 𝑽𝒂𝒍𝒖𝒆 ∗ 𝑴𝑨𝑿 = 𝟏𝟎 ∗ 𝑽𝒂𝒍𝒖𝒆
• If the input value is 0.5, then:
Out = 0.5 * 10 = 5
• If the input value is 7, then:
Out = 7 * 10 = 70
19
Conversion Operations
• If Min = 5, Max = 10, then:
𝑶𝒖𝒕 = 𝑴𝑰𝑵 + 𝑽𝒂𝒍𝒖𝒆 ∗ (𝑴𝑨𝑿 − 𝑴𝑰𝑵)
𝑶𝒖𝒕 = 𝟓 + 𝟓 ∗ 𝑽𝒂𝒍𝒖𝒆
• If the input value is 0.5, then:
Out = 5 + 0.5 * 5 = 7.5
20
10
04/09/1444
Conversion Operations
NORM_X ➔ To normalize the value of the tag at the VALUE input by mapping it
to a linear scale. (MIN to MAX are mapped to 0 to 1)
• The result at the OUT is calculated and
stored as a floating-point number
depending on the location of the value to
be normalized within this value range.
• MIN, MAX & VALUE are of the same data
type and are assigned according to the
input data type.
21
Conversion Operations
• The following figure shows how the values can be scaled.
𝑶𝒖𝒕 𝑽𝒂𝒍𝒖𝒆−𝑴𝑰𝑵
= 1
𝟏−𝟎 𝑴𝑨𝑿−𝑴𝑰𝑵
𝑽𝒂𝒍𝒖𝒆 − 𝑴𝑰𝑵 Out
𝑶𝒖𝒕 =
(𝑴𝑨𝑿 − 𝑴𝑰𝑵)
0
• If the MIN = 0, then:
Min Value MAX
𝑶𝒖𝒕 = 𝑽𝒂𝒍𝒖𝒆/𝑴𝑨𝑿
22
11
04/09/1444
Conversion Operations
• If Min = 0, Max = 10, then:
𝑽𝒂𝒍𝒖𝒆 𝑽𝒂𝒍𝒖𝒆
𝑶𝒖𝒕 = =
𝑴𝑨𝑿 𝟏𝟎
• If the input value is 8, then:
Out = 8 / 10 = 0.8
• If the input value is 70, then:
Out = 70 / 10 = 7
23
Conversion Operations
• If Min = 5, Max = 10, then:
𝑽𝒂𝒍𝒖𝒆 − 𝑴𝑰𝑵
𝑶𝒖𝒕 =
(𝑴𝑨𝑿 − 𝑴𝑰𝑵)
𝑽𝒂𝒍𝒖𝒆 − 𝟓
𝑶𝒖𝒕 =
𝟓
• If the input value is 8, then:
Out = ( 8 – 5 ) / 5 = 0.6
24
12