B02 Analog e
B02 Analog e
MODULE B2
Analog Value Processing
This document has been written by Siemens AG for training purposes for the project entitled "Siemens
Automation Cooperates with Education (SCE)".
Siemens AG accepts no responsibility for the correctness of the contents.
Transmission, use or reproduction of this document is only permitted within public training and educational
facilities. Exceptions require the prior written approval of Siemens AG (Mr. Michael Knust
[email protected]).
Offenders will be liable for damages. All rights, including the right to translate the document, are reserved,
particularly if a patent is granted or utility model is registered.
We would like to thank the following: Michael Dziallas Engineering, the teachers at vocational schools, and all
others who helped to prepare this document.
PAGE
1. Preface .................................................................................................................. 4
4.1. Computing with Fixed Point Numbers (INT and DINT) ......................................... 9
4.2. Computing with Floating Point Numbers (REAL) .................................................. 10
4.3. Data Type Conversion Operations ........................................................................ 11
Information
Programming
Sample Exercise
Notes
1. PREFACE
In terms of its contents, Module B2 is part of the teaching unit entitled "Additional Functions of
STEP 7 Programming'.
Basics of
STEP 7 Programming
2 to 3 days Module A
Additional Functions of
Plant Simulation with
STEP 7 Programming
2 to 3 days Module B SIMIT SCE
1 to 2 days Module G
Learning Objective:
In module B2, the reader learns how analog values are input, processed, and output in a SMATIC
S7. The following steps are discussed:
Analog signals
Data types in STEP 7
Mathematical operations
Converting data types in STEP 7
Inputting and normalizing analog values
Normalizing and reading out analog values
Prerequisites:
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
1 PC, operating system Windows 2000 Professional starting with SP4/XP Professional starting
with SP1/Server 2003 with 600MHz and 512RAM, free hard disk storage 650 to 900 MB, MS
Internet Explorer 6.0
2 Software STEP7 V 5.4
3 MPI interface for the PC (for example, PC adapter USB)
4 PLC SIMATIC S7-300 with at least one analog input/output module to which, on one analog
value input, a potentiometer or another analog signal transmitter is connected. In addition, an
analog value display has to be connected to at least one analog output.
Sample configuration:
- Power supply: PS 307 2A
- CPU: CPU 314
- Digital inputs: DI 16xDC24V
- Digital outputs: DO 16xDC24V/0.5 A
- Analog inputs/outputs: AI 4/AO2 x 8 bits
2 STEP7
1 PC
3 PC Adapter USB
4 SIMATIC S7-300
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
2. ANALOG SIGNALS
In contrast to a binary signal that can only have the two signal states “Voltage present +24V“ and
"Voltage not present 0V“, analog signals can have many values within a certain range. A typical
example for an analog transmitter is a potentiometer. Depending on the position of the rotary button,
any resistance can be set here up to the maximum value.
By using a measuring transducer, these variables are converted into electrical voltages, currents or
resistances. If, for example, a certain speed is to be recorded, the speed range of
500 ... 1500 r/min can be converted into a voltage range of 0 ... +10V by using a measuring
transducer. For a speed of 865 r/min, the transducer would read out a voltage of + 3.65 V.
These electrical voltages, currents or resistances are then connected to an analog module that
digitalizes this signal.
Note: Some analog modules can process different types of signals. This has to be set in the
hardware configuration. Please refer to the notes in the manuals.
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
If analog variables are processed with a PLC, the voltage, current or resistance value that was
entered has to be converted into digital information. This conversion is called analog/digital
conversion. It means that the voltage of 3.65V, for example, is stored as information in a series of
binary digits. The more binary digits are used for digital representation, the more detailed the
resolution. If, for example, you would have only 1 bit available for the voltage range 0 ... +10V, only
two values could be provided: whether the measured voltage is in the range of 0 .. +5V or in the
range +5V ... +10V. With 2 bits, however, the range can be divided into 4 individual ranges; that is 0
... 2.5 / 2.5 ... 5 / 5 ... 7.5 / 7.5 ... 10V. Usually, A/D converters in control engineering convert with 8
or 11 bits.
8 bits provide 256 individual ranges, and 11 bits a resolution of 2048 individual ranges.
0A/0V 20mA/10V
10V: 2048 = 0,0048828
Voltage differences of
11 Bits <5mV can be recognized
0 2048
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
The SIMATIC S7 has a large number of different data types that can represent different numerical
formats. Below is a list of the elementary data types.
Note: The data types ’INT’ and ’REAL’ are very important to analog value processing, since
input analog values are present as integers in the format ’INT’, and for exact continued processing,
only floating point numbers ’REAL’ can be used because of rounding errors regarding ’INT’.
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
4. MATHEMATICAL OPERATIONS
4.1 CALCULATING WITH THE FIXED POINT NUMBERS (INT AND DINT)
With fixed point numbers, the basic mathematical operations Addition, Subtraction, Multiplication
and Division are performed. However, digits after the decimal point are not taken into account which
causes rounding errors regarding division.
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
Floating point numbers can be used for performing a large number of mathematical operations. For
this format, the decimal positions are also taken into account.
Operation Function
+R Adds up floating point numbers (32 bits, IEEE-FP) in ACCU 1 and 2 and stores the 32 bit
result in ACCU 1.
-R Subtracts the floating point number (32 bits, IEEE-FP) in ACCU 1 from the floating point number (32
bits, IEEE-FP) in ACCU 2 and stores the 32 bit result in ACCU 1.
*R Multiplies the floating point number (32 bits, IEEE-FP) in ACCU 1 with the floating point number (32
bits, IEEE- FP) in ACCU 2 and stores the 32 bit result in ACCU 1.
/R Divides the floating point number (32 bits, IEEE-FP) in ACCU 2 by the floating point number
(32 bits, IEEE-FP) in ACCU 1. The 32 bit result is stored in ACCU 1.
ABS Generates the absolute value of a floating number (32 bits, IEEE-FP) in ACCU 1. The result is
stored in ACCU 1. The operation is performed without taking the status bits into account, or
influencing them.
SQRT Calculates the square root of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the
32 bit result in ACCU 1.
SQR Calculates the square of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the 32
bit result in ACCU 1. ÁÁÁÁÁ
LN Calculates the natural logarithm of the floating point number (32 bits, IEEE-FP) in ACCU 1 and
stores the 32 bit result in ACCU 1.
EXP Calculates the exponential value of the floating point number (32 bits, IEEE-FP) to base e and
stores the 32 bit result in ACCU 1.
SIN Calculates the sine of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the 32 bit
result in ACCU 1. ÁÁÁÁÁ
COS Calculates the cosine of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the 32
bit result in ACCU 1.
TAN Calculates the tangent of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the 32
bit result in ACCU 1
ASIN Calculates the arc sine of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the 32
bit result in ACCU 1. ÁÁÁÁÁ
ACOS Calculates the arc cosine of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the
32 bit result in ACCU 1. ÁÁÁÁÁ
ATAN Calculates the arc tangent of the floating point number (32 bits, IEEE-FP) in ACCU 1 and stores the
32 bit result in ACCU 1.
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
Since numbers are often not available in the number formats required for continued processing, they
have to be adapted by using conversion operations.
Operation Function
BTI Converting a BCD into an integer (16 bits). This operation converts a binary coded decimal value
in ACCU 1 into an integer (16 bits).
BTD Converting a BCD into an integer (32 bits). This operation converts a binary coded decimal value in
ACCU 1 into an integer (32 bits).
ITB Converting an integer (16 bits) into a BCD. This operation converts an integer (16 bits) in the less
significant word of ACCU 1 into a binary coded decimal value.
ITD Converting an integer (16 bits) into an integer (32 bits). This operation converts an integer (16 bits) in
the less significant word of ACCU 1 into an integer (32 bits).
DTB Converting an integer (32 bits) into a BCD. This operation converts an integer (32 bits) in ACCU 1
into a binary coded decimal value.
DTR Converting an integer (32 bits) into a floating point number (32 bits, IEEE-FP). This operation
converts an integer (32 bits) in ACCU 1 into a floating point number (32 bits, IEEE-FP).
RND Rounding off into an integer. This operation rounds off the converted number into the next integer. If
the fraction of the converted number is exactly between an even and an uneven result, the operation
selects the even result.
RND+ Rounding off to the next higher integer. This operation rounds off the converted number to the lowest
integer that is larger or equal to the converted floating point number.
RND- Rounding off to the next lower integer. This operation rounds off the converted number to the highest
integer that is lower or equal to the converted floating point number.
TRUNC Rounding off with truncation. This operation converts the integer part of the floating point number
Note: Regarding analog value processing, the analog value is present in ’INT’ and -for
exact continued processing- is to be converted into a floating point number ’REAL’
because of the rounding errors in the case of ’INT’. Since this can’t be done directly,
first a conversion has to be made with ’ITD’ to ’DINT’ and then with ’DTR’ to ’REAL’.
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
Analog values are entered in the PLC or read out as word information. These words are accessed
with the following instructions:
To each analog value ("channel“), an IO input word or an IO output word is assigned. The format is
’INT’, an integer.
Addressing the input and output words is based on the module’s start address. If the analog module
is inserted in Slot 4, it has the default start address 256. The start address for each additional analog
module increases by 16 per slot.
This default start address can be looked up in the hardware configuration table under Detail View.
Thus, the address of the first analog input for an analog module would be PEW 288 on Slot 6, the
address of the second analog input PEW 290, the address of the first analog output therefore PAW
288 etc..
The analog value transformation for further processing in the PLC (digitalized) is the same for analog
inputs and analog outputs.
For module SM334, with 4 analog inputs and 2 analog outputs, and with the analog value ranges
0 to 10V as well as 0 to 20mA, the digitalized value ranges look like this:
Often, these digitalized values have to be normalized through corresponding further processing in
the PLC.
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
If the analog input value is a digitalized value, it usually has to be normalized before it can be
processed further in the PLC.
Likewise, the analog output to the IO output word is usually made only after the output value is
normalized.
Exercise:
In the example below, a value from 0 to 10V is entered with an analog module SM334 on Slot 6
(PEW288). Initially, the value is an INTEGER (16 bits) and is to be normalized to 100 to 1000 in the
floating point format, and stored in the flag double word MD10.
Solution in STL:
L PEW 288 //Enter analog value; 0 to 10 V corresponds to 0 to 27648 integer (16 bits)
ITD //Convert the value of the integer (16 bits) to integer (32 bits)
DTR // Convert the value of the integer (32 bits) into floating point number
L 2.7648e+4 //
/R //Division with floating point number 27648
L 9.000e+2 //
*R // Multiplication with floating point number 900 (1000 - 100)
L 1.000e+2 //
+R // Addition with floating point number 100 (offset)
T MD10 //Normalized value 100 to 1000 in floating point format
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values
If there is a normalized value and it is to be read out on an analog output module, it has to be
suitably normalized.
In STEP7 program, mathematical operations are used for normalizing. This is done in the data type
REAL, to keep the rounding errors to a minimum. Only then is this value rounded off to an integer
value. The digits after the decimal point are lost.
Exercise:
In the example below, there is a value from 100 to 1000 -stored in the floating point format on the
flag double word MD20- which is, normalized from 0 to 10V, to be read out with an analog module
SM334 (PAW288).
Solution in STL:
Preface Analog Signals Data Types Math Operations Entering/Reading Out Analog Values