You are on page 1of 15

S7-200 Instruction Set Chapter 6

Program Control Instructions


Conditional End
The Conditional End instruction (END) terminates the current scan
based upon the condition of the preceding logic. You can use the
Conditional End instruction in the main program, but you cannot
use it in either subroutines or interrupt routines.

Stop
The Stop instruction (STOP) terminates the execution of your
program by causing a transition of the S7-200 CPU from RUN to
STOP mode.
If the Stop instruction is executed in an interrupt routine, the
interrupt routine is terminated immediately, and all pending
interrupts are ignored. Remaining actions in the current scan cycle
are completed, including execution of the main user program, and
the transition from RUN to STOP mode is made at the end of the
current scan.
6
Watchdog Reset
The Watchdog Reset instruction (WDR) retriggers the system watchdog timer of the S7-200 CPU to
extend the time that the scan is allowed to take without getting a watchdog error.

You should use the Watchdog Reset instruction carefully. If you use looping instructions either to prevent
scan completion or to delay excessively the completion of the scan, the following processes are inhibited
until the scan cycle is completed:

- Communications (except Freeport Mode)


- I/O updating (except Immediate I/O)
- Force updating
- SM bit updating (SM0, SM5 to SM29 are not updated)
- Run-time diagnostics
- 10-ms and 100-ms timers will not properly accumulate time for scans exceeding 25 seconds
- STOP instruction, when used in an interrupt routine
- Expansion modules with discrete outputs also include a watchdog timer that turns off outputs if the
module is not written by the S7-200. Use an immediate write to each expansion module with
discrete outputs to keep the correct outputs on during extended scan times. Refer to the example
that follows this description.

Tip
If you expect your scan time to exceed 500 ms, or if you expect a burst of interrupt activity that could
prevent returning to the main scan for more than 500 ms, you should use the Watchdog Reset
instruction to retrigger the watchdog timer.
Each time you use the Watchdog Reset instruction, you should also use an immediate write to one
output byte (QB) in each discrete expansion module to reset each expansion module watchdog.
If you use the Watchdog Reset instruction to allow the execution of a program that requires a long
scan time, changing the mode switch to the STOP position causes the S7-200 to transition to STOP
mode within 1.4 seconds.

169
S7-200 Programmable Controller System Manual

Example: Stop, End, and Watchdog Reset Instructions


Network 1 //When an I/O error is detected:
//Force the transition to STOP mode.
LD SM5.0
STOP

Network 2 //When M5.6 is on, allow the scan to be extended:


//1. Retrigger the Watchdog Reset for the S7-200.
//2. Retrigger the watchdog for the first output module.
LD M5.6
WDR
BIW QB2, QB2

Network 3 //When I0.0 is on, terminate the current scan.


LD I0.0
END

170
S7-200 Instruction Set Chapter 6

For-- Next Loop Instructions


Use the For (FOR) and Next (NEXT) instructions to delineate a
loop that is repeated for the specified count. Each For instruction
requires a Next instruction. You can nest For--Next loops (place a
For--Next loop within a For--Next loop) to a depth of eight.
The For instruction executes the instructions between the For and
the Next instructions. You specify the index value or current loop
count INDX, the starting value INIT, and the ending value FINAL.
The Next instruction marks the end of the FOR loop.
Error conditions that set ENO = 0
H 0006 (indirect address)

If you enable the For--Next loop, it continues the looping process


until it finishes the iterations, unless you change the final value
from within the loop itself. You can change the values while the

6
For--Next loop is in the looping process. When the loop is enabled
again, it copies the initial value into the index value (current loop
number).
The For--Next instruction resets itself the next time it is enabled.
For example, given an INIT value of 1 and a FINAL value of 10, the instructions between the For
instruction and the Next instruction are executed 10 times with the INDX value being incremented:
1, 2, 3, ...10.

If the starting value is greater than the final value, the loop is not executed. After each execution of the
instructions between the For instruction and the Next instruction, the INDX value is incremented and the
result is compared to the final value. If the INDX is greater than the final value, the loop is terminated.

If the top of stack is 1 when your program enters the For--Next loop, then the top of stack will be 1 when
your program exits the For--Next loop.

Table 6-56 Valid Operands for the For and Next Instructions
Inputs/Outputs Data Types Operands
INDX INT IW, QW, VW, MW, SMW, SW, T, C, LW, AC, *VD, *LD, *AC
INIT, FINAL INT VW, IW, QW, MW, SMW, SW, T, C, LW, AC, AIW, *VD, *LD, *AC, Constant

171
S7-200 Programmable Controller System Manual

Example: For-- Next Loop Instructions


Network 1 //When I2.0 comes on, the outside loop
1 //(arrow 1) is executed 100 times
LD I2.0
FOR VW100, +1, +100

Network 2 //The inside loop (arrow 2) is executed twice


//for each execution of the outside loop
//when I2.1 is on.
2 LD I2.1
FOR VW225, +1, +2

Network 3 //End of Loop 2.


NEXT

Network 4 //End of Loop 1 .


NEXT
6

172
S7-200 Instruction Set Chapter 6

Jump Instructions
The Jump to Label instruction (JMP) performs a branch to the
specified label N within the program.
The Label instruction (LBL) marks the location of the jump
destination N.
You can use the Jump instruction in the main program, in
subroutines, or in interrupt routines. The Jump and its
corresponding Label instruction must always be located within the
same segment of code (either the main program, a subroutine, or
an interrupt routine).
You cannot jump from the main program to a label in either a
subroutine or an interrupt routine. Likewise, you cannot jump from
a subroutine or interrupt routine to a label outside that subroutine
or interrupt routine.
You can use a Jump instruction within an SCR segment, but the
corresponding Label instruction must be located within the same
SCR segment.
6
Table 6-57 Valid Operands for the Jump Instructions
Inputs/Outputs Data Types Operands
N WORD Constant (0 to 255)

Example: Jump to Label Instruction


Network 1 //If the retentive data has not been lost, Jump to LBL4
LDN SM0.2
JMP 4
Network 2
LBL 4

173
S7-200 Programmable Controller System Manual

Sequence Control Relay (SCR) Instructions


SCR instructions provide you with a simple yet powerful state
control programming technique that fits naturally into a LAD, FBD,
or STL program.
Whenever your application consists of a sequence of operations
that must be performed repetitively, SCRs can be used to structure
your program so that it corresponds directly to your application. As
a result, you can program and debug your application more quickly
and easily.
The Load SCR instruction (LSCR) loads the SCR and logic stacks
with the value of the S bit referenced by the instruction N.
The SCR segment is energized or de-energized by the resulting
value of the SCR stack. The value of the SCR stack is copied to
the top of the logic stack so that boxes or output coils can be tied
directly to the left power rail without an intervening contact.

6 Restrictions
When using SCRs, be aware of the following restrictions:
- You cannot use the same S bit in more than one routine. For
example, if you use S0.1 in the main program, do not use it
in a subroutine.
- You cannot jump into or out of an SCR segment; however,
you can use Jump and Label instructions to jump around
SCR segments or to jump within an SCR segment.
- You cannot use the END instruction in an SCR segment.

Table 6-58 Valid Operands for the Sequence Control Relay Instructions
Inputs/Outputs Data Types Operands
S_bit BOOL S

174
S7-200 Instruction Set Chapter 6

Figure 6-32 shows the S stack and the logic stack and the effect of executing the Load SCR instruction.
The following is true of Sequence Control Relay instructions:

- The Load SCR instruction (LSCR) marks the beginning of an SCR segment, and the SCR End
instruction (SCRE) marks the end of an SCR segment. All logic between the Load SCR and the
SCR End instructions are dependent upon the value of the S stack for its execution. Logic
between the SCR End and the next Load SCR instruction is not dependent on the value of the S
stack.
- The SCR Transition instruction (SCRT) Load the value of Sx.y onto the SCR and logic stacks.
provides the means to transfer control S stack ivS Sx.y
from an active SCR segment to another S bit
SCR segment. iv0 Sx.y
iv1 iv1
Execution of the SCR Transition Logic stack iv2 iv2
instruction when it has power flow will iv3 iv3
reset the S bit of the currently active iv4 iv4
segment and will set the S bit of the iv5 iv5
referenced segment. Resetting the S bit
iv6 iv6
of the active segment does not affect the
S stack at the time the SCR Transition Before
iv7
After
iv7 6
iv8 iv8
instruction executes. Consequently, the
SCR segment remains energized until it
is exited. Figure 6-32 Effect of LSCR on the Logic Stack

- The Conditional SCR End instruction (CSCRE) provides a means to exit an active SCR segment
without executing the instructions between the Conditional SCR End and the SCR End
instructions. The Conditional SCR End instruction does not affect any S bit nor does it affect the S
stack.
In the following example, the first scan bit SM0.1 sets S0.1, which will be the active State 1 on the first
scan. After a 2-second delay, T37 causes a transition to State 2. This transition deactivates the State 1
SCR (S0.1) segment and activates the State 2 SCR (S0.2) segment.

175
S7-200 Programmable Controller System Manual

Example: Sequence Control Relay Instruction


Network 1 //On the first scan enable State 1.
LD SM0.1
S S0.1, 1

Network 2 //Beginning of State 1 control region.


LSCR S0.1

Network 3 //Control the signals for Street 1:


//1. Set: Turn on the red light.
//2. Reset: Turn off the yellow and green lights.
//3. Start a 2--second timer.
LD SM0.0
S Q0.4, 1
R Q0.5, 2
TON T37, +20

6 Network 4
LD T37
//After a 2 second delay, transition to State 2.

SCRT S0.2

Network 5 //End of SCR region for State 1.


SCRE

Network 6 //Beginning of State 2 control region.


LSCR S0.2

Network 7 //Control the signals for Street 2:


//1. Set: Turn on the green light.
//2. Start a 25--second timer.
LD SM0.0
S Q0.2, 1
TON T38, +250

Network 8 //After a 25 second delay, transition to State 3.


LD T38
SCRT S0.3

Network 9 //End of SCR region for State 2.


SCRE

176
S7-200 Instruction Set Chapter 6

Divergence Control
In many applications, a single stream of sequential states must be split into two or more different
streams. When a control stream diverges into multiple streams, all outgoing streams must be activated
simultaneously. This is shown in Figure 6-33.

State L

Transition Condition

State M State N

Figure 6-33 Divergence of a Control Stream

6
The divergence of control streams can be implemented in an SCR program by using multiple SCRT
instructions enabled by the same transition condition, as shown in the following example.

Example: Divergence of Control Streams


Network 1 //Beginning of State L control region.
LSCR S3.4

Network 2
LD M2.3
A I2.1
SCRT S3.5 //Transition to State M
SCRT S6.5 //Transition to State N

Network 3 //End of the State region for State L.


SCRE

Convergence Control
A situation similar to divergence control arises when two or more streams of sequential states must be
merged into a single stream. When multiple streams merge into a single stream, they are said to
converge. When streams converge, all incoming streams must be complete before the next state is
executed. Figure 6-34 depicts the convergence of two control streams.

The convergence of control streams can be implemented in an SCR program by making the transition
from state L to state L’ and by making the transition from state M to state M’. When both SCR bits
representing L’ and M’ are true, state N can the enabled as shown in the following example.

177
S7-200 Programmable Controller System Manual

State L State M

Transition Condition

State N

Figure 6-34 Convergence of a Control Stream

Example: Convergence of Control Streams


Network 1 //Beginning of State L control region

6 LSCR S3.4

Network 2 //Transition to State L’


LD V100.5
SCRT S3.5

Network 3 //End of SCR region for State L


SCRE

Network 4 //Beginning of State M control region


LSCR S6.4

Network 5 //Transition to State M’


LD C50
SCRT S6.5

Network 6 //End of SCR region for State M


SCRE

Network 7 //When both State L’ and State M’ are activated:


//1. Enable State N (S5.0)
//2. Reset State L’ (S3.5)
//3. Reset State M’ (S6.5)
LD S3.5
A S6.5
S S5.0, 1
R S3.5, 1
R S6.5, 1

178
S7-200 Instruction Set Chapter 6

In other situations, a control stream might be directed into one of several possible control streams,
depending upon which transition condition comes true first. Such a situation is depicted in Figure 6-35,
which shows an equivalent SCR program.

State L

Transition Condition Transition Condition

State M State N

Figure 6-35 Divergence of a Control Stream, Depending on the Transition Condition

Example: Conditional Transitions 6


Network 1 //Beginning of State L control region
LSCR S3.4

Network 2 //Transition to State M


LD M2.3
SCRT S3.5

Network 3 //Transition to State N


LD I3.3
SCRT S6.5

Network 4 //End of SCR region for State L


SCRE

179
S7-200 Programmable Controller System Manual

Subroutine Instructions
The Call Subroutine instruction (CALL) transfers control to the
subroutine SBR_N. You can use a Call Subroutine instruction with
or without parameters. After the subroutine completes its
execution, control returns to the instruction that follows the Call
Subroutine.
The Conditional Return from Subroutine instruction (CRET)
terminates the subroutine based upon the preceding logic.
To add a subroutine, select the Edit > Insert > Subroutine menu
command.
Error conditions that set ENO = 0
H 0008 (maximum subroutine nesting exceeded)
H 0006 (indirect address)

6
From the main program, you can nest subroutines (place a
subroutine call within a subroutine) to a depth of eight. From an
interrupt routine, you cannot nest subroutines.

A subroutine call cannot be placed in any subroutine called from an interrupt routine. Recursion (a
subroutine that calls itself) is not prohibited, but you should use caution when using recursion with
subroutines.

Table 6-75 Valid Operands for the Subroutine Instruction


Inputs/Outputs Data Types Operands
SBR_N WORD Constant for CPU 221, CPU 222, CPU 224, and CPU 226: 0 to 63
for CPU 226XM: 0 to 127
IN BOOL V, I, Q, M, SM, S, T, C, L, Power Flow
BYTE VB, IB, QB, MB, SMB, SB, LB, AC, *VD, *LD, *AC1, constant
WORD, INT VW, T, C, IW, QW, MW, SMW, SW, LW, AC, AIW, *VD, *LD, *AC1, constant
DWORD, DINT VD, ID, QD, MD, SMD, SD, LD, AC, HC, *VD, *LD, *AC1,
&VB, &IB, &QB, &MB, &T, &C, &SB, constant
IN/OUT BOOL V, I, Q, M, SM2, S, T, C, L
BYTE VB, IB, QB, MB, SMB2, SB, LB, AC, *VD, *LD, *AC1
WORD, INT VW, T, C, IW, QW, MW, SMW2, SW, LW, AC, *VD, *LD, *AC1
DWORD, DINT VD, ID, QD, MD, SMD2, SD, LD, AC, *VD, *LD, *AC1
OUT BOOL V, I, Q, M, SM2, S, T, C, L
BYTE VB, IB, QB, MB, SMB2, SB, LB, AC, *VD, *LD, *AC1
WORD, INT VW, T, C, IW, QW, MW, SMW2, SW, LW, AC, AQW, *VD, *LD, *AC1
DWORD, DINT VD, ID, QD, MD, SMD2, SD, LD, AC, *VD, *LD, *AC1

1 Must be offset 1 or above


2 Must be offset 30 or above

Tip
STEP 7--Micro/WIN automatically adds an unconditional return from each subroutine.

When a subroutine is called, the entire logic stack is saved, the top of stack is set to one, all other stack
locations are set to zero, and control is transferred to the called subroutine. When this subroutine is
completed, the stack is restored with the values saved at the point of call, and control is returned to the
calling routine.

Accumulators are common to subroutines and the calling routine. No save or restore operation is
performed on accumulators due to subroutine use.

204
S7-200 Instruction Set Chapter 6

Calling a Subroutine With Parameters


Subroutines can contain passed parameters. The parameters are defined in the local variable table of
the subroutine. The parameters must have a symbol name (maximum of 23 characters), a variable type,
and a data type. Sixteen parameters can be passed to or from a subroutine.

The variable type field in the local variable table defines whether the variable is passed into the
subroutine (IN), passed into and out of the subroutine (IN_OUT), or passed out of the subroutine (OUT).
Table 6-76 describes the parameter types for a subroutine. To add a parameter entry, place the cursor
on the variable type field of the type (IN, IN_OUT, or OUT) that you want to add. Click the right mouse
button to get a menu of options. Select the Insert option and then the Row Below option. Another
parameter entry of the selected type appears below the current entry.

Table 6-76 Parameter Types for a Subroutine


Parameter Description
IN Parameters are passed into the subroutine. If the parameter is a direct address (such as VB10), the

6
value at the specified location is passed into the subroutine. If the parameter is an indirect address
(such as *AC1), the value at the location pointed to is passed into the subroutine. If the parameter is
a data constant (16#1234) or an address (&VB100), the constant or address value is passed into the
subroutine.
IN_OUT The value at the specified parameter location is passed into the subroutine, and the result value from
the subroutine is returned to the same location. Constants (such as 16#1234) and addresses (such
as &VB100) are not allowed for input/output parameters.
OUT The result value from the subroutine is returned to the specified parameter location. Constants (such
as 16#1234) and addresses (such as &VB100) are not allowed as output parameters.
TEMP Any local memory that is not used for passed parameters can be used for temporary storage within
the subroutine.

As shown in Figure 6-38, the data type field in the local variable table defines the size and format of the
parameter. The parameter types are listed below:

- BOOL: This data type is used for single


bit inputs and outputs. IN3 in the following
example is a Boolean input.
- BYTE, WORD, DWORD: These data
types identify an unsigned input or output
parameter of 1, 2, or 4 bytes,
respectively.
- INT, DINT: These data types identify
signed input or output parameters of 2 or
Figure 6-38 Local Variable Table
4 bytes, respectively.

- REAL: This data type identifies a single precision (4 byte) IEEE floating-point value.
- Power Flow: Boolean power flow is allowed only for bit (Boolean) inputs. This declaration tells
STEP 7-Micro/WIN that this input parameter is the result of power flow based on a combination of
bit logic instructions. Boolean power flow inputs must appear first in the local variable table before
any other type input. Only input parameters are allowed to be used this way. The enable input
(EN) and the IN1 inputs in the following example use Boolean logic.

205
S7-200 Programmable Controller System Manual

Example: Subroutine Call


There are two STL examples provided. The first set of STL instructions can be displayed only in the STL editor since
the BOOL parameters used as power flow inputs are not saved to L memory.
The second set of STL instructions can be displayed also in the LAD and FBD editors because L memory is used to
save the state of the BOOL inputs parameters that are shown as power flow inputs in LAD and FBD.
STL only:
Network 1
LD I0.0
CALL SBR_0, I0.1, VB10, I1.0, &VB100, *AC1, VD200

To display correctly in LAD and FBD:


Network 1
LD I0.0
= L60.0
LD I0.1
= L63.7
LD L60.0
6 CALL SBR_0, L63.7, VB10, I1.0, &VB100, *AC1, VD200

Address parameters such as IN4 (&VB100) are passed into a subroutine as a DWORD (unsigned
double word) value. The type of a constant parameter must be specified for the parameter in the calling
routine with a constant descriptor in front of the constant value. For example, to pass an unsigned
double word constant with a value of 12,345 as a parameter, the constant parameter must be specified
as DW#12345. If the constant describer is omitted from the parameter, the constant can be assumed to
be a different type.

There are no automatic data type conversions performed on the input or output parameters. For
example, if the local variable table specifies that a parameter has the data type REAL, and in the calling
routine a double word (DWORD) is specified for that parameter, the value in the subroutine will be a
double word.

When values are passed to a subroutine, they are placed into the local memory of the subroutine. The
left-most column of the local variable table shows the local memory address for each passed parameter.
Input parameter values are copied to the subroutine’s local memory when the subroutine is called.
Output parameter values are copied from the subroutine’s local memory to the specified output
parameter addresses when the subroutine execution is complete.

The data element size and type are represented in the coding of the parameters. Assignment of
parameter values to local memory in the subroutine is as follows:

- Parameter values are assigned to local memory in the order specified by the call subroutine
instruction with parameters starting at L.0.
- One to eight consecutive bit parameter values are assigned to a single byte starting with Lx.0 and
continuing to Lx.7.
- Byte, word, and double word values are assigned to local memory on byte boundaries (LBx, LWx,
or LDx).

206
S7-200 Instruction Set Chapter 6

In the Call Subroutine instruction with parameters, parameters must be arranged in order with input
parameters first, followed by input/output parameters, and then followed by output parameters.

If you are programming in STL, the format of the CALL instruction is:

CALL subroutine number, parameter 1, parameter 2, ... , parameter

Example: Subroutine and Return from Subroutine Instructions


M Network 1 //On the first scan, call subroutine 0 for initialization.
A LD SM0.1
I CALL SBR_0
N

S Network 1 //You can use a conditional return to leave


B //the subroutine before the last network.
R LD M14.3
0 CRET

Network 2 //This network will be skipped if M14.3 is on. 6


LD SM0.0
MOVB 10, VB0

207

You might also like