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;
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.
Here are a few ideas for you:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With