input capture both leading and trailing edges?

I was trying to understand the timer1 register input capture stuff from the datasheet, and it appears that the capture mode has to be set to see either the leading or trailing edge. I was just wondering if anyone knows if it is valid to toggle that bit while in the interrupt routine so you can capture times for both the leading and trailing edges of pulses? (I've been wondering if this would be an even more accurate way to receive IR pulses than using a CHANGE interrupt on pin 2 or 3). I'm also not entirely clear if the input capture can reset the timer and start counting from zero again (which would be nifty, since I wouldn't have to worry about the time wrapping around - the capture register would always have the actual duration of the pulse recorded).

Of course, I'm sure I don't actually need more accuracy, but it is fun to investigate :-).

Claghorn:
I was just wondering if anyone knows if it is valid to toggle that bit while in the interrupt routine so you can capture times for both the leading and trailing edges of pulses?

Sneaky :wink:

Actually the datasheet recommends just that, in order to measure duty cycle. I didn't see where input capture had any functionality to reset the counter. Not sure how it would do that actually, it can't happen at the time of the interrupt. I suppose the ISR could clear it, but there's little reason to do so, in fact that might introduce inaccuracies. I'd just save the successive captured values, and subtract them. Being unsigned numbers, rollover is not an issue.

16.6.3 Using the Input Capture Unit
The main challenge when using the Input Capture unit is to assign enough processor capacity
for handling the incoming events. The time between two events is critical. If the processor has
not read the captured value in the ICR1 Register before the next event occurs, the ICR1 will be
overwritten with a new value. In this case the result of the capture will be incorrect.
When using the Input Capture interrupt, the ICR1 Register should be read as early in the interrupt
handler routine as possible. Even though the Input Capture interrupt has relatively high
priority, the maximum interrupt response time is dependent on the maximum number of clock
cycles it takes to handle any of the other interrupt requests.
Using the Input Capture unit in any mode of operation when the TOP value (resolution) is
actively changed during operation, is not recommended.
Measurement of an external signal’s duty cycle requires that the trigger edge is changed after
each capture. Changing the edge sensing must be done as early as possible after the ICR1
Register has been read. After a change of the edge, the Input Capture Flag (ICF1) must be
cleared by software (writing a logical one to the I/O bit location). For measuring frequency only,
the clearing of the ICF1 Flag is not required (if an interrupt handler is used).

Thanks. I guess I need to read the datasheet a dozen or so more times, I didn't pick up on it recommending that.

To get even sneakier, I was considering moving the TOP value around in front of the counter value to detect timer
overflow at the end of the IR sequence so I'll know when the final pulse showed up (I did see the datasheet warning about wrap around when you do that, but in this case wrap around is exactly what I'd want :-).

Claghorn:
Thanks. I guess I need to read the datasheet a dozen or so more times, I didn't pick up on it recommending that.

You're in good company! XD