7

Click here to load reader

PRO2_03e Instructions With REAL Numbers

Embed Size (px)

Citation preview

Page 1: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Contents Page

REAL Number Representations in SIMATIC S7 .............................................................. 2Basic Instructions with REAL Numbers .......................................................................................... 3Extended Mathematical Functions ......................................................................................... 4Trigonometric Functions and Their Inverse Functions .................................................................... 5Other Instructions with REAL Numbers ......................................................................................... 6Exercise 3.1: Calculating the Distance ........................................................................................ 7

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

Instructions with REAL Numbers

y=LN(x)

sin?cos?

tan?...?

??

Page 2: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

REAL Number Representation in SIMATIC S7

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

0 1 0 0 0 0 1 0 1 1 0 1 0 0 0 0

31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16

Exponent: e = 8 BitSMantissa: f (23 Bits)

Representation format of a REAL number (IEEE FP 32 bit binary format):

Representation of a normalized REAL number:

S x (1.f) x 2 (e-127) S = Sign bit, (0 corresponds to +, 1 corresponds to -)f = 23 bit Mantissa with MSB = 2-1 and LSB =2-23

e = binary integer exponent (0 < e < 255)

Value range of normalized REAL numbers:

- 3.402 823 x 10+38 ... -1.175 494 x 10-38, 0, 1.175 494 x 10-38 ... 3.402 823 x 10+38

2-1 2-23

Example:S = 0e = 1000 0101 = 133f = 1010 0000... = 0.5 + 0.125

R = +1.625 x 2 (133-127) = 1.625 x 64 = 104.0

REAL Number REAL (floating point) numbers enable the implementation of complex, mathematical calculations for process control and closed-loop process control. A REAL data type variable consists internally of three components: the sign, the 8-bit exponent of Base 2 and the 23-bit Mantissa. The sign can take the values "0" (positive) or "1" (negative). The exponent is increased by one constant (Bias, +127) and stored, so that it has a value range of 0 to 255. The Mantissa represents the fractional part. The integer part of the Mantissa is not stored, since it is always either 1 (for normalized floating point) or 0 (for denormalized floating point).

Range Limits Designation Value e Mantissa f Value CC1 CC0 OV OS

Not float.pt.no. 255 <>0 [qNaN] 1 1 1 1

Overflow 255 0 >(2-2-23) 2127 1 0 1 1<(-2+ 2-23) 2127 0 1 1 1

Normalized No. 1.. 254 any (1.f) 2e-127 1 0 0 -(-1.f) 2e-127 0 1 0 -

Denormaliz. No. 0 <>0 (0.f) 2-126 0 0 1 1(- 0.f) 2-126 0 0 1 1

Zero 0 0 +0 0 0 0 -

Note The CPUs calculate with the full accuracy of the floating-point numbers. The display on the PG can deviate from the exact representation, due to round up errors in conversion. REAL numbers are rounded up to the sixth decimal place

Page 3: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Overview The functions +R, -R, *R, /R interpret the values found in ACCU1 and ACCU2 as REAL data type numbers. They carry out the programmed logic operation (+R, -R, *R and /R) and save the result in ACCU1.After the calculation is carried out, the status bits CC0 and CC1 indicate whether the result is negative (CC1=0, CC0=1), zero (CC1=0; CC0=0) or positive (CC1=1, CC0=0).The status bits OV and OS signal the leaving of the permitted numbers range.

Unauthorized With an unauthorized calculation, that is, when one of the two input values is anREAL Numbers invalid REAL number, then the result in ACCU1 is also an invalid REAL number.

Invalid REAL numbers are also stored as a result in ACCU1 if you try to process unauthorized values with the following instructions:Addition: Addition of + infinite and - infinite.Subtraction: Subtraction of + infinite and + infinite or - infinite

and - infiniteMultiplication: Multiplication of 0 by infiniteDivision: Division of infinite by infinite or 0 by 0.The result of the division of valid REAL numbers by 0 is, depending on the number s sign, + infinite or - infinite.

Note The hexadecimal number DW#16#FFFF FFFF represents an invalid REAL number, for example.

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

Basic Instructions with REAL Numbers

REAL Addition:L MD10 // Load 1st. REAL numberL MD20 // Load 2nd. REAL number+R // Addition of REAL numbers (MD10 + MD20)T MD30 // Transfer result into MD30

REAL Subtraction:L MD10 // Load 1st. REAL numberL MD20 // Load 2nd. REAL number-R // Subtraction of REAL numbers (MD10 - MD20)T MD30 // Transfer result into MD30

REAL Multiplication:L MD10 // Load 1st. REAL numberL MD20 // Load 2nd. REAL number*R // Multiplication of REAL numbers (MD10 * MD20)T MD30 // Transfer result into MD30

REAL Division:L MD10 // Load 1st. REAL numberL MD20 // Load 2nd. REAL number/R // Division of REAL numbers (MD10 / MD20)T MD30 // Transfer result into MD30

Page 4: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Overview The mathematical functions take the number in ACCU1 as the input value for the function to be carried out and store the result in ACCU1. Mathematical functions only change the contents of ACCU1. The contents of ACCU2, or ACCU3 and AKKU4 for S7-400, remain unchanged.Depending on the function result, the mathematical functions set the status bits CC0, CC1, OV and OS.If there is an invalid REAL number in ACCU1 before the function is carried out, then the mathematical function returns an invalid REAL number and sets the status bits accordingly.

SQR The SQR function squares the contents of ACCU1.

SQRT The SQRT function calculates the square root from the value in ACCU1. If there is a value smaller than zero in ACCU1, SQRT sets the status bits CC0, CC1, OV and OS to "1" and returns an invalid REAL number.If -0 (minus zero) is in ACCU1, -0 is also returned.

EXP The EXP function calculates the power from the Base e (=2.71828) and the value (eACCU1) found in ACCU1.

LN The LN function calculates the natural logarithm to Base e from the number found in ACCU1. If there is a value smaller than or equal to zero in ACCU1, LN sets the status bits CC0, CC1, OV and OS to "1" and returns an invalid REAL number.The natural logarithm is the reverse function to the exponential function:If: y= ex

then: x = ln y

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

Extended Mathematical Functions

Mathematical Functions:SQR Form the squareSQRT Calculate the square root

EXP Exponential function for Base eLN Natural Logarithm

(e=2.718282)

Example:

L MD10 // Load REAL numberSQR // Calculate the squareT MD30 // Transfer result into MD30

SQREN ENO

IN OUTMD10 MD30

(STL)

(LAD)

Page 5: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Trigonometric The trigonometric functions expect an angle in radian measure as a REALFunctions number in ACCU1. For the angle input ( 00 ... 3600), you must, if necessary,

perform a conversion to degree measure (0 ... 2 pi, with pi=3.141593).During function execution, for values smaller than 0 or larger than 2pi, a multiple of 2pi is automatically added or subtracted until the value lies between 0 and 2pi (automatic modulo-2pi calculation).

Arc Functions The arc functions are the inverse functions of the respective trigonometric functions. They expect a REAL number in a specific value range in ACCU1 and return an angle in radian measure:Function Permitted definition range Value rangeASIN -1 to +1 - pi/2 to + pi/2ACOS -1 to +1 0 to piATAN entire range - pi/2 to + pi/2With an overrange of the permitted definition range, the arc functions return an invalid REAL number and set the status bits CC0, CC1, OV and OS to "1".

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

Trigonometric Functions and Their Inverse Functions

Angle

Trigonometric functions:SIN SineCOS CosineTAN Tangent

Arc functions:ASIN Arc sineACOS Arc cosineATAN Arc tangent

Example:

L MD10 // Load REAL numberSIN // Calculate the sineT MD30 // Transfer result into MD30

SINEN ENO

IN OUTMD22 MD30

(STL)

(LAD)

Page 6: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Overview The conversion functions convert the data type of the value found in ACCU1 into another data type and store the result in ACCU1. The contents of the otherACCUs remain unchanged.If, in one of the instructions (RND+, RND-, RND or TRUNC), the value found in ACCU1 is larger or smaller than that for a number in the DINT format s permissible range or it doesn t correspond to a number in the REAL format, the instruction sets the status bits OV and OS to "1". A conversion then does not take place.

RND+ The RND+ instruction converts the contents of ACCU1 as REAL number into an integer (DINT), that is larger than or equal to the number to be converted.

RND- The RND- instruction converts the contents of ACCU1 as REAL number into an integer (DINT), that is smaller than or equal to the number to be converted.

RND The RND instruction converts the contents of ACCU1 as REAL number into the next possible integer (DINT). If the result lies exactly between an even and an uneven number, the even number is returned.

TRUNC The TRUNC instruction returns the integer component of the number to be converted; the fraction component is cut off.

DTR The DTR instruction converts a number from the DINT format into the REAL number format. Since a large number in the DINT format is more exact than a number in the REAL format, it is possible that during conversion a rounding takes place to the next valid number.

ABS The ABS instruction forms the absolute value from the REAL number found in ACCU1, that is, the sign (bit 31) is set to "0" (even for an invalid REAL number).

NEGR The NEGR instruction negates the REAL number in ACCU1. That is, the sign (bit 31) is inverted (even for an invalid REAL number). The instructions DTR, ABS and NEGR do not affect the status bits.

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

Other Instructions with REAL Numbers

Conversion instructions from REAL to DINT:RND+ with rounding off to the next larger DINT numberRND- with rounding off to the next smaller DINT number RND with rounding off to the next integerTRUNC integer component

Conversion instructions from DINT to REAL:DTR conversion with rounding off

Other instructions from REAL to REAL:ABS forming the absolute amountNEGR negation of a REAL number

Example:L MD10 // Load REAL numberRND+ // Convert to next larger DINT numberT MD30 // Transfer result into MD30

RND+EN ENO

IN OUTMD22 MD30

(STL)

(LAD)

Page 7: PRO2_03e Instructions With REAL Numbers

ST-7PRO2Instructions with REAL NumbersPage

SITRAIN Training forAutomation and Drives

Goal of the Exercise The application of mathematical functions for calculating the distance between two points.

Task Create an FC31 with the following functionality:FC31 expects the coordinates (X1, Y1) or (X2, Y2) of the two points P1 and P2 in the input parameters.FC31 returns the distance of both points in the output parameter RET_VAL.FC31 is to be installable in the S7-300 system as well as the S7-400 system. It may not use global CPU addresses for any possible saving of intermediate results.

What to Do 1. Create an FC31 with the above functionality2. Call FC31 in OB1 and connect the input and output parameters as follows:

X1 = MD0, Y1 = MD4X2 = MD8, Y2 = MD12RET_VAL = MD16

3. Download the program into the S7-CPU.4. Test FC31 with the help of "Monitor/Modify Variable".

Additional Task Create a run-time optimal version of FC31 for S7-400 that can manage without the use of temporary variables.

Date: 10.10.2011File: PRO2_03E.

SIMATIC S7Siemens AG 2001. All rights reserved.

SITRAIN Training forAutomation and Drives

Exercsie 3.1: Calculating the Distance

Example: Calculating the distance D between two points in a right-angled coordinate system

P1

P2

X1 X2 X

Y

Y2

Y1

Function: FC31 with D= (X2 - X1)2 + (Y2 - Y1)2

D