126
5-17 Subroutines
Subroutines can be used for one of two different purposes: either to separate
off sections of large control tasks so that they can be handled as smaller
ones and to enable you to reuse a given set of instructions at different places
within one program or as a part of different programs. When the main pro-
gram calls a subroutine, control is transferred to the subroutine and the in-
structions in the subroutine are executed. The instructions within a subrou-
tine are written in the same way as main program code. When all the instruc-
tions in the subroutine have been executed, control returns to the main pro-
gram.
5-17-1 SUBROUTINE DEFINE and SUBROUTINE RETURN
SBN(92)/RET(93)
N: Subroutine number
# (00 to 15)
Ladder Symbol Definer Data Areas
SBN(92) N
Ladder Symbol
RET(93)
Each subroutine number can be used in SBN(92) once only, i.e., up to 16
subroutines may be programmed.
SBN(92) is used to mark the beginning of a subroutine program; RET(93) is
used to mark the end. Each subroutine is identified with a subroutine number,
N, that is programmed as a definer for SBN(92). This same subroutine num-
ber is used in any SBS(91) that calls the subroutine (see next section). No
subroutine number is required with RET(93).
All subroutines must be programmed at the end of the main program. When
one or more subroutines have been programmed, the main program will be
executed up to the first SBN(92) before returning to address 0000 for the
next cycle. Subroutines will not be executed unless called by SBS(91).
END(01) must be placed at the end of the last subroutine program, i.e., after
the last RET(93). It is not required at any other point in the program.
Flags There are no flags directly affected by these instructions.
5-17-2 SUBROUTINE ENTRY – SBS(91)
N: Subroutine number
# (00 to 15)
Ladder Symbol Definer Data Areas
SBS(91) N
To execute a subroutine, it must be called from the main by programming
SBS(91) with the number of the desired subroutine. When SBS(91) is exe-
cuted (i.e., when the execution condition for it is ON), the instructions be-
tween the SBN(92) with the same subroutine number and the first RET(93)
after it are executed before execution returns to the instruction following the
SBS(91) that made the call.
Limitations
Description
Description
Subroutines Section 5-17