JSR, SBR, RET Jump To Subroutine, Subroutine, & Return Ladder Logic Instructions

JSR Definition

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

 

SBR

      
                 

 

RET or Return From Subroutine Instruction

RET

 

 

 

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
  • BOOL
  • SINT
  • INT
  • DINT
  • REAL
  • STRUCTURE

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
  • BOOL
  • SINT
  • INT
  • DINT
  • REAL
  • STRUCTURE

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.

Note: Arithmetic status flags are affected (S:V, S:Z, S:N, S:C). Overflow, Zero, Negative, and Carry.

 

IOT (Immediate Output) Ladder Logic Instruction

Definition

The IOT instruction is a ladder logic output instruction that updates output data immediately rather than waiting for the RPI of the module or tag to update the data.

Use the instruction to update the output data of an entire output module. For instance, when you use the instruction to update an output card, address the entire card, not the individual outputs.

Correct:           Local:3:O

Incorrect:         Local:3:O.Data.0

The instruction can also be used to update a produced tag. When using the instruction with a produced tag an Event Trigger is sent to the consuming processor. This allows the consuming processor to trigger an Event Task.

Note: When using the IOT instruction produced data is transferred immediately when the communication method is Ethernet I/P or the Backplane, and within the API (Actual Packet Interval) of the Consumed Tag over ControlNet.

TND (Temporary End) Ladder Logic Instruction

Definition

The Temporary End ladder instruction is intended for debugging and troubleshooting. The instruction can be placed on a rung before the end of a routine. Logic before the TND instruction is scanned normally and logic after the instruction is skipped. 

Note: Great instruction for debugging code line by line or block by block.

 

UIE (User Interrupt Enable) Ladder Logic Instruction

Definition

The (UIE) User Interrupt Enable instruction is used in conjunction with a (UID) User Interrupt Disable instruction. When enabled, the UID instruction prevents tasks with higher priority from interrupting the execution of the current task. The UIE instruction enables other tasks to interrupt the current task.

The task fault routine and the Controller fault handler are not disabled by the UID instruction.

Take caution; limit the amount of rungs placed between the UID and UIE instructions as a loss of communication may occur. In other words use only as needed.

The following image demonstrates the proper use of the UID and UIE instructions.

UID_UIE_Example

UID (User Interrupt Disable) Ladder Logic Instruction

Definition

The (UID) User Interrupt Disable instruction is used in conjunction with a (UIE) User Interrupt Enable instruction. When enabled, the UID instruction prevents tasks with higher priority from interrupting the current task. The UIE instruction allows other tasks to interrupt the current task.

The task fault routine and the Controller fault handler are not disabled by the UID instruction.

Take caution; limit the amount of rungs placed between the UID and UIE instructions as a loss of communication may occur. In other words use only as needed.

The following image demonstrates the proper use of the UID and UIE instructions.

UID_UIE_Example

 

LBL (Jump to Label) Ladder Logic Instruction

Definition

The Label instruction is used in conjunction with a Jump instruction. When the JMP instruction is enabled, the code between the JMP and LBL instruction is not executed. Code execution begins immediately after the LBL instruction. When the JMP instruction is disabled the code between the JMP and LBL instruction is executed normally. A LBL instruction is required when a JMP label is inserted.

The JMP/LBL instructions can move ladder execution backward or forward. Skipping forward saves scan time while skipping backward increases scan time.

The LBL instruction has the same name as the JMP instruction which calls it.

Example:

         Instruction                   Name

  • JMP                           SKIP_CODE
  • LBL                            SKIP_CODE

 

Names can be up to 40 characters long and can contain letters, numbers and the underscore ( _ ) character.

 

Note: Watch the amount of time you jump backward as this increases scan time and can cause a Watchdog fault if excessive. Jumping forward can save scan time by skipping code that does not need to be scanned continuously. Also make sure the LBL instruction is the first or left most instruction on a rung.

JMP (Jump to Label) Ladder Logic Instruction

Definition

The JMP instruction is used in conjunction with a LBL Label instruction. When the JMP instruction is enabled the code between the JMP and LBL instruction is not executed. Code execution begins immediately after the LBL instruction. When the JMP instruction is disabled the code between the JMP and LBL instruction is executed normally. A LBL instruction is required when a JMP label is inserted.

The LBL instruction has the same name as the JMP instruction which calls it.

Example:

          Instruction                   Name

  • JMP                           SKIP_CODE
  • LBL                            SKIP_CODE

Names can be up to 40 characters long and can contain letters, numbers and the underscore ( _ ) character.

The JMP instruction can move ladder execution backward or forward. Skipping forward saves scan time while skipping backward increases scan time.

Note: Watch the amount of time you jump backward as this increases scan time and can cause a Watchdog fault if excessive. Jumping forward can save scan time by skipping code that does not need to be scanned continuously. Also make sure the LBL instruction is the first or left most instruction on a rung.

 

EVENT Ladder Logic Instruction

Definition

The Event instruction is used to trigger an Event task. The task is run one time for each trigger. Make sure the event has enough time to complete before calling the task again. The instruction is enabled when the preceding logic is true and disabled otherwise.

Arithmetic flags are not affected.

Note: Check the task overlap counter to see if the task has been called before task execution is complete.

NOP (No Operation Instruction) Ladder Logic Instruction

Definition

The NOP is a ladder logic rung instruction that does nothing. Place it anywhere on a rung or branch and it will do nothing. Have fun with it. Use it as a placeholder for a search. It can be used in place of an output instruction just in case you want your logic to do nothing.

Note: See the AFI and MCR for related instructions.

AFI (Always False Instruction) Ladder Logic Instruction

Definition & Video

The AFI is a ladder logic rung instruction that sets the rung output to false. Use this instruction to temporarily make the output of the rung false, great for troubleshooting.

Note: See the NOP and MCR for related instructions.