avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [avr-gcc-list] BUG? Comparing of words error.


From: Dave Hansen
Subject: RE: [avr-gcc-list] BUG? Comparing of words error.
Date: Wed, 18 Jan 2006 14:13:35 -0500

From: "Flemming Steffensen (sent by Nabble.com)" <address@hidden>
[...]

I noticed the problem using this code:
------------

unsigned short RotCount;

SIGNAL(SIG_INTERRUPT0){
        RotCount++;
}

void test(void){
        while (1){
                RotCount = 0;
                while (RotCount < 1000) {}
                RotCount = 0;
                while (RotCount < 2000) {}
        }
}
------------

My problem is that the two inner whiles sometimes exists prematurly.

You have two problems.

First, RotCount should be declared volatile, since it's shared between main-line code and an ISR. This will prevent the compiler from optimizing access to RotCount away completely inside the loop.

Second, and the one whose symptoms you're seeing, RotCount is a 16-bit value. Occasionally, you're getting an interrupt between the comparisons of the first and second bytes.

You need to protect access to RotCount from interruption. For example, you could change the first loop to something like

  unsigned short temp;
  do
  {
     Disable();   // disable interrupts
     temp = RotCount;
     Enable();   // Re-enable interrupts

  } while (temp < 1000);

You of course need to do something similar for the second loop, and whenever you write RotCount as well.

HTH,
  -=Dave






reply via email to

[Prev in Thread] Current Thread [Next in Thread]