November 27, 2024
PLC

A Quick Guide to Subroutines in Studio 5000 Logix Designer

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.

Routines

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.

Create and Call a Routine

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.

Data Exchange with Subroutines

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.

Subroutine Warnings

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.

Wrap Up

In this post, I gave you a quick guide to subroutines in Studio 5000 Logix Designer including;

  • What a routine is,
  • How to create and call a subroutine,
  • How to pass data to and retrieve data from a subroutine

You should be able to use this information to use subroutines in your own projects to organize and reuse your code.

Good luck!

30 Day Free Trial

Get access to all my courses and priority support for free for 30 days.

Learn More

Claim your discount

Sign up to the mailing list to receive a 25% discount off all products.

Thanks for joining our newsletter. Use the code "LIST25" at checkout for your discount.
Oops! Something went wrong while submitting the form.
Subscribe to Our Weekly Newsletter - Courselify X Webflow Template
Subscribe to Our Weekly Newsletter - Courselify X Webflow Template
Subscribe to Our Weekly Newsletter - Courselify X Webflow Template

Related courses

Structured Programming in Studio 5000 Logix Designer

Learn how to use arrays and user-defined data types for structured programming in Studio 5000 Logix Designer

Coming Soon

Sequential Function Chart in Studio 5000 Logix Designer

Learn how to write logic using the Sequential Function Chart programming language

Coming Soon

Structured Text in Studio 5000 Logix Designer

Learn how to write logic using the Structured Text programming language

Coming Soon