Sei sulla pagina 1di 11

Indirect Addressing in S7 PLCs

This application note provides an overview of indirect addressing methods and


structures available in the Statement List (STL) language for S7 PLCs. It describes
the types of addressing available, including immediate addressing, direct addressing,
and indirect addressing, with primary focus on memory indirect addressing. The
document also includes an archived STEP7library of functions that use
these addressing methods. The functions in the library demonstrate the use
of memory indirect addressing to read from and write to arrays of bytes,
integers, and real numbers.
Structure of an STL Statement
The STL Statement is the smallest piece of a user program. It defines a
specific task for the
CPU to perform, such as loading a variable into a register, or performing a
math operation. The structure of an STL statement is shown below, and the
individual elements of the statement are further described.
A) Label the label is an optional name assigned to a statement used for
Jump or Loop operations
B) Operation the operation is the task to be performed such as Load,
Transfer, Add.
C) Operand the operand is the variable (or data) to be manipulated by this
statement. The operand may be an actual value, or it may be a memory address
that contains the data, or it may be a pointer to a memory address that
contains the data
D) Comment Optional text field for documenting the program code the
following sections will describe various methods of addressing the Operand.
Label Operation Operand Comment Loop:
L MB 12 // Load accu1

Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory


Indirect Addressing
Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory
Indirect AddressingCopyright 2000 by SIEMENS page 2 / 9 Revision 4
SIMATIC NET customers have free use of the application tips. These tips are only a
general approach to using SIMATIC NET with various applications. Your specific
application may be different. It is your responsibility to use SIMATIC NET properly in
your applications.
Types of Addressing in S7

There are several basic methods of addressing that can be used to identify
an operand in an S7 programmable controller. These basic types are as
follows:
A) Immediate Addressing Immediate addressing refers to the use of a
number value as an operand. A Statement List example would be to load a
constant into the accumulator.
There is no operand address in this operation, the operand is contained in the
Statement.
L 14
// Load accumulator with the integer value 14B) Direct addressing - Direct
addressing refers to the use of an actual memory or object address to specify
the location of the operand. A statement list example would be loading the value
contained in Marker word 46 into the accumulator. The operand address is explicitly
called out in the operational.
L MW46
// Load the accumulator with the contents of MW46C) Indirect addressing
Indirect addressing refers to the use of a pointer to indicate where
the operand address can be found. A statement list example would be loading
theaccumulator with the contents of a memory word, whose address is contained in
apointer. The operand address is not computed until runtime. Braces are used to
signifyan indirect address to the operanda.
L MW [POINTER_LOCATION]
// Load accumulator from a MW whoseaddress is stored as a POINTER in
POINTER_LOCATION
Two Types of Indirect Addressing
Indirect addressing in S7 PLCs can then be further subdivided into two types: A)
Memory-Indirect addressing

In memoryindirect addressing, the pointer to the


operand address is stored in a memory location. The pointers may
be stored in
double word locations in Marker (MD), Local (LD), Global Data Block
(DBD) or
Instance Data Block (DID) memory. Memory indirect addressing is convenient to
usewhen you have multiple pointers that you are using in the program
B)Register Indirect addressing
In register indirect addressing, an address registers

Is used for computing the actual address of the data. The pointer to the
actual data
Address is stored in one of the two Address Registers of the PLC, and
that gets
combined with an offset value to compute the variable address. Register
indirect
addressing is the form best supported by STL since you can directly display
thecontents of the address register to see where the pointer is aimed.
Register indirect addressing also executes faster than Memory Indirect
addressing. Register indirect
Addressing can also be used for programming situations where you dont
explicitly Know the Memory Area of the actual data until runtime. Register
Indirect Addressing is covered more completely in a
Companion application note Pointers and Indirect
Addressing in S7 PLCs, Volume 2

Register Indirect Addressing

Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory


Indirect Addressing
Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory
Indirect AddressingCopyright 2000 by SIEMENS page 6 / 9 Revision 4
SIMATIC NET customers have free use of the application tips. These tips are only a
general approach to using SIMATIC NET with various applications. Your specific
application may be different. It is your responsibility to use SIMATIC NET properly in
your applications.
Pointer P# Valve_12 creates the same pointer value as P# M12.3. A pointer data
type can then be used as immediate operands in STL statements as shown in the
table below.
Preparing Pointers for use as Indirect Addresses
Area Internal Pointer Operation
Description
L P# 123.0
This operation instructs the CPU to load the
Accumulator with an area internal pointer for 123.0

After execution of this statement, the accumulator will contain the following 32
bit value: 00000000 00000000 00000011 11011000
L P# 123.0
T MD4
This twoStep operation loads an area internal pointer
Into the accumulator, and then transfers the pointer value into MD4, which can
now be used for an indirect addressing operation. MD4 now contains00000000
00000000 00000011 11011000
L P# 123.0T MD4.L MB [MD4]
This three-step operation loads a pointer of value 123.0into the accumulator and
stores the pointer into the double word location MD4. The third instruction will
Then use the pointer found in MD4 to point to location
M123.0 and will load the contents of MB123 into the into the accumulator
Area Crossing Pointer Operation
Description
L P#M123.0
This operation instructs the CPU to load the
accumulator with an area crossing pointer forM123.0 After execution of
this statement
the accumulator will contain the following 32 bit value:10000011 00000000 00000011
11011000
L P#M123.0T MD4
This two-step operation loads a pointer to M123.0 into the accumulator, and then
transfers the pointer value into MD4, which can now be used for an indirect
addressing operation. MD4 now contains10000011 00000000 00000011 11011000
L P#M123.0T MD4.L B [MD4]
This three-step operation loads a pointer to M123.0 into the accumulator and stores
the pointer into the double word location MD4. The third instruction will then use
the pointer found in MD4 to point to location M123.0
and will load the contents of MB123 into the into the accumulator
Working With Memory Indirect Addressing - Instructions
Memory Indirect Addressing can be used in the following Statement List instructions:

Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory


Indirect Addressing
Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory
Indirect AddressingCopyright 2000 by SIEMENS page 7 / 9 Revision 4

SIMATIC NET customers have free use of the application tips. These tips are only a
general approach to using SIMATIC NET with various applications. Your specific
application may be different. It is your responsibility to use SIMATIC NET properly in
your applications.
Operation STL Mnemonics Sample
Binary Logic Operations A, AN, O, ON, X, XN A M [MD12]O T [QW12]*Binary
Memory Functions=, S, R, FP, FN = Q [MD66]R C [MW88]* Accumulator
Functions L, T T QW [DID44]L T [MW12]*Block Functions(Integer memory
indirect specification of the objects))OPN (opening a DB)S, R, CU, CD
(counter actions)SP, SE, SD, SS, SF (start timers)CALL, UC, CC (calling
code
blocks)OPN DB [mw12]*CU C [DBW56]*SE T [MW12]*UC FC [MW12]**
Integer Memory Pointers for Block Objects
Working with Memory Indirect Addressing - Indexing
Area pointers may be used to perform indexed reading and writing from a
table or array of data items. This can easily be accomplished by creating an
area pointer that contains the starting
Address of the table of data, and then adding a pointer increment to the
table pointer that is equal to the desired index into the table of data. The example
below shows a routine for
Accessing an element in the table of bytes starting at location MB20
// This assumes an integer symbol named index exists that
//contains the index (from 0 to 179) into the table
L index // load a variable index into the table
SLD 3 /
/ Shift left 3 bits to create an index pointer
L P#20.0 // load the pointer to the start of table
+D // Add index pointer to start of table pointer
T MD100 // Store the indexed pointer in MD100L MB [MD100] // read
the table element into the accumulator Location Table element
MB20 Element 0MB21 Element 1MB22 Element 2MB24 Element 3

MB200 Element 180


Sample STEP7 Function Library
The object inserted below is an archived library of STEP7 functions that demonstrate
the use of memory indirect addressing for performing some commonly used

data handling operations regarding arrays. You may copy this archived object into
a folder on your hard drive and then retrieve the archive using STEP7 program
manager. Once the Archive is retrieved, it will add a new library, called
Array Functions to your STEP7 package. The functions in this library are

Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory


Indirect Addressing
Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory
Indirect AddressingCopyright 2000 by SIEMENS page 8 / 9 Revision 4
SIMATIC NET customers have free use of the application tips. These tips are only a
general approach to using SIMATIC NET with various applications. Your specific
application may be different. It is your responsibility to use SIMATIC NET properly in
your applications.
Then available for your use in developing PLC programs. You may also open this
library in the Program manager
and transfer the STEP7 program called Simple Arrays into an S7 PLC for
testing.
The Simple Arrays program includes a set of six Functions (FC10 through
FC15) for reading
and writing to arrays of BYTE, INTEGER, or FLOATING POINT data. The
program also includes a main OB1, three data blocks containing test arrays, and
three VAT tables for exercising these functions. The library of six arrayhandling functions will be available for your use in the instruction catalogs
shown in the figure below.
Related Information
Further detailed information regarding the S7 statement List programming
language may be
found in a textbook by Hans Berger called Automating with STEP7 in STL and SCL
ISBN
Number 3-89578-140-1

Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory


Indirect Addressing
Pointers and Indirect Addressing in S7 PLCs, Volume 1- Memory
Indirect AddressingCopyright 2000 by SIEMENS page 9 / 9 Revision 4
SIMATIC NET customers have free use of the application tips. These tips are only a
general approach to using SIMATIC NET with various applications. Your specific
application may be different. It is your responsibility to use SIMATIC NET properly in
your applications.
Hardware and Software Considerations General Notes
The SIMATIC Application Tips are provided to give users of Siemens Simatic
products some

Indication as to how, from the view of programming technique, certain tasks


can be solved. These instructions do not purport to cover all details or variations in
equipment, nor do they provide for every possible contingency. Use of the
Simatic Application Tips is free. Siemens reserves the right to make
changes in specifications shown herein or make improvements at any time
without notice or obligation. It does not relieve the user of responsibility to use
sound practices in application, installation, operation, and maintenance of the
equipment purchased. Should a conflict arise between the general information
contained in this publication, the contents of drawings or supplementary
material, or both, the latter shall takeprecedence.Siemens is not liable, for
whatever legal reason, for damages or personal injury resulting from the
use of the application tips. All rights reserved.
Any form of duplication or distribution, including excerpts, is only permitted
with express authorization by SIEMENS.

Potrebbero piacerti anche