The digital functions process digital values mainly with the data types INT, DINT, and REAL.
The “simple” digital functions are implemented with SCL through the value assign- ment of an expression. When linking two values, the type of digital function depends on the operator used: comparison expression (comparison functions), arithmetic expression (arithmetic and mathematical functions), or logic expres- sion (word logic operations). The functions for data type conversion (conversion functions) and for shifting and rotating are available for manipulating just one value.
//Simple lock counter
#temp_bool1 := #Light_barrier1 AND NOT #“Light_barrier1.edge”;
#“Light_barrier1.edge” := #Light_barrier1;
#temp_bool2 := #Light_barrier2 AND NOT #“Light_barrier2.edge”;
#“Light_barrier2.edge” := #Light_barrier2;
#Lock_counter(CU := #Light_barrier2 AND #temp_bool1, CD := #Light_barrier1 AND #temp_bool2, R := #Acknowledge, LD := FALSE, PV := 0);
Fig. 10.12 Example of IEC counter function with SCL
10.5.1 Transfer function, value assignment of a digital tag
The “simple” transfer function corresponds with SCL to the value assignment.
A detailed description of the transfer functions is provided in Chapter 13.2 “Trans- fer functions” on page 508. Example of a value assignment: The value of the #Mes- sages tag in data block “Data.SCL” is assigned to the “Message_bits” tag in the mem- ory area.
“Message_bits” := “Data.SCL”.Messages;
10.5.2 Comparison functions
A comparison function compares the values of two tags.
SCL implements the comparison function using a comparison operator. The com- parison result has the data type BOOL and can be linked further like a Boolean tag.
The comparison result has signal state TRUE if the comparison is fulfilled, other- wise FALSE. The comparison function is described in Chapter 13.3 “Comparison functions” on page 518. Table 10.5 shows the comparison operators available with SCL.
Two comparison functions are programmed in Fig. 10.13. In the first comparison, the #Measurement_temperature tag is compared with “Lower_limit”, in the second comparison with “Upper_limit”. The result of the two comparisons is linked accord- ing to AND and saved in the #Measurement_in_range tag. “Lower_limit” and “Up- per_limit” are created as symbolically addressed user constants.
Table 10.5 Comparison functions with SCL
Operator Description Operand data types
=
<>
<
<=
>
>=
Compare for equal Compare for unequal Compare for greater than Compare for greater than-equal Compare for less than Compare for less than-equal
INT, DINT, REAL, CHAR, STRING,
TIME, DATE, TIME_OF_DAY
=
<>
Compare for equal Compare for unequal
BOOL, BYTE, WORD, DWORD
#Measurement_in_range := (#Measurement_temperature >= "Lower_limit") AND (Measurement_temperature <= "Upper_limit");
Fig. 10.13 Example of comparison expressions with SCL
10.5.3 Arithmetic functions
The arithmetic functions for numerical values implement the basic arithmetical operations with the data formats INT, DINT, and REAL. SCL uses an arithmetic op- erator for this.
The program elements catalog contains the arithmetic functions under Basic instructions > Math functions.
A detailed description of these arithmetic functions is provided in Chapter 13.4
“Arithmetic functions” on page 521. Table 10.6 shows the arithmetic operators available with SCL.
The data type of the first and second operands can be INT, DINT, or REAL. If you link INT and DINT operands together, the result is of data type DINT; if you link an INT or DINT operand with a REAL operand, the result is of data type REAL. The program editor carries out a corresponding data type conversion (not visible to the user) pri- or to the arithmetic operation (see also Chapter 13.6.1 “Implicit data type conver- sion” on page 531).
In the case of a division, the second operand must not be zero.
In Fig. 10.14, the upper limit of a measured value is monitored. A hysteresis is introduced to ensure that the #Measurement_too_high and #Measurement_too_low messages do not “pulsate” when the measurement changes rapidly around the upper or lower limit. The messages are only canceled when the measured value has dropped again below the upper limit or risen again above the upper limit by the magnitude of the hysteresis.
Table 10.6 Arithmetic functions with SCL
Data type
Operator Description 1. operand 2. operand Result
** Power INT, DINT, REAL INT REAL
* Multiplication INT, DINT, REAL
TIME
INT, DINT, REAL INT, DINT
INT, DINT, REAL TIME
/ Division INT, DINT, REAL INT, DINT, REAL INT, DINT, REAL
DIV Integer division INT, DINT
TIME
INT, DINT INT, DINT
INT, DINT TIME MOD Division with remainder as result INT, DINT INT, DINT INT, DINT
+ Addition INT, DINT, REAL
TIME TOD DT
INT, DINT, REAL TIME TIME TIME
INT, DINT, REAL TIME TOD DT
– Subtraction INT, DINT, REAL
TIME TOD DATE TOD DT
INT, DINT, REAL TIME TIME DATE TOD TIME
INT, DINT, REAL TIME TOD TIME TIME DT
10.5.4 Math functions
The mathematical functions comprise the trigonometric functions, exponential functions, and logarithmic functions, and deliver a result in data format REAL.
A detailed description of these math functions is provided in Chapter 13.5 “Math functions” on page 527. Table 10.7 shows the mathematical functions available with SCL.
The program elements catalog contains the arithmetic functions under Basic instructions > Basic instructions > Math functions.
Fig. 10.15 shows the calculation of reactive power using the SIN function, calcula- tion of the volume of a sphere, the solution of a quadratic equation, and calculation of an arithmetic mean value.
IF #Measurement_temperature >= “Upper_limit”
THEN #Measurement_too_high := TRUE; END_IF;
IF #Measurement_temperature <= “Upper_limit” - “Hysteresis”
THEN #Measurement_too_high := FALSE; END_IF;
IF #Measurement_temperature <= “Lower_limit”
THEN #Measurement_too_low:= TRUE; END_IF;
IF #Measurement_temperature >= “Lower_limit” + “Hysteresis”
THEN #Measurement_too_low:= FALSE; END_IF;
Fig. 10.14 Example of arithmetic expressions with SCL
Table 10.7 Math functions with SCL
Operation Function Operation Function
SIN COS TAN
Calculate sine Calculate cosine Calculate tangent
ASIN ACOS ATAN
Calculate arcsine Calculate arccosine Calculate arctangent SQR
SQRT
Generate square Extract square root
EXP LN
Generate exponential function to base e Generate Napierian logarithm (to base e)
#Reactive_power := #Voltage * #Current * SIN(#phi);
#Volume := 4/3 * “pi” * #Radius**3;
#Solution_1 := -#p/2 + SQRT(SQR(#p/2) - #q);
#Mean_value := (#Motor[1].power + #Motor[2].power)/2;
Fig. 10.15 Example of math functions with SCL
10.5.5 Conversion functions
The conversion functions convert the data formats of tags and expressions.
A detailed description of the conversion functions – including a description of implicit conversion – is provided in Chapter 13.6 “Conversion functions” on page 531. Table 10.8 shows the (explicit) conversion functions available with SCL.
The program elements catalog contains the conversion functions under Basic instructions > Basic instructions > Conversion operations.
When inserting the CONVERT function, the data types involved in the conversion are selected in a dialog box (Fig. 10.16).
Table 10.9 shows the data type conversions possible with SCL. There are also the conversions WORD_TO_BLOCK_DB and BLOCK_DB_TO_WORD. If the permissible numerical range is left during a conversion, the ENO tag is set to FALSE, and the result of the conversion is invalid.
Fig. 10.17 shows an example of the conversion functions. A measured value present in data format INT is first expanded to the data format DINT and then converted in- to the 7-decade BCD format.
Table 10.8 Conversion functions with SCL Operation Operand Function
CONVERT Data type conversion
ROUND TRUNC
Data type conversion from REAL to DINT with rounding to the next integer Data type conversion from REAL to DINT without rounding
Fig. 10.16
Selection of data types with the CONVERT function
#Measurement_display :=
DINT_TO_BCD_DWORD(INT_TO_DINT(#Measurement_temperature));
Fig. 10.17 Example of conversion functions with SCL
10.5.6 Shift functions
A shift function shifts the content of a tag bit-by-bit to the left or right.
A detailed description of the shift functions is provided in Chapter 13.7 “Shift func- tions” on page 544. Table 10.10 shows the shift functions available with SCL.
The program elements catalog contains the shift functions under Basic instruc- tions > Shift and rotate.
In Fig. 10.18, the three decades of two numbers present in BCD format of a SIMATIC counter are joined. The more significant component #Quantity_high is shifted to the Table 10.9 Data type conversion with SCL
to
from BOOL BYTE WORD DWORD INT DINT REAL TIME S5TIME DT TOD DATE CHAR STRING BCD16 BCD32
BOOL X X X K K
BYTE X X X K K X
WORD X X X X K
DWORD X X X K X XX
INT K K X K X X X S B
DINT K K K X X X X X X S B
REAL XX X X R S
TIME X T
S5TIME T
DT T1) T T
TOD X
DATE X
CHAR X X X
STRING S S S X
BCD16 B
BCD32 B
Data type conversion is possible: X With CONVERT
S With S_CONV (integrated in CONVERT)
T With T_CONV (integrated in CONVERT) 1) Conversion of DT to day of week
B The BCD data types are handled like WORD or DWORD (example: BCD16 = WORD_BCD)
R ROUND, TRUNC
K By combination of two conversions
(example: BOOL_TO_INT = WORD_TO_INT(BOOL_TO_WORD) XX The content of the tag (the value) is not converted!
left by three decades (12 bits) and linked to the less significant component #Quan- tity_low according to an OR logic operation. In the result #Quantity_display, the two times three decades are then present as a 6-decade BCD number.
10.5.7 Word logic operations, logic expression
The word logic operations apply the binary operations AND, OR, and XOR to each bit of a word or doubleword.
A detailed description of the word logic operations is provided in Chapter 13.8.1 “Word logic operations” on page 549. Table 10.11 shows the word logic operations available with SCL.
Fig. 10.19 shows how you can program 32 edge evaluations simultaneously for ris- ing and falling edges. The message bits are collected in a doubleword Messages, which is present in data block “Data.SCL”. The edge trigger flags Messages_EM are also present in this data block. If the two doublewords are linked by an XOR logic operation, the result is a doubleword in which each set bit represents a different assignment of Messages and Messages_EM, in other words: the associated message bit has changed. In order to obtain the positive signal edges, the changes are linked to the messages by an AND logic operation. The bit is set for a rising signal edge wherever the message has a “1” and the change a “1”. This corresponds to the pulse flag of the edge evaluation. If you do the same with the negated message bits – Table 10.10 Shift functions with SCL
Operation Data types IN Data type N Function
SHR (IN, N) SHL (IN, N)
BYTE, WORD, DWORD BYTE, WORD, DWORD
INT, DINT INT, DINT
Shift to right Shift to left ROR (IN, N)
ROL (IN, N)
BYTE, WORD, DWORD BYTE, WORD, DWORD
INT, DINT INT, DINT
Rotate to right Rotate to left
#Quantity_display :=
SHL(IN := #Quantity_high, N := 12) OR #Quantity_low;
Fig. 10.18 Example of shift functions with SCL
Table 10.11 Word logic operations with SCL
Operation Operand data types Function
AND, &
OR XOR
BOOL, BYTE, WORD, DWORD BOOL, BYTE, WORD, DWORD BOOL, BYTE, WORD, DWORD
AND logic operation OR logic operation Exclusive OR logic operation
NOT BOOL, BYTE, WORD, DWORD Negation
the message bits with signal state “0” are now “1” – you obtain the pulse flags for a falling edge. At the end it is only necessary for the edge trigger flags to track the messages.