====== Counter with Overflow Protection ====== {{:cs:mervis-ide:35-help:incrementalcounter.png}} ==== Full Name ==== Lib.Mervis.v1_0.IncrementalCounter ==== Required Project References ==== * Lib.Mervis ===== Description ===== A helper block used for handling counter inputs that are at risk of overflow. ===== Usage in ST ===== program test var inp: lib.mervis.v1_0.IncrementalCounter; end_var inp(); end_program ===== Inputs ===== |Name |Data Type|Retain|Mandatory 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 | | |PulseWeight|real |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| ===== Outputs ===== |Name |Data Type|Meaning| |Out |udint |Output| |Weighted |real |Output converted to a physical value using PulseWeight and Offset| |OverflowFlag |bool |Overflow flag| |OverflowCount|udint |Counter overflow count| ===== Detailed Function Description ===== 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//