[chbot] Synco's reentrant chocolate fish

Peter Harris petes.username at gmail.com
Fri Jul 22 22:15:40 BST 2011


Hi Synco
Welcome to the joys of reentrancy.
For those not familiar with this problem: The variable 'count' 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.

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.

  Peter

#define TIMERWRAP     1024
static uint number_of_times_the_timer_has_overflowed_during_pulse = 0;

ISR(ExternalInterrupt)  // highest priority
{
   if (triggered on +ve edge) {
      // Start of pulse detected
      while (number_of_times_the_timer_has_overflowed_during_pulse > 0) //
The while loop will catch an overflow occurring at the start of the pulse
      {
         number_of_times_the_timer_has_overflowed_during_pulse = 0;
         set to trigger on -ve edge;
         count = TIMERWRAP - Timer;
      }

   }
   else {
       // end of pulse
       do
       {
          temp_overflow_count =
number_of_times_the_timer_has_overflowed_during_pulse;
          set to trigger on +ve edge;
          count += Timer +(temp_overflow_count * TIMEWRAP);
       } 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
   }
}

ISR(TimerOverflow)   // overflows at TIMERWRAP ticks
{
   number_of_times_the_timer_has_overflowed ++;
   ....
}

ISR(other)
{
  ...
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ourshack.com/pipermail/chchrobotics/attachments/20110723/e3e25727/attachment.htm 


More information about the Chchrobotics mailing list