Counter with Overflow Protection

Lib.Mervis.v1_0.IncrementalCounter

  • Lib.Mervis

A helper block used for handling counter inputs that are at risk of overflow.

program test
    var
        inp: lib.mervis.v1_0.IncrementalCounter;
    end_var

    inp();
end_program
Name Data TypeRetainMandatory Connection Meaning
In udint No Yes Counter input value
Set bool No No Counter reset
Overflow udint No No Maximum counter input value for overflow handling (if =0, not used)
Step udint No No
PulseWeightreal No No Pulse weight, used to convert the counter value to a physical value
Offset real No No Pulse count offset, used to convert the counter value to a physical value
Name Data TypeMeaning
Out udint Output
Weighted real Output converted to a physical value using PulseWeight and Offset
OverflowFlag bool Overflow flag
OverflowCountudint Counter overflow count

The block is used to handle the value from a counter that frequently overflows. The block contains an internal 32-bit counter, which always increments by the number of incoming pulses (the difference from the previous computation cycle). If this difference is negative, it is considered an overflow, and the behavior depends on the Overflow parameter:

  • Overflow = 0 → the change is ignored, and the internal counter state does not change.
  • Overflow > 0 → the counter has overflowed, the difference Overflow - Penultimate input value and also the current counter state is added to the internal state.

The Weighted output is calculated as a linear transformation of the internal counter state, i.e., Weighted = PulseWeight * Out + Offset

  • © Energocentrum Plus, s.r.o. 2017 - 2024