I need to deal with a counter that gives me ticks for my application. The counter is 32bits so what i need to know is how to deal with it when it wraps. for example:
I h
It will not matter so long as the difference between the start and end count is less than (2^32)/2, and assuming 2's complement 32bit arithmetic is performed (almost universally true), even if the count value spans the wrap-point. For example:
Start count: 0xfffffff
End Count: 0x00000002 (incremented through 0,1,2 - i.e. three counts)
End - Start == 0x00000002 - 0xfffffff == 0x00000003
So the right answer is achieved so long as the counter is the bit width of a built-in integer type, and that type is used. Where perhaps a counter register is not the width of a built-in integer type, you can achieve the same effect by masking the higher order "overflow" bits.
If you need the larger count for other reasons or if the difference between successive timestamps is too large, then you can simply use another integer that is incremented when the lower-order counter wraps. This integer will form the high order bits of a larger integer, so the LSB of the second integer is the 33rd bit of this larger integer.