Hello,
You would need to take into account timer overflows only If the pulse width to be measured exceeds the timer overflow period. For pulse widths less than the timer overflow period, a simple 16-bit subtraction of the two values would suffice.
If the two timer values have an intervening timer overflow, the second value would be less than the first value, but the first value would still need to be subtracted. The correct value will be obtained if the result is simply considered unsigned. For assembler this is straightforward - not sure of the implications for C code.
The pulse duration measurement getting "out of step" shouldn't be a problem provided the input capture is set up to occur on either a positive or a negative edge - but not for the timer mode to capture on either edge. The opposite edge for the next capture should be set from within the ISR for the timer channel.
Regards,
Mac
Message Edited by bigmac on 2006-07-05 12:20 PM
Hi guys.
Thanks for the help. Nice to know that I'm at least on the right track.
My code is set to trigger on both edges , but I do check the current pin condition
to make sure I know which edge has occured. The pulse widths are in the order of about 12ms and shorter. The prescalar is set such that the timer would overflow only after about 20ms. I currently take 1 reading on the rising edge and one on the falling edge. If the second reading is bigger than the first I subtract the 2 to get a result. If it's smaller I assume the counter has rolled over and I subtract the first result from 65535 and add the second reading.
Is there a better weay to do this.
Cheers
Seegoon
Thanks for all the help guys. Think I've got it sorted now.
Now I need to figure how to save things to internal eeprom/ flash. A quick look through the datasheet shows that it looks to be quite a task :0(
Oh well.
Thanks again.
Cheers
Seegoon