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.
_______________________________________________
AVR-GCC-list mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list