Structured Text Compare Statements


Introduction to ControlLogix Structured Text Compare Statements

The ControlLogix Structured Text Compare Statements work a bit differently than the compare statements of ladder logic and function blocks. In Structured Text, the compare statements are similar to what you see in other standard programming languages. Basically, we can test a value to see if it’s greater than, less than, or equal to another value, etc. In Ladder Logic and Function Blocks there is a separate block instruction that we use.

Some programmers prefer structured text for several reasons. First, it’s probably closer to what they learned in college for programming. Secondly, it’s usually easier to perform complex math in structured text. Additionally, several programmers can develop the code in Notepad without having to buy a license for Studio 5000. Structured text closely resembles the PASCAL programming language. However, as electricians and troubleshooters, we usually hate it.

The most common compare statements include:

  • Equal “=”
  • Not Equal “<>”
  • Greater Than “>”
  • Less Than “<“
  • Greater Than or Equal “>=”
  • Less Than or Equal “<=”

Usually, we use these statements inside of an “If…Then” Structure. Occasionally you may also see this in a “While… Do” loop as well as other structures.

In this section, we’ll go over some examples of these compare instructions.

Create Your Tags

Before we begin, we’ll need to create some tags. We’ll use these tags to prove our logic. We’ll need 5 BOOL tags, and one DINT tag. Later on, we’ll put our own values into the “MyValue” tag. Be sure you are in “Edit Tags”. As long as you are not in RUN mode, you should have a blank line at the bottom to add new tags.

Write Your Logic

We’ll create a new subroutine that is the “Structured Text” type. To add a routine just right click on a program and add a new routine. If you cannot create such a routine, then you might not have a software license that supports structured text. Keep in mind that if you create a subroutine, you will probably want to add a JSR instruction into the MainRoutine (or another routine that is executing). Otherwise, the processor will not solve your logic.

Be sure to end each command with the semicolon “‘;”. Here I’m breaking each section down into a separate if… then statement.

if MyValue = 0 then 
	boolEQU := 1;
Else
	boolEQU := 0;
end_if;

if MyValue <> 0 then 
	boolNEQ := 1;
Else
	boolNEQ := 0;
end_if;

if MyValue >= 0 then 
	boolGEQ := 1;
Else
	boolGEQ := 0;
end_if;

if MyValue > 0 then 
	boolGRT := 1;
Else
	boolGRT := 0;
end_if;

if MyValue <= 0 then 
	boolLEQ := 1;
Else
	boolLEQ := 0;
end_if;

if MyValue < 0 then 
	boolLES := 1;
Else
	boolLES := 0;
end_if;


After you are finished, you can finalize your edits or download. Just remember if you download, then your system will go down!

Test Your Work

To test our work, we’ll bring up the watch window in Studio 5000. The watch window displays the values of tags that are in this routine. We’ll place different numbers into the “MyValue” Tag… Try a value that is less than zero. As you can see, three tags are true. LEQ, LES, and NEQ.

Let’s try zero. Once again, three tags are true. GEQ, LEQ, and EQU.

After that, try a positive value. Obviously, NEQ, GRT, and GEQ become true.

Summary of Structured Text Compare Statements

In short structured text should not be intimidating. It’s just English as if you were reading a book. The hardest part is getting the syntax exactly right. Just keep in mind that every command should have a semicolon after it. Additionally if you start an if… then… statement don’t forget the End_If instruction at the end of the structure.

To learn more about structured text, simply type “structured text” in the search bar at the top of this website!

For other information on ControlLogix, visit the ControlLogix Category Page.

— Ricky Bryce

For Customized automation training, visit my employer's website at http://atifortraining.com!

Leave a comment

Your email address will not be published.

eight  +  one  =