When using the optimizer, in a loop like the following one:

uint8_t flag;
...
ISR(SOME_vect) {
 flag = 1;
}
...

       while (flag == 0) {
               ...
       }

the compiler will typically access flag only once, and optimize further accesses completely away, since its code path analysis shows that nothing inside the loop could change the value of flag anyway. To tell the compiler that this variable could be changed outside the scope of its code path analysis (e. g. from within an interrupt routine), the variable needs to be declared like:

volatile uint8_t flag;

Back to FAQ Index.