Hi Col et Al<br>The size of number_of_times_the_timer_has_overflowed_during_pulse is irrelevant as the multi-read trick will catch overflows.<br><br>There are a couple of critical bugs in my original code which are fixed below. There are also a couple of optimisations.<br>
<br> Peter<br><br>#define TIMERWRAP 1024<br>
static uint number_of_times_the_timer_has_overflowed_during_pulse = 0;<br><div id=":h8">
<br>
ISR(ExternalInterrupt) // highest priority<br>
{<br>
if (triggered on +ve edge) {<br>
// Start of pulse detected<br>
do <br>
{<br>
number_of_times_the_timer_has_overflowed_during_pulse = 0;<br>
count = Timer;<br>
}while (number_of_times_the_timer_has_overflowed_during_pulse > 0); <br> //The while loop will catch an overflow occurring at the start of the pulse<br> // FIX Changed this to a do while so that count will always be set. Previously it would only work after an overflow <br>
<br>
count = TIMERWRAP - count; // This only needs to be done once.<br>
set to trigger on -ve edge; // This only needs to be done once.<br>
<br>
}<br>
else {<br>
// end of pulse<br>
do<br>
{<br>
temp_overflow_count = number_of_times_the_timer_has_overflowed_during_pulse;<br> temp_timer = Timer;<br>
} while (temp_overflow_count !=
number_of_times_the_timer_has_overflowed_during_pulse); <br> // The while loop
will catch an overflow occurring at the end of the pulse<br><br> count += temp_timer +(temp_overflow_count * TIMEWRAP);<br> // FIX moved count calculation out of the while loop so it will only be updated once.<br>
<br> set to trigger on +ve edge; // This only needs to be done once too<br>
}<br>
}<br>
<br>
ISR(TimerOverflow) // overflows at TIMERWRAP ticks<br>
{<br>
number_of_times_the_timer_has_overflowed ++;<br>
....<br>
}<br>
<br>
ISR(other)<br>
{<br>
...<br>
}</div><div style="visibility: hidden; left: -5000px; position: absolute; z-index: 9999; padding: 0px; margin-left: 0px; margin-top: 0px; overflow: hidden; word-wrap: break-word; color: black; font-size: 10px; text-align: left; line-height: 130%;" id="avg_ls_inline_popup">
</div>