A guide to using subroutines in Studio 5000 Logix Designer to organize and reuse your code.
This post explains what a routine is in Studio 5000 Logix Designer and how subroutines can be used to organize and reuse code in a project.
Let's start by talking about what a routine is.
A routine is the area where application logic is viewed and created. Every project includes a MainRoutine by default.
A program can have multiple routines and each routine can be written in a different programming language. The programming languages supported by Studio 5000 Logix Designer are Ladder Logic, Function Block Diagram, Sequential Function Chart, and Structured Text. We'll learn how to write logic in each of these programming languages in future courses.
Every routine in a program is classified as a main routine, subroutine, or fault routine.
The main routine is the routine that is called when a program is executed. A program can only have one main routine and it is identified with a "1" symbol on the routine's icon.
A fault routine is a routine that is called if the controller encounters a recoverable major fault while executing a program. Once again, there can only be one fault routine in a program and it is identified with an "!" symbol on the routine's icon.
Finally, any routine that is not a main routine or a fault routine is automatically considered a subroutine. The only limit on the number of subroutines that a program can have is the controller's memory. Subroutines are not executed by default - they must be explicitly called by a Jump to Subroutine instruction.
Now that we know what a routine is, let's see how we can call subroutines in a program.
You can add a routine to a project by right-clicking on a program and select Add > New Routine.
The New Routine dialog opens.
In this dialog, you can give your routine a name and description. You can also specify the programming language used for the routine, the program that the routine belongs to, and the assignment of the routine.
The new routine is created and added to the Controller Organizer under the MainProgram.
You can open a routine by double-clicking on it. Once a routine is open, you can write your application logic in it.
To execute the subroutine, we have to call it explicitly from the main routine using a Jump to Subroutine or JSR instruction as shown here.
To promote code reuse, you can pass data to and from a subroutine. You can define what data is exchanged with the subroutine by adding input and return parameters to the subroutine as shown here.
Inside the subroutine, you use a Subroutine instruction to map the data passed into the subroutine to local tags as shown here. These local tags can be used throughout the subroutine to give the subroutine dynamic behavior.
To return data from a subroutine, you use a Return from Subroutine instruction as shown here.
Now that our subroutine supports dynamic data, we can call it multiple times with different input parameters to get different outputs.
When working with subroutines, there are a few configuration issues that can cause major faults in the controller. The most common configuration issues are:
The Jump to Subroutine instruction has fewer input parameters than the Subroutine instruction in the called subroutine.
A Jump to Subroutine instruction is used to jump to a fault routine.
The RET instruction has fewer return parameters than the JSR instruction.
The main routine contains an RET instruction.
In this post, I gave you a quick guide to subroutines in Studio 5000 Logix Designer including;
You should be able to use this information to use subroutines in your own projects to organize and reuse your code.
Good luck!
Get access to all my courses and priority support for free for 30 days.
Sign up to the mailing list to receive a 25% discount off all products.
Learn how to use arrays and user-defined data types for structured programming in Studio 5000 Logix Designer
Learn how to write logic using the Sequential Function Chart programming language
Learn how to write logic using the Structured Text programming language