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
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, so the outputs each one drives are never being fought over by the other. Toggle Mode_Auto and watch which rungs light up: in manual the jog works and the cycle latch cannot be set, and in auto it is the other way round.
Run this rung in the simulatorWhat it is called on other controllers
| Platform | Equivalent |
|---|---|
| Rockwell (Studio 5000) | JSR |
| Siemens (TIA Portal) | Calling an FC or FB |
| Mitsubishi (GX Works) | CALL |
| CODESYS and IEC 61131-3 | A program or function block call |
Common mistakes
- Gating a routine on a bit that only that routine can set. The call never runs, so the bit is never set, so the call never runs: a deadlock that looks exactly like a routine full of correct logic doing nothing at all.
- Assuming a routine runs because it exists. Rungs in a routine nothing calls are never scanned, and nothing anywhere reports it. If a section of a program appears dead, check that something calls it before suspecting the logic.
- Calling a routine from inside itself, directly or round a loop. A real controller faults on the recursion; this simulator declines the call so the rest of the scan still runs, which is gentler and just as wrong.
Practise it
Interactive exercises that use JSR. Each one checks your ladder against a real scan.
