Programming digital functions with SCL

Một phần của tài liệu Hans berger automating with SIMATIC s7 1200 configuring programming (Trang 299 - 306)

The digital functions process digital values mainly with the data types for fixed- point and floating-point numbers.

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 (e.g. word logic operations). The functions for data type conversion (conver- sion functions) and for shifting and rotating are available for manipulating just one value.

9.5.1 Transfer function, value assignment of a digital tag

The “simple” transfer function corresponds with SCL to the value assignment.

Both sides of the value assignment must have a “tolerable” data type that is either automatically adapted (see Chapter 4.3.2 “Implicit data type conversion” on page Fig. 9.11 Example of a counter function with SCL

Fig. 9.12 Examples of transfer and conversion functions with SCL

93) or must be converted using a program with conversion functions (see Chapter 11.6 “Conversion functions (Conversion of data type)” on page 376).

A detailed description of the transfer functions is provided in Chapter 11.1 “Trans- fer functions” on page 356. Fig. 9.12 shows some examples of value assignments:

The constant value 123 is assigned to the #var_int tag. The #Quantity tag imports the value of the #var_int tag. A value in INT data format can be assigned to a tag with data type DINT or REAL, where SCL “implicitly” converts the data type. If no implicit data type conversion is possible, a conversion function must be programmed.

9.5.2 Conversion functions

The conversion functions convert the data formats of tags and expressions. SCL can

“automatically” convert data types if no information can be lost during conversion (see also Chapter 4.3.2 “Implicit data type conversion” on page 93). In all other cases, the (explicit) conversion functions must be used.

A detailed description of the conversion functions is provided in Chapter 11.6 “Con- version functions (Conversion of data type)” on page 376. Table 9.3 shows the con- version functions available with SCL. When inserting the CONVERT function, the data types involved in the conversion are selected in a dialog box.

The program elements catalog contains the conversion functions under Basic instructions > Converters.

Fig. 9.12 on page 298 shows examples of simple and “nested” conversion functions where the #Measurement_display tag has the data type DWORD.

Table 9.4 shows the data type conversions possible with SCL. There are also the con- versions WORD_TO_BLOCK_DB and BLOCK_DB_TO_WORD. If the permissible numerical range is left during a conversion, the block-internal ENO tag is set to FALSE, and the result of the conversion is invalid.

Table 9.3 Conversion functions with SCL

Operation Function Data type selection with CONVERT

CONVERT Data type conversion

ROUND Data type conversion from REAL to DINT with rounding to the next integer CEIL Data type conversion from REAL to DINT

with rounding to the next higher integer FLOOR Data type conversion from REAL to DINT with rounding to the next lower integer TRUNC Data type conversion from REAL to DINT

without rounding

9.5.3 Comparison functions

A comparison function is implemented with a comparison expression in SCL.

A comparison expression compares the values of two tags or expressions using a comparison operator. The comparison 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, otherwise FALSE. The comparison function is described in Chapter 11.2 “Comparison functions” on page 364. Table 9.5 shows the comparison operators available with SCL.

Table 9.4 Data type conversion with SCL to

from BOOL BYTE WORD DWORD USINT UINT UDINT SINT INT DINT REAL LREAL TIME TOD DATE DTL CHAR STRING BCD16 BCD32

BOOL x x x W W W W W W

BYTE x x x x x x x x x T T T x

WORD x x x x x x x x x T T T x

DWORD x x x x x x x x x x T T

USINT x x x x x x x x x x x x S

UINT x x x x x x x x x x x T T T x S

UDINT x x x x x x x x x x x T x S

SINT x x x x x x x x x x x T x S

INT x x x x x x x x x x x T x S x

DINT x x x x x x x x x x x T T T x S x

REAL x x x x x x x x S

LREAL x x x x x x x x S

TIME T T T

TOD T T T

DATE T T T T T T T T

DTL T T

CHAR x x x x x x x x x S

STRING S S S S S S S S S

BCD16 x

BCD32 x

Data type conversion is possible:X With CONVERT S with S_CONV T with T_CONV

W By programmed conversions (example: BOOL_TO_INT)

In Fig. 9.13 on page 302, a measured value #Measurement_temperature is com- pared with an upper and a lower limit. The tags #Measurement_too_high and

#Measurement_too_low are controlled depending on the comparison result.

Resetting occurs at a distance from the upper or lower limit (#Hysteresis tag).

9.5.4 Arithmetic functions

The arithmetic functions implement the basic arithmetic operations with the data formats for fixed-point and floating-point numbers as well as with time values. SCL uses arithmetic expressions with an arithmetic operator for this purpose.

Table 9.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

USINT, UINT, UDINT, SINT, INT, DINT, REAL, LREAL,

CHAR, STRING,

TIME, DATE, TIME_OF_DAY (TOD), DTL

=

<>

Compare for equal Compare for unequal

BOOL, BYTE, WORD, DWORD

Table 9.6 Arithmetic operators with SCL Data type *)

Operator Description 1st operand 2nd operand Result

** Power Fixed point, floating point Fixed point, floating point Fixed point, floating point

* Multiplication Fixed point, floating point Fixed point, floating point Fixed point, floating point

TIME Fixed point TIME

/ Division Fixed point, floating point Fixed point, floating point Fixed point, floating point

DIV Integer

division

Fixed point, floating point Fixed point, floating point Fixed point, floating point

TIME Fixed point TIME

MOD Division with remainder as result

Fixed point Fixed point Fixed point

+ Addition Fixed point, floating point Fixed point, floating point Fixed point, floating point

TIME TIME TIME

TOD TIME TOD

DTL TIME DTL

– Subtraction Fixed point, floating point Fixed point, floating point Fixed point, floating point

TIME TIME TIME

TOD TIME TOD

DATE DATE TIME

TOD TOD TIME

DTL TIME DTL

DTL DTL TIME

*) Fixed point = USINT, UINT, UDINT, SINT, INT, DINT; floating point = REAL, LREAL

You can find a detailed description of these arithmetic functions in Chapters 11.3

“Arithmetic functions for numerical values” on page 366 and 11.4 “Arithmetic func- tions for time values” on page 369. Table 9.6 shows the arithmetic operators avail- able with SCL with the allowed data types.

The tags or expressions linked to an arithmetic function can have fixed-point, float- ing-point or time value data types. If different data types can be matched in the con- text of implicit data type conversion (see Chapter 4.3.2 “Implicit data type conver- sion” on page 93), a programmed function for (explicit) data type conversion can be dispensed with. The result of an arithmetic function has the “most powerful” of the involved data types. If, for example, you link an INT or DINT tag with a REAL tag, the result is of data type REAL.

In the case of a division, the second operand must not be zero.

In Fig. 9.13, the upper limit of a measured value is monitored under the header

“Comparison expressions”. A hysteresis is introduced to ensure that the #Measure- ment_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.

There are several calculation examples under the heading “Arithmetic expres- sions”: the calculation of reactive electric power, the volume calculation of a ball, one of the solutions of a quadratic equation, and the formation of an arithmetic mean value.

Fig. 9.13 Examples of comparison expressions, arithmetic and logical expressions

9.5.5 Mathematical functions

The mathematical functions comprise the trigonometric functions, exponential functions, and logarithmic functions, and deliver a result in floating-point data for- mat. The input tag can have any data type that can be converted into the data type of the output tag using the implicit data type conversion. A detailed description of these math functions is provided in Chapter 11.5 “Mathematical functions” on page 372. Table 9.7 shows the mathematical functions available with SCL.

Under the heading “Arithmetic expressions”, Fig. 9.13 provides several examples of the mathematical functions SIN, SQR, and SQRT.

9.5.6 Word logic operations

The word logic operations apply the binary operations AND, OR, and XOR to each bit of a byte, word, or doubleword. SCL uses logic expressions for this.

A detailed description of the word logic operations is provided in Chapter 11.8.2

“Word logic operations (AND, OR, XOR)” on page 392. Table 9.8 shows the word logic operations available with SCL.

Fig. 9.13 on page 302 shows how you can program 32 edge evaluations simulta- neously for rising and falling edges. The message bits are collected in a double- word Messages, which is present in data block “Data.90”. 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 rep- resents a different assignment of Messages and Messages_EM, in other words: the associated message bit has changed.

Table 9.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)

Table 9.8 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

In order to obtain the positive signal edges, the changes are linked to the mes- sages 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 – the mes- sage 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 mes- sages.

9.5.7 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 11.7 “Shift functions” on page 389. Table 9.9 shows the shift functions available with SCL.

The program elements catalog contains the shift functions under Basic instructions

> Shift and rotate.

In Fig. 9.14 the tags #Quantity_high and #Quantity_low are available as positive numbers in BCD16 format. They contain three digit decades and in the highest decade the sign in each case (here the value zero). The digits of both numbers are combined into a single number in the #Quantity_display tag. In addition, the

#Quantity_high tag is shifted 12 bits to the left (corresponds to three decades) and linked according to an OR operation with the #Quantity_low tag.

Table 9.9 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

Fig. 9.14 Example of the shift function with SCL

Một phần của tài liệu Hans berger automating with SIMATIC s7 1200 configuring programming (Trang 299 - 306)

Tải bản đầy đủ (PDF)

(577 trang)