Hi Synco<br>Welcome to the joys of reentrancy. <br>For those not familiar with this problem: The variable &#39;count&#39; is more than 8-bits long and is being dealt with on an 8-bit micro. Assuming count is 16 bits long the micro has to read first one byte then the other. This problem occurs when count is modified by an interrupt between reading the first and second bytes the value read is the first byte of the old value of count and the second byte of the new value of count E.g. garbage. <br>

<br>The following code works by reading values that may be changed more than once and making sure the same value is read twice. When an overflow occurs at one or other end of the pulse count may be off by a few ticks. If timing is critical you could count the number of times the while loops execute, a value greater than 1 indicates that an overflow has occurred. The value of count can be increased if there was an overflow at the start and decreased if there was one at the end to compensate.<br>

<br>  Peter<br><br>
#define TIMERWRAP     1024<br>
static uint number_of_times_the_timer_has_overflowed_during_pulse = 0;<br><br>
ISR(ExternalInterrupt)  // highest priority<br>
{<br>
    if (triggered on +ve edge) {<br>      // Start of pulse detected<br>      while (number_of_times_the_timer_has_overflowed_during_pulse &gt; 0) // The while loop will catch an overflow occurring at the start of the pulse<br>

      {<br>         number_of_times_the_timer_has_overflowed_during_pulse = 0;<br>
          set to trigger on -ve edge;<br>
          count = TIMERWRAP - Timer;<br>      }<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>
           set to trigger on +ve edge;<br>
           count += Timer +(temp_overflow_count * TIMEWRAP);<br>       } while (temp_overflow_count != number_of_times_the_timer_has_overflowed_during_pulse); // The while loop will catch an overflow occurring at the end of the pulse<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>
}<br><br><br>
<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>