Introduction to Simple subroutine.
We use ControlLogix simple subroutines mainly for the purpose of organization. You can add all of the logic to the MainRoutine. However, it’s easier to program and troubleshoot when you divide logic into subroutines.
You need to tell the processor to execute subroutines with the JSR instruction. Usually, you will place the JSR instruction for simple subroutines into the MainRoutine.
When you add the subroutine, you must also declare the language that the subroutine will use. Controllogix supports four programming languages: Ladder Diagram, Sequential Function Chart, Function Block Diagram, and Structured Text. The most common type of subroutine is “Ladder Diagram”. Many industrial locations are starting to use Function Block Diagrams for analog manipulation. You can use Structured Text for complex algorithms that are cumbersome to work with in Ladder Diagrams. You will not normally use Sequential Function Blocks in the United States, although, there are a few locations where this is common.
To add subroutines, right click the program. Here, we will just use the main program.
The name of this routine is “Pumps”, and it will be a ladder diagram.
Follow the same procedure to create two more subroutines: Motors and Valves. When you finish, your program tree should look like this:
Next, add your JSR instructions. To do this, open (double-click) the MainRoutine. Highlight Rung #0, then type the following text: “jsr pumps”. That is jsr (then a space) followed by the routine name “pumps”.
Repeat this procedure for the motors, and valves routine. Be sure to click the END rung each time to prevent overwriting your previous logic. When you are finished, your logic should look like this:
Your processor will execute the pumps routine, then come back to the JSR in the main routine. Next, the logic will execute the motors routine, then return to the MainRoutine. Finally, your logic will execute the valves routine, then return back to the main routine to execute any remaining logic.
Let’s take a look at what would happen if one of the JSR’s were in a subroutine. This is called “Nesting Subroutines”. You have to be very careful when nesting subroutines. Here is an example of what the scan will look like if the JSR to Valves is placed in the motors routine.
If the processor attempts to nest subroutines too many times (32 levels deep), the processor will fault. Except in a very small amount of cases, you never want to jump to the same subroutine that you are placing the JSR in. This will usually cause a processor fault very quickly. Here, we are just nesting subroutines 2 levels deep.
Also, read about passing parameters to subroutines if you wish to use subroutines as a function.
— Ricky Bryce