Page 210 - Programmable Logic Controllers, Fifth Edition - Mobile version
P. 210

JSR
                                   JUMP-TO-SUBROUTINE                      the RET instruction may be conditional if this rung
                                                                           precedes the end of the subroutine. In this way, the
                                   SBR file number            U:3
                                                                           processor omits the balance of a subroutine only if its
                                                                           rung condition is true.
                                   SBR
                                   SUBROUTINE
                                                                           The jump to subroutine (JSR), subroutine (SBR), and
                                   RET                                   return (RET) instructions are used to direct the controller
                                   RETURN                                to execute a subroutine file. Figure 9-11 shows a materials
                                                                         conveyor system with a flashing pilot light as a subrou-
                   Figure 9-10  Allen-Bradley subroutine-related instructions.  tine. The operation of the program can be summarized as
                                                                         follows:

                     When a subroutine is called from the main pro-        •  If the weight on the conveyor exceeds a preset
                   gram, the program is able to escape from the main         value, the solenoid is de-energized and pilot light
                   program and  go to a program  subroutine to perform       PL1will begin flashing.
                   certain functions and then return to the main program.   •  When the weight sensor switch closes, the JSR is
                   In situations in which a machine has a portion of its     activated and directs the processor scan to jump to
                   cycle that must be repeated several times during one      the subroutine U:3.
                   machine cycle, the subroutine can save a great deal of   •  The subroutine program is scanned and pilot light
                   duplicate programming. The sequence of rungs could        PL1 begins flashing.
                   be programmed one time into a subroutine and just       •  When the weight sensor switch opens, the proces-
                   called when needed.                                       sor will no longer scan the subroutine area and pilot
                     The subroutine concept is the same for all program-     light PL1 will return to its normal on state.
                   mable controllers, but the method used to call and return
                   from a subroutine uses different commands, depending on   The Allen-Bradley SLC 500 controller main program
                   the PLC manufacturer. The subroutine-related instructions   is located in program file 2 whereas subroutines are as-
                   used in the Allen-Bradley PLCs shown in Figure 9-10 are   signed to program file numbers 3 to 255. Each subroutine
                   the jump to subroutine (JSR) output instruction, the sub-  must be programmed in its own program file by assigning
                   routine (SBR) input instruction, and the return (RET) out-  it a unique file number. Figure 9-12 illustrates the proce-
                   put instruction.                                      dure for setting up a subroutine and can be summarized
                     The  subroutine instructions  can be  summarized  as   as follows:
                   follows:
                                                                           •  Note each ladder location where a subroutine should
                     Jump to Subroutine (JSR)—The JSR instruction            be called.
                     redirects logic execution from the current ladder     •  Create a subroutine file for each location.
                     file to the specific subroutine file. When rung condi-  Each subroutine file should begin with an SBR
                     tions are true for this output instruction, it causes   instruction.
                     the processor to jump to the targeted subroutine file.
                     Each subroutine must have a unique file number        •  At each ladder location where a subroutine is called,
                     ( decimal 3–255).                                       program a JSR instruction specifying the subroutine
                                                                             file number.
                     Subroutine (SBR)—The SBR instruction is the first
                     input instruction on the first rung in the subroutine   •  The RET instruction is optional.
                     file. It serves as an identifier that the program file is     – The end of a subroutine program will cause a
                     a subroutine. This file number is used in the JSR in-      return to the main program.
                     struction to identify the target to which the program      – If you want to end a subroutine program before it
                     should jump. It is always true, and although its use is    executes to the end of program file, a conditional
                     optional, it is still recommended.                         return (RET) instruction may be used.
                     Return (RET)—The RET instruction is an output         Nesting subroutines allows you to direct program flow
                     instruction that marks the end of the subroutine file.   from the main program to a subroutine and then to another
                     It causes the scan to return to the main program at the   subroutine, as illustrated in Figure 9-13. Nested subrou-
                     instruction following the JSR instruction where it ex-  tines make complex programming easier and program op-
                     ited the program. The scan returns from the end of the   eration faster because the programmer does not have to
                     file if there is no RET instruction. The rung containing   continually return from one subroutine to enter another.



                                                                               Program Control Instructions   Chapter 9  191







          pet73842_ch09_184-206.indd   191                                                                              03/11/15   4:01 PM
   205   206   207   208   209   210   211   212   213   214   215