Is there a good way of implementing communication between an ISR and the rest of the program for an embedded system which avoids global variables?
It seems that the general pattern is to have a global variable which is shared between the ISR and the rest of the program and used as a flag, but this use of global variables goes against the grain to me. I've included a simple example using avr-libc style ISRs:
volatile uint8_t flag;
int main() {
...
if (flag == 1) {
...
}
...
}
ISR(...) {
...
flag = 1;
...
}
I can't see away around what is essentially a scoping issue; any variables accessible by both the ISR and the rest of the program must inherently be global, surely? Despite this, I've often seen people say things along the lines of "global variables are one way of implementing communication between ISRs and the rest of the program" (emphasis mine), which seems to imply that there are other methods; if there are other methods, what are they?
Best Answer
There is a de facto standard way to do this (assuming C programming):
extern
keyword or simply by mistake.static
. Such a variable is not global but restricted to the file where it is declared.volatile
. Note: this does not give atomic access or solve re-entrancy!