PLC programs are executed sequentially. In other words, the PLC executes a rung before proceeding to the next one. Within a single rung, the PLC will execute each sub-branch before proceeding to the next one. A PLC programmer may choose to navigate between rungs in a non-sequential order as defined by the routine. In other words, using instructions such as JSR, JMP and LBL allows one to create an order which doesn’t follow the rung labels.
We’ve been using the JSR, or the Jump to Subroutine instruction, in many places of our code. This simple instruction allows the programmer to indicate which routines to execute within a program. As the PLC scans the JSR instruction, it executes the associate subroutine. At the end of the specified subroutine, the PLC returns to the instructions and continues from there.
The JMP and LBL instructions allow one to jump between different rungs within a routine or otherwise. These isntructions are simple in nature, but create an environment which may confuse even the most experienced programmers. It’s important to understand them in order to avoid troubleshooting complications in PLC systems.
The JSR Instruction is a fundamental building block of any PLC program. It allows the PLC to execute code within different routines and specifies the sequence of their execution.
From a practical standpoint, the most common use of the JSR Instruction is within the “Main” routine. This routine will point to all the others in order to execute them once per PLC scan. See the example below of this exact setup.
In the image above, the PLC will execute the rung “0” by calling the “_02a_Inputs” routine first. Once the routine completes, the PLC will return to the rung and execute “_03a_Outputs”. The PLC will then jump to rung “1” and execute the next instruction.
By defining the structure of JSR instructions, the programmer will make sure that every piece of code is executed.
Although the premise of the JSR Instruction is simple, it’s important to keep a few things in mind.
The JMP and LBL instructions work together. The LBL will specify a point within a routine. By calling this point, the JMP will navigate the PLC execution directly to the location of the LBL. Let’s explore this concept in more detail.
Suppose that a routine has 5 distinct rungs. The routine is executed as expected. In other words, Rung 0, rung 1, 2, etc. We may add a LBL Instruction on Rung 4. The LBL Instruction by itself does not change the execution; the routine operates exactly as it did. Once we add a JMP Instruction with the same name as the LBL, things change. If the JMP isn’t preceeded by a condition, it will always execute just like any other instruction. Suppose that the JMP is placed in Rung 0. Therefore, the PLC will execute Rung 0 & immediately jump to rung 4 which contains the LBL. The rungs between the two instructions will not execute.
As you implement the JMP / LBL pair into your code for the first time, the functionality may seem undewhelming. However, these instructions are highly flexible. As you add a condition before the JMP, you can start controlling at which point you want to execute certain pieces of your code. Furthermore, by combining the JSR routine into the mix, you may choose the execution of certain routines based on conditions of your code.
It’s important to keep in mind that issues may arise with improper or overexcessive use of these instructions. You may create infinite loops, areas which are never executed and simply confusing routines. Troubleshooting code which contains these instructions isn’t always obvious as the PLC will not explicitely tell you what’s being execute and what isn’t.
Use these instructions wisely.
The JSR, JMP and LBL Instructions are highly versatile. They allow the programmer to create sequences of execution which are non-linear. However, there’s a tradeoff which isn’t always obvious. Code which contains these instructions isn’t always simple to troubleshoot and follow. It is highly advised to avoid using these instructions unless you’ve had a lot of experience programming PLC based systems.