Professional Documents
Culture Documents
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:
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
170
S7-200 Instruction Set Chapter 6
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
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)
173
S7-200 Programmable Controller System Manual
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
6 Network 4
LD T37
//After a 2 second delay, transition to State 2.
SCRT S0.2
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
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.
Network 2
LD M2.3
A I2.1
SCRT S3.5 //Transition to State M
SCRT S6.5 //Transition to State N
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
6 LSCR S3.4
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
State M State N
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.
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
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.
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:
- 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
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:
207