Sie sind auf Seite 1von 7

Instructions with REAL Numbers

y=LN(x)

? ?

sin? cos? tan? ...?

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.1

Information and Training Center Knowledge for Automation

Contents

Page 2 3 4 5 6 7

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

Training Center
for Automation and Drives

Page 1

ST-7PRO2 Instructions with REAL Numbers

REAL Number Representation in SIMATIC S7


Representation format of a REAL number (IEEE FP 32 bit binary format):
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
2-1 S Exponent: e = 8 Bit Mantissa: f (23 Bits) 2-23

Representation of a normalized REAL number:


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

Example:
S =0 e = 1000 0101 = 133 f = 1010 0000... = 0.5 + 0.125 R = +1.625 x 2(133-127) = 1.625 x 64 = 104.0

Value range of normalized REAL numbers:


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

3.402 823 x 10+38

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.2

Information and Training Center Knowledge for Automation

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 8bit 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). Designation Not float.pt.no. Overflow Normalized No. Denormaliz. No. Zero Value e 255 255 1.. 254 0 0 Mantissa f <>0 0 any <>0 0 Value [qNaN] CC1 CC0 OV OS 1 1 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 1 1 1 1 -

Range Limits

>(2-2-23) 2127 1 <(-2+ 2-23) 2127 0 (1.f) 2e-127 (-1.f) 2e-127 (0.f) 2-126 (- 0.f) 2-126 +0 1 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

Training Center
for Automation and Drives

Page 2

ST-7PRO2 Instructions with REAL Numbers

Basic Instructions with REAL Numbers


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

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

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

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

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.3

Information and Training Center Knowledge for Automation

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, if 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. With an unauthorized calculation, that is, when one of the two input values is an 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 - infinite Multiplication: Multiplication of 0 by infinite Division: Division of infinite by infinite or 0 by 0. The result of the division of valid REAL numbers by 0 is, depending on the numbers sign, + infinite or - infinite. The hexadecimal number D#16#FFFF FFFF represents, for example, an invalid REAL number.

Unauthorized REAL Numbers

Note

Training Center
for Automation and Drives

Page 3

ST-7PRO2 Instructions with REAL Numbers

Extended Mathematical Functions


Mathematical Functions:
SQR SQRT EXP LN Form the square Calculate the square root Exponential function for Base e Natural Logarithm (e=2.718282)

Example:
L SQR T MD10 MD30 // Load REAL number // Calculate the square // Transfer result into MD30

(STL)

EN MD10 IN

SQR

ENO OUT MD30

(LAD)

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.4

Information and Training Center Knowledge for Automation

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. Dependent 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. The function SQR squares the contents of ACCU1. The function SQRT 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. The function EXP calculates the power from the Base e (=2.71828) and the value (eACCU1) found in ACCU1. The function LN 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

SQR SQRT

EXP

LN

Training Center
for Automation and Drives

Page 4

ST-7PRO2 Instructions with REAL Numbers

Trigonometric Functions and Their Inverse Functions


Trigonometric functions:
SIN COS TAN Sine Cosine Tangent Angle

Arc functions:
ASIN ACOS ATAN Arc sine Arc cosine Arc tangent

Example:
L SIN T MD10 MD30
SIN

// Load REAL number // Calculate the sine // Transfer result into MD30

(STL)

EN MD22 IN

ENO OUT

(LAD)
MD30

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.5

Information and Training Center Knowledge for Automation

Trigonometric Functions

The trigonometric functions expect an angle in radian measure as a REAL 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). 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 range ASIN -1 to +1 - pi/2 to + pi/2 ACOS -1 to +1 0 to pi ATAN entire range - pi/2 to + pi/2 With 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".

Arc Functions

Training Center
for Automation and Drives

Page 5

ST-7PRO2 Instructions with REAL Numbers

Other Instructions with REAL Numbers


Conversion instructions from REAL to DINT:
RND+ RNDRND TRUNC with rounding off to the next larger DINT number with rounding off to the next smaller DINT number with rounding off to the next integer integer component

Conversion instructions from DINT to REAL:


DTR conversion with rounding off

Other instructions from REAL to REAL:


ABS NEGR forming the absolute amount negation of a REAL number

Example:
L RND+ T MD10 MD30 // Load REAL number // Convert to next larger DINT number // Transfer result into MD30

(STL)

EN MD22 IN

RND+ ENO OUT

(LAD)
MD30

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.6

Information and Training Center Knowledge for Automation

Overview

The conversion functions convert the data type of the value found in ACCU1 into another data type and store the result in ACCU. The contents of the other ACCUs 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 formats permissible range or it doesnt 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. The instruction RND+ converts the contents of ACCU1 as REAL number into an integer (DINT), that is larger than or equal to the number to be converted. The instruction RND- converts the contents of ACCU1 as REAL number into an integer (DINT), that is smaller than or equal to the number to be converted. The instruction RND 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. The instruction TRUNC returns the integer component of the number to be converted; the fraction component is cut off. The instruction DTR converts a number from the DINT format into the REAL number format. Since a 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 representable number. The instruction ABS 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). The instruction NEGR 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.

RND+ RNDRND

TRUNC DTR

ABS NEGR

Training Center
for Automation and Drives

Page 6

ST-7PRO2 Instructions with REAL Numbers

Exercsie 3.1: Calculating the Distance


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

Y1

P1

X1

X2 (X2 - X1)2 + (Y2 - Y1)2

Function: FC31 with D=

SIMATIC S7
Siemens AG 1999. All rights reserved.

Date: File:

08.04.2007 PRO2_03E.7

Information and Training Center Knowledge for Automation

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. 1. Create an FC31 with the above functionality 2. Call FC31 in OB1 and connect the input and output parameters as follows: X1 = MD0, Y1 = MD4 X2 = MD8, Y2 = MD12 RET_VAL = MD16 3. Download the program into the S7-CPU. 4. Test FC31 with the help of "Monitor/Modify Variable". Create a run-time optimal version of FC31 for S7-400, that can manage without the use of temporary variables.

What to Do

Additional Task

Training Center
for Automation and Drives

Page 7

ST-7PRO2 Instructions with REAL Numbers

Das könnte Ihnen auch gefallen