greydet
greydet

Reputation: 5539

How to detect/prevent a counter overflow

I have a counter on a 16 bits field that is incremented/decremented over the time by a hardware peripheral.
I sample its value periodically to sum the difference into a 32bits field.

My problem is to detect the overflow/underflow of the 16 bits field when computing the difference.

Let's take an example:
At sample n-1, the counter value Vn-1 is 65530.
As sample n, the counter value Vn is 4.
The counter has been incremented by 10. But the difference (Vn - Vn-1), will be something like 65529 (not sure of the exact value).

The only way I found to detect this overflow is to compare the difference value to a fixed value greater than the max increment (I choose 10000).
Do you know a solution to manage this overflow without comparing to this subjective value?

Here is a code example:

static sint32 overallCount = 0;
sint32 diff;
static sint16 previousValue = 0;
sint16 currentValue;

currentValue = sampleValue();

diff = ((sint32) currentValue) - previousValue;
if(diff > 10000) {
    diff -= 65536;
} else if ((-diff) > 10000) {
    diff += 65536;
}

overallCount += diff;

Upvotes: 8

Views: 7926

Answers (4)

David Winant
David Winant

Reputation: 832

Another option is to just keep track of the overflow count. Using uint16_t for the values,

if (currentValue < previousValue) overflows++;

Then to get a 32-bit value, you combine overflows with currentValue.

result = currentValue | (overflows << 16);

Upvotes: 1

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215387

My previous answer had some mistakes so I've rewritten it, but the idea is the same, use unsigned types correctly.

Make currentValue and previousValue both unsigned integers of the chosen size (e.g. uint16_t). Then merely subtract them. Since the difference will be implicitly promoted to int if int is a larger type than uint16_t, you'll need to cast or implicitly convert the result back to uint16_t. So:

static uint16_t previousValue;
uint16_t currentValue = sampleValue();
uint16_t diff = currentValue - previousValue;

This makes use of the implicit conversion in the assignment, but you could cast if you like.

Upvotes: 8

diciu
diciu

Reputation: 29333

You could try using a Kalman filter to detect the overflow.

Upvotes: -1

Jonathan
Jonathan

Reputation: 13624

Here are a few ideas for you:

  1. Do the addition in a 32-bit field, and verify that the result will fit into the 16-bit field afterwards.
  2. Test whether the old value's high bit changed as a result of the addition.
  3. Do the addition in assembly, and check the carry flag afterwards.

Upvotes: 1

Related Questions