[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] BUG? Comparing of words error.
From: |
Dimitar Dimitrov |
Subject: |
Re: [avr-gcc-list] BUG? Comparing of words error. |
Date: |
Wed, 18 Jan 2006 23:02:38 +0200 |
User-agent: |
KMail/1.9.1 |
On 18.1.2006 16:47, Flemming Steffensen (sent by Nabble.com) wrote:
> unsigned short RotCount;
>
> SIGNAL(SIG_INTERRUPT0){
> RotCount++;
> }
>
> void test(void){
> while (1){
> RotCount = 0;
> while (RotCount < 1000) {}
> RotCount = 0;
> while (RotCount < 2000) {}
> }
> }
I noticed two problems with this code:
1) RotCount should be declared volatile. See AVR-Libc FAQ #1 for details.
2) Operations with two byte variables (RotCount in this example) are NOT
atomic. Think what will happen if the interrupt triggers between comparing
the two RotCount bytes. The first compare will be using the old value of
RotCount, whereas the second compare will use the new one.
I suggest a program like this:
-------
#define enable_int0() (GICR |= (1<<INT0))
#define disable_int0() (GICR &= ~(1<<INT0))
volatile unsigned short RotCount;
SIGNAL(SIG_INTERRUPT0){
RotCount++;
}
void test(void){
unsigned short temp;
while (1){
enable_int0();
RotCount = 0;
disable_int0();
do {
enable_int0();
temp = RotCount;
disable_int0();
} while (temp < 1000);
enable_int0();
RotCount = 0;
disable_int0();
do {
enable_int0();
temp = RotCount;
disable_int0();
} while (temp < 2000);
}
}
---------
Hope that helps.
- Re: [avr-gcc-list] BUG? Comparing of words error., (continued)
Re: [avr-gcc-list] BUG? Comparing of words error., Francisco Silva, 2006/01/18
RE: [avr-gcc-list] BUG? Comparing of words error., Dave Hansen, 2006/01/18
Re: [avr-gcc-list] BUG? Comparing of words error., Dave Hylands, 2006/01/18
Re: [avr-gcc-list] BUG? Comparing of words error., Albert Andras, 2006/01/18
Re: [avr-gcc-list] BUG? Comparing of words error., Graham Davies, 2006/01/18
Re: [avr-gcc-list] BUG? Comparing of words error., Markus Baertschi, 2006/01/18
[avr-gcc-list] Re: BUG? Comparing of words error., Volkmar Dierkes, 2006/01/18
Re: [avr-gcc-list] BUG? Comparing of words error.,
Dimitar Dimitrov <=