ControlLogix Structured Text Functions


Introduction to ControlLogix Structured Text Functions

ControlLogix Structured Text Functions allow us to perform the same calculations several times through a processor scan. For example, we might want to convert Celsius to Fahrenheit. We simply create another routine that acts as our function. After that, we can pass and receive parameters from this subroutine. To call a subroutine as a function, we use the JSR statement. The JSR statement contains the input and return parameters.

Create Your Tags

Before we begin we’ll create some tags. This will give us some values to pass to the subroutine and a place to put the value that we convert. Let’s create three tags to hold Celsius Values. We’ll also create three Fahrenheit tags to store the result. Additionally we’ll create a tag as a workspace for our subroutine to read and store data. All of these tags will have the REAL Data Type. This allows us to store decimal point values in the tags.

While we’re here, let’s go over to “Monitor Tags”. We’ll populate the Celsius tags with some random values. Later on, we’ll convert these values to Fahrenheit.

Create Your Subroutine for ControlLogix Structured Text Functions

At this point, we are ready to create the subroutine. Right click on the program you wish to add the subroutine to and select “New Routine”. We’ll just name this “CTOF”. Be sure to make this a “Structured Text” type routine.

Additionally, I’m going to delete the MainRoutine and add it back as Structured Text. In order to delete the MainRoutine, I went to the properties of the MainProgram. On the Configuration tab, I made CTOF the main routine temporarily. After I created the MainRoutine as a structured text routine, I went back to the properties of the program to make it the main routine again.

Add Logic to Your MainRoutine

In the MainRoutine, we’ll add a JSR Instruction. The JSR instruction does not always have to go in the MainRoutine. It can go in any routine that is already executing within our program. In this case, we don’t have a choice though. Only one routine is executing. It’s important to realize, though, that when you nest subroutines, you cannot go more than 32 deep. You will fault the processor.

First let’s try this with only one JSR. Once we get that working we will go back and add the other two. The first JSR instruction will take data from Celsius1. This is called the INPUT parameter. Likewise, when we receive an answer back from the subroutine, we will place this value into Fahrenheit1. We call this the RETURN parameter. Do NOT finalize your edits yet. If you finalize edits before the subroutine is set up properly, you will fault the processor. It needs to be able to receive and return the correct number of parameters.

The format is: JSR(CTOF,1,Celsius1,Fahrenheit1);

Basically, we are jumping to the CTOF routine. We have 1 input parameter. The input parameter is Celsius1, and the return parameter is Fahrenheit1.

Add Logic to Your Subroutine

Let’s add the following logic to your subroutine. As you can see we need an SBR as the first instruction. This SBR will receive a value from the JSR that called it. It’s important to realize that this subroutine does not know what tag the value came from. There could be hundreds of JSR’s passing values to our subroutine. All we know is that we are getting a value. We will place this value into our workspace. Be sure to end each command with a semicolon.

After the SBR we’ll calculate the Fahrenheit value. Workspace := Workspace * 1.8 + 32; It’s OK to store the answer back to Workspace, because after we convert the Celsius value we don’t need that original value anymore.

Finally we need to return the converted value (workspace) back to the JSR. Remember, we don’t know where the JSR will place the value. The JSR Knows that.

Now if you go back to the Tags, you should see that our first conversion is working.

Finish up the Main Routine

At this point, we just need to go back and add the other two JSR’s to the MainRoutine. We do not have to modify the subroutine.

Now if we check our tags, everything should be working!

Summary of ControlLogix Structured Text Functions

In short we just need to create a subroutine one time. We can have as many JSR’s as we like in other parts of the program without modifying this subroutine. The JSR simply passes a value (or values) to the subroutine. The subroutine manipulates the data then returns it back to the JSR. The JSR then places the data it receives into the tags for the return parameter.

Keep in mind that you could also do something similar to this using Add-On Instructions.

— Ricky Bryce

Leave a comment

Your email address will not be published. Required fields are marked *