My question is again about modifying variables inside the ISR. If the variable used only inside of ISR, I don't have to declare it as volatile, right? After some researching, I figured out that variable need to be declared as static, and inside of ISR function. But it also didn't help.
I'm using Arduino Ide, so don't have many debugging options. I'm using an LED indicator for checking if the variable has incremented, as you can see below:
ISR(TIMER2_COMPA_vect) {
static unsigned int count20ms = 0;
count20ms += 5;
PORTD ^= (1 << PD6); // Indicates that ISR executes
if (count20ms == 10) {
PORTD ^= (1 << PD7); // Indicates that counter incremented
count20ms = 0;
}
}
I can observe that ISR executes through a blinking LED on PD6. But Condition count20ms == 10 is never true . And thus, the LED connected to the PD7 never blinks.
How should a static variable behave ?
Best Answer
Folk on avrfreaks.net answered to me
I didnt't provide interrupt handler for interrupts I had enabled. AVR compiler's default action to this situation is reset.
Setup code, where TIMSK2 is responsible for enabling interrupts:
Enabling 3 Interrupts vectors, but providing only one handler. That caused AVR to reset.
More about AVR interrputs here