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
lets assume the counter counts down (many count down to save on gates in the logic).
you need to first know the period of time it takes to get to 2^32 ticks and need to insure that you are well oversampling that.
If you want to find the time period between two events, say start and end
start = read timer lasttime = start rollover = 0
while waiting for thing to happen
nowtime = read timer if(nowtime>lasttime) rollover+=1 (this is a down counter) lasttime = nowtime
event happens: end = read timer
total time = start - end (this is a down counter and note that this math works even when rolling over)
total time = total time/ scaling factor to get from ticks to seconds, minutes, whatever total time += rollover * seconds/minutes/whatever per 2^32 counts
if you have an up counter then nowtime
If you can guarantee that your event will happen within 2^32 counts you dont need to do the rollover now time last time thing you only need start and end and the total ticks = start - end will work even if the counter rolls from 0x00000000 to 0xFFFFFFFF between start and end.