The JSR instruction is a ladder logic rung output instruction. When the instruction is scanned program execution jumps to the subroutine named in the JSR instruction. After the called subroutine is executed the logic execution continues from the rung the subroutine was called from. The instruction is enabled when the preceding logic is true and disabled otherwise.
The JSR instruction can be used alone or with the SBR and/or RET instructions. At minimum the JSR instruction must contain the “Routine Name” that is to be called. In other words parameters and their associated instructions are not required.
SBR or Subroutine Instruction
RET or Return From Subroutine Instruction
To pass a parameter to a subroutine:
Parameters may be passed to subroutines called out by a JSR instruction. To do this the JSR instruction must contain an Input Parameter (Tag or Constant) and an SBR instruction with an Input Parameter (Tag) must be in the called subroutine.
The Input Parameter data from the JSR instruction is copied to the Input Parameter of the SBR instruction.
For instance it you call a subroutine named “Conveyor_01” with a JSR containing an Input Parameter (Tag or Constant), the first rung in the subroutine “Conveyor_01” must contain an SBR instruction with an Input Parameter (Tag) to receive the parameter.
Note: The SBR instruction must be the first instruction of the first rung in the called subroutine.
INPUT PARAMETERS can be any combination of the following data types. They are optional and multiple input parameters can be used.
- CONSTANT When used as an Input Parameter on a JSR
To return a parameter to a subroutine:
Data can be passed back to the JSR instruction that called the subroutine. To do this the JSR instruction must contain a Return Parameter (Tag) and a RET instruction with a Return Parameter (Tag or Constant) must be in the called subroutine.
The Return Parameter data from the RET instruction is copied to the Return Parameter referenced in the calling JSR instruction.
For instance it you were to call a subroutine named “Conveyor_01” with a JSR containing a Return Parameter (Tag), the subroutine “Conveyor_01” must contain a RET instruction with a Return Parameter (Tag or Constant).
Note: The RET instruction can be anywhere in the called routine. When enabled the RET instruction jumps back to the JSR that called the instruction.
RETURN PARAMETER can be any combination of the following data types. They are optional and multiple return parameters can be used.
- CONSTANT When used as a Return Parameter on a RET
Do not use the JSR instruction to call the main routine as it already runs unconditionally. Do not use the RET instruction in a main routine as a fault will occur.
The JSR, RET and SBR instructions can show or hide parameters. There is more than one way to do this, however only one will be covered here.
To add an input Parameter or Return Parameter to a JSR instruction: Right click on the Routine Name variable. From the pop up menu choose “Add Input Parameter” or “Add Return Parameter”. Curiously enough if one or more Input or Return parameters are already visible you have to Right click on the visible parameter to display all the selections. To remove a parameter right click on the parameter and choose “Remove Instruction Parameter” from the pop up window.