4.8.6 Accessing Objects From Interrupt Routines
Reading or writing objects from interrupt routines can be unsafe if other functions access these same objects.
It is recommended that you explicitly mark objects accessed in interrupt
and main-line code using the volatile
specifier (see 4.3.8.2 Volatile Type Qualifier). The compiler will
restrict the optimizations performed on volatile
objects.
Even when objects are marked as volatile
, the compiler
cannot guarantee that they will be accessed atomically. This is particularly true of
operations on multi-byte objects.
Interrupts should be disabled around any main-line code that modifies an
object that is used by interrupt functions, unless you can guarantee that the access is
atomic. Macros are provided in <avr/atomic.h>
to assist you access
these objects.