Arduino uno PWM CTC mode - strange behaviour on interrupt i have this simple code that i use for generating a square wave signal of 125 KHz on pin 9 using Timer1 in the "Clear Timer on Compare Match (CTC) Mode".
I also use Timer1 Compare Match A interrupt for toggling pin 7 of PORTD.

void setup() {
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);

TCNT1 = 0;
TCCR1A = 0;
TCCR1B = 0;

OCR1A = 7;
TIMSK1 = _BV(OCIE1A);
TCCR1A = _BV(COM1A0);
TCCR1B = _BV(CS11) | _BV(WGM12);
}

void loop() {
}

ISR(TIMER1_COMPA_vect)       
{
  PORTD = PORTD ^ 128;
}


It happens that


The 125KHz waveform on pin 7 is delayed by 1us with respect to the waveform of pin 9. I think that is related to the time that the microcontroller takes to "entering" the interrupt function and toggling the PORTD pin (1us are 16 clock cycle of the 16MHz master clock).
Sometimes the waveform on pin 7 (the one generated inside the interrupt service routine) has some "gaps". For example, this is the waveform acquired with my scope:Why this strange behaviour happens?


Thank you
This isn't the only ISR in the Arduino, there is at least the one counting millis. Counter 0 overflow ISR is not as short as yours, so every 1024us it'll cause some jitter (or as in your case, skipping the edge and basically inverting the phase). It'd be also several clock cycles delayed (against HW generated signal). Pushing and poping the registers into the stack are the deterministic delays, but the latency between setting the ISR flag and calling the ISR routine depends on currently executed instruction (some of them takes 2cycles, and there are also longer ones). 

There is not much time to do anything else either. Your ISR might take less than 20 cycles, but you only have 64 cycles (ISR is called at 250kHz rate so it generates 125kHz signal).
So if anything else prevents your ISR to be called (another ISR, disabled interrupts), you can miss them, as there is only one interrupt flag.

If you want the same waveform on pin 7, just use a wire and let the pin mode as INPUT.

BTW: You don't have to use PORTB = PORTB ^ _BV(PB7);, the PINB = _BV(PB7) toggles PB7 without read-modify-write access.