I read the documentation about interrupts in Arduino and there is this example code:
int pin = 13;
volatile int state = LOW;
void setup() {
pinMode(pin, OUTPUT);
attachInterrupt(0, blink, CHANGE);
}
void loop() {
digitalWrite(pin, state);
}
void blink() {
state = !state;
}
Well, it is not good example, but for my question is good enough.
What if in function blink()
is some non-atomic operation. Let's say, I have to change several variables, call some function, etc. Should I call noInterrupts()
before, or are the interrupts disabled automatically?
void blink() {
noInterrupts(); // Disable interrupts
// some complex work, here.
interrupts(); // Enable interrupts
}
I looked into hardware/arduino/cores/arduino/WInterrupts.c
in my Arduino IDE directory, but I cannot find any information there.
And one more thing: Why should I declare any variable in interrupt routine as volatile
?