I'm having a hard time wrapping my head around what I'm trying to accomplish....
I'm using Timer1 in Input Capture Mode, to time an external event. I clear the timer, set it up in Input Capture Mode, set the pre-scaler to ClkIO/1024, and enable Overflow interrupts, as the event I'm timing can take up to 10 seconds, which means the timer may overflow twice. I maintain a software count, in a uint32_t that is increment by 65536 every time an overflow occurs. When the capture occurs, I can then read the timer count, add it to the software count, and have the total event time. What has me a bit stumped is how to handle the corner cases where the overflow and capture occur at nearly the same time. How do I know whether the capture occurred before or after the overflow? Consider this scenario:
Capture occurs at a count of 0xffff. Overflow interrupt occurs AFTER the capture, so the overflow handler SHOULD NOT increment the software count to account for the overflow.
Or, capture occurs at a count of 0x0000. Overflow interrupt occurs AFTER the capture, so the overflow handler SHOULD increment the software count to account for the overflow.
I think the correct logic would then be:
- Do NOT update the software count if TIFR1 ICF1 is set, AND Timer1Count is less than perhaps 100
- DO update the software count if TIFR1 ICF1 is clear OR Timer1Count is greater than perhaps 100
Is that correct and robust?
Regards,
Ray L.