Electrical – Modifiying variable inside of Interrupt vector. AVR

arduinoatmega328pavrinterruptsstatic

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

Blockquote Yup, thought so, you enable COMPA, COMPB and OVF interrupts but only provide ISR() for COMPA. As soon as the other two conditions occur they'll jump to _bad_interrupt and from there "JMP 0" so the AVR keeps "resetting".

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:

void setup() {
  DDRD = 0xff;
  TCCR2A = (1 << WGM12);            
  TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20); 
  TIMSK2 = 0x07;            // <<<<< Enabling 3 Interrupts vectors
  TCNT2 = 0x00;             
  OCR2A = 250;              
}

Enabling 3 Interrupts vectors, but providing only one handler. That caused AVR to reset.

More about AVR interrputs here