Premature CTC Interrupt Problem

Hello, I am trying to be able to re-enable and disable an interrupt on runtime. However very first interrupt is triggered prematurely. The code running on a nano. I don't use any gpio pin except 5v and gnd. I am powering it externally and connected to it through usb(5v cutted off). I couldn't understand the reason. Maybe someone fimiliar with? Thanks in advance!

I know that it is not wise to use Serial in ISR but the situation is still the same without them(+ micros) . Just to show the problem better.
Code:

long isr_set_time = 0;

ISR(TIMER1_COMPA_vect)
{
  Serial.print("ISR TRIGGERED in ");
  Serial.print(micros() - isr_set_time);
  Serial.println("us.");
  cli();
  TCCR1A = 0;
  TCCR1B = 0;
  OCR1A = 0;
  TIMSK1 = 0;
  TIMSK1 |= (0 << OCIE1A);
  sei();
  Serial.println("ISR ENDED");
}

void setup() {
  Serial.begin(9600);
  delay(100);
}

void loop() {
  if(Serial.available() > 0)
    if((char)Serial.read() == 't')
    {
      Serial.println("configuring isr");
      cli();
      TCCR1A = 0;
      TCCR1B = 0;
      TIMSK1 = 0;
      OCR1A = 25000;
      TCCR1B |= (1 << WGM12);
      TCCR1B |= (1 << CS12) | (1 << CS10);
      TIMSK1 |= (1 << OCIE1A);
      isr_set_time = micros();
      sei();
      Serial.println("configuring isr done");
    }
}

Exmaple Output:

->t
configuring isr
ISR TRIGGERED in 132us.
ISR ENDED
configuring isr done
->t
configuring isr
configuring isr done
ISR TRIGGERED in 1590696us.
ISR ENDED
->t
configuring isr
configuring isr done
ISR TRIGGERED in 1599680us.
ISR ENDED
->t
configuring isr
configuring isr done
ISR TRIGGERED in 1599700us.
ISR ENDED

The timer setup seems nonsensical. This does nothing at all:

  TIMSK1 |= (0 << OCIE1A);

Here is a working example to set up AVR TIMER1 for 100 Hz interrupts:

#define SAMPLE_HZ 100
void initTimer() {                                 // set up 100Hz interrupt
  // Start timer1 for periodic interrupt
  noInterrupts();
  TCCR1A = 0;   //essential to clear this first with Arduino!
  TCCR1B = 0;
  TCNT1  = 0;
  OCR1A = F_CPU / 256 / SAMPLE_HZ;  
  TCCR1B |= (1 << WGM12);   // CTC mode
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt
  interrupts();
}



ISR(TIMER1_COMPA_vect) {  // called every 10mS
  flag = 1;  //set external flag
}

You MUST not print, or do any serial I/O in an interrupt routine. Interrupts are off, and communications generally depend on interrupts. You SOMETIMES get away with serial printing in Arduino, because the system programmers put in a kluge that allows it to work, a little bit.

TIMSK1 |= (0 << OCIE1A);

To disable timer1 interrupt until re-enabled again, maybe with different frequency.

The code basicly sets one time alarm after receiving 't' from serial.

Nope.

This disables the interrupt:

TIMSK1 &= ~(1<<OCIE1A);
or
TIMSK1 = 0;

The situation still the same...

During timer setup, clear the pending interrupt flag by writing a 1 to it (see the data sheet).

TIFR1 = (1<<OCF1A);

Yes. This solved the issue! Many thanks! Here is final code:

long isr_set_time = 0;
long isr_end_time = 0;
bool flag = 0;
ISR(TIMER1_COMPA_vect)
{
  flag = 1;
  isr_end_time = micros();
  cli();
  TCCR1A = 0;
  TCCR1B = 0;
  OCR1A = 0;
  TIMSK1 = 0;
  sei();
}

void setup() {
  Serial.begin(9600);
  delay(100);
}

void loop() {
  if(Serial.available() > 0)
    if((char)Serial.read() == 't')
    {
      flag = 0;
      isr_set_time = micros();
      cli();
      TCCR1A = 0;
      TCCR1B = 0;
      TIMSK1 = 0;
      OCR1A = 25000;
      TCCR1B |= (1 << WGM12);
      TCCR1B |= (1 << CS12) | (1 << CS10);
      TIFR1 = (1<<OCF1A);
      TIMSK1 |= (1 << OCIE1A);
      sei();
      while(!flag)
          __asm__("nop\n\t");
      Serial.print("interrupt done in:");
      Serial.print(isr_end_time - isr_set_time);
      Serial.println(" us.");
    }
}
->t
interrupt done in:1591064 us.
->t
interrupt done in:1600056 us.
->t
interrupt done in:1600068 us.

@anon94801460 I have a question, in datasheet I see two compare register, OCR1A and OCR1B. Is it possible to use two different frequencies on them, with same prescaler, and using TIMER1_COMPAB_vect too?

I don't think so. CTC timing mode uses just OCR1A or ICR1 for the comparison.

Usually the two compare registers are used for two separate and independent PWM outputs, with different duty cycles but the same period.