ControlLogix Odd/Even Detection


Introduction to ControlLogix Odd/Even Detection

There are a couple ways to implement ControlLogix Odd/Even Detection for DINT tags. One way is to divide your number by 2, and store this to a real tag. Divide the same number by 2, and store the result to a DINT tag. If the values agree, then we know the number is even. Another way to do this is at the binary level. We can simply look at bit zero of a DINT tag. If the least significant bit is 0, then we know the value is even.

There are several reasons to do this. One reason would be for sorting odd and even parts for distribution. Another reason might be to use a single push button for on/off control of an output.

In this post, we’ll take a look at both methods, and how to create the logic.

Method #1 — Division

This is the method I used to use years ago using BASIC on the Commodore 64. We’ll just divide the value by 2. If the floating point result equals the integer result, then we know the number is even. We will test the tag “TestSourceDINT” to determine if it’s even or odd.

In this case, we’ll write three rungs of logic. First, we’ll divide our original value by 2. We’ll store this result to a tag with the REAL data type. This will show us the actual result of our division using floating point math.

At this point, we’ll add another DIV statement. This time, we’ll store the result to a DINT tag. Keep in mind that the DINT data type cannot have decimal points. The processor will round the value.

Finally, we just compare the two results. If TestDestinationREAL is equal to TestDestinationDINT, then we know the number is even. However, if they are not equal, then we know the number is ODD.

Method #2 — Binary

With this method, we simply look at Bit #0 of the value. This method is much easier to implement, and uses less memory, and scan time. If you use this method, be sure to document what you are doing. Remember, someone else will need to troubleshoot your code years into the future.

Basically, in the binary numbering system, bit #0 of a DINT has the value of 0 if the number is even. On the other hand, if the integer value is odd, bit #0 has the value of 1. Let’s take a look at how this works.

In this example, TestSourceDINT is EVEN. Therefore, TestSourceDINT.0 is OFF.

Now let’s take a look at what happens when TestSourceDINT is ODD:

This is a useful feature of the BINARY numbering system. All we need to do is write a rung of logic that looks at this bit to determine if our value is odd or even.

Additionally, we can use this feature as a “frequency” divider. Let’s say, TestSourceDINT is counting upwards. Bit 0 will change every count. Likewise, bit 1 will change every 2 counts. Bit 3 will change every 4 counts, and so on.

Summary of ControlLogix Odd/Even Detection

In short, you can use these methods in any programming language in your ControlLogix processor. Simply divide a DINT by 2, and store the result to a tag with a REAL data type. Divide again by 2, and store the result to a DINT data type. If the values are equal, then the original number is even. The most efficient way to determine odd/even is to look at the least significant bit in binary. If you use this easier method, though, be sure those who troubleshoot your code know what you are doing. Not everyone understands how the binary numbering system works.

For more information, visit the CotnrolLogix Category page!

— Ricky Bryce

Leave a comment

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