In one file, define the variable outside the scope of any function (i.e., a global variable). Use the volatile
type qualifier to tell the compiler not to optimize away any reads because the value of this variable could change at unpredictable times (for example, in the interrupt handler).
volatile circular_buffer_type circularbuffer;
In the other file, declare the variable as extern
, again outside the scope of any function. This allows the other file to access the same variable defined in the first file. Alternately, you could make this declaration in an .h file included by the other file.
extern volatile circular_buffer_type circularbuffer;
All accesses to this variable in the main code should be atomic to prevent variable corruption. For example, if your main code does a read-modify-write and the interrupt occurs between the read and the write then the main code's write would overwrite any changes that occurred in the interrupt handler. To prevent this situation, you could disable interrupts before the read and re-enable interrupts after the write in order to make the read-modify-write access atomic.