Structured Text Counter (CTUD)


Introduction to the Structured Text Counter (CTUD)

We use the CTUD in Structured Text, or in Function block to count up or down. CTUD is the instruction for the Count Up/Down Counter. One instruction will work both ways. Each time the CUEnable bit goes true, the counter increments. On the other hand, when the CD bit goes true, the counter decrements. The counter will count transitions. You might use this to count parts or machine cycles. Additionally, you might use the CTUD to track bit transitions in other parts of logic. You would use it this way for troubleshooting.

Add your Tags

I’ll add two program tags. We’ll use “MyTimer” to generate a pulse to test our counter. This will have the counter data type. Likewise, we’ll add “MyCounter”. This will have the FBD_Counter data type.

Add a Structured Text Routine

I’m going to right-click on a program to add a subroutine. I’ll name it “STXRoutine”. Obviously, the type of this subroutine will be “Structured Text”. Keep in mind that since this is a subroutine, we will need to add a JSR instruction into the Main Routine for the logic to execute.

Add a Timer in Logic

In order to test the CTUD’s operation, we will use a self-running timer. Basically, this timer generates a pulse. In reality though, you might use a limit switch or photo eye for the CTUD. Before we begin, let’s add the JSR instruction and our timer to the MainRoutine.

Add your Counter

At this point, we are ready to add the counter to the subroutine. If you do this online, you will need to start the routine edits.

Next, let’s take a look at our tag for the counter.

Whenever Enable is high, the instruction updates it’s outputs. CUEnable and CDEnable will be our inputs to the instruction. In other words, when CUEnable goes true, the accumulator increments. By the same token when the CDEnable bit goes true, the instruction decrements. PRE is our preset, and ACC is our accumulator. When the Preset is at or above the accumulator, our DN (DONE) bit goes high. Obviously, Reset will reset the counter back to zero. CU and CD Bits indicate when the corresponding Enable bits are true. OV is Overflow and UN is underflow. EnableOut is high when the instruction produced a valid result.

It’s important to realize that the overflow (OV) bit does not go true when the accumulator passes the preset. The instruction will continue to count. Keep in mind that the PRE and ACC are both of the DINT datatype. For this reason, the maximum value for the counter is 2147483647. That is to say, all bits are high except for the sign bit. Once the counter crosses this number, it sets the OV bit. At this point the number will go negative, and continue counting upward.

On the other hand, if we go in the negative direction, and keep going past -2147483648 (and the OV is already clear), the counter will underflow.

Add the logic for our Structured Text Counter (CTUD)

In brief, if we want the counter to count up, we need to turn on the CU Enable bit. Let’s do this in our structured text routine.

Notice that we needed to use a := (colon and equal) to assign a value to the CUEnable bit. Equally important is that we end our commands with the simicolon ( ; ).

Finally, we’ll finalize our edits, and watch the accumulator on the counter. Click View | Watch to open the watch window. This will display the value of our tags that are in this routine.

Summary

In short, Structured Text Counters are very easy. We simply have the CTUD instruction followed by it’s tag in parenthesis. After that, we just set the CUEnable bit to count up, or the CDEnable bit to count down. The Reset member will reset the counter back to zero.

At this point, I would recommend checking out the post on the Structured Text Timer!

— Ricky Bryce

Leave a comment

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