As always reference the LinuxCNC manuals and the LinuxCNC Wiki for more information.
In this example we will create a self reseting counter. Self reseting counters are used when you need a counter to wrap around. For example if your trying to maintain what position a turret is at that has an input for each time it indexes. In this example I’ll use the Value of the counter and compare it to the Preset of the counter. When the value of the counter is equal to or greater than reset the counter. Now some of you wonder why not just use equal to reset the counter. If your code causes the counter value to go above the set point it will never reset. Using >= makes sure this can never happen.
In the following circuit we have two rungs, the first rung enables the counter input and the second rung is the counter. In this circuit because %Q0 is not on it blocks the Up input to the counter preventing any count pulses from reaching the counter.
Now we have latched in %Q0 and this enables the Up input to the counter. Notice how the %Q0 contact on the Up input is lit up to indicate it is on.
Next we cycled the %I1 input a few times and you can see the counter value is 7. The preset is 8 so the comparison block Counter 0 Value (%C0.V) is greater than or equal to (>=) Counter 0 Preset (%C0.P) is still false.
Cycling the %I1 input one more time and the comparison block evaluates to true and several things happen. When the counter reached the preset value of 8 the done bit (%C0.D) came on for one scan, this dropped out the latching circuit. Next the comparison toggled the reset (%C0.R) on the counter and set the accumulated value back to 0. The %C0.D bit is only on for one scan so when it goes back off you can latch the first rung in again.