Program control (output instruction)

JSR: Jump To Subroutine

Runs another routine, then continues from the next rung.

Also called: call routine, subroutine call, CALL.

JSR(routine)

What it does

JSR is how a program stops being one long list of rungs. Group the logic for a station, a mode, or a piece of equipment into its own routine and call it from the main program, and both the main program and the routine become readable.

The important thing to understand is that a routine only runs when it is called. Logic in an uncalled routine does nothing at all, which is a perfectly ordinary way to organise a program by mode and also the most common reason a generated or half-finished program appears to do nothing.

Gating a JSR on a condition that the routine itself is responsible for setting produces a deadlock: the routine cannot run, so it cannot set the bit, so it never runs.

A rung you can run

Split auto and manual into their own routines

[object Object]
ROUTINE Main
RUNG
  XIC(Mode_Auto)JSR(AutoSeq)
RUNG
  XIO(Mode_Auto)JSR(Manual)

ROUTINE AutoSeq : SUBROUTINE
RUNG
  XIC(Start_Cmd)XIO(Fault)OTL(Cycle_Active)

ROUTINE Manual : SUBROUTINE
RUNG
  XIC(Jog_Fwd)XIO(Fault)OTE(Motor_Fwd)

Exactly one of the two routines runs on any given scan, and the outputs each one drives are not being fought over by the other.

Run this rung in the simulator

What it is called on other controllers

PlatformEquivalent
Rockwell (Studio 5000)JSR
Siemens (TIA Portal)Calling an FC or FB
Mitsubishi (GX Works)CALL
CODESYS and IEC 61131-3A program or function block call

Common mistakes

  • Gating a routine on a bit only that routine can set.
  • Assuming a routine runs because it exists. It runs when something calls it.
  • Calling a routine from inside itself.

Related instructions

All ladder logic instructions