Serial Print Stopped By timer2 Interrupt Setup and/or Wire.h Library

Hi,

I am trying to print debug messages in a sketch however only the first few letters of my first Serial.Print() comes through on the serial monitor. Through trial and error, I have removed sections of my sketch to cut it down whilst still reproducing the same situation.

(Cut down code:)

//Include associated libraries

#include <Wire.h>

void setup() {
  Serial.begin(38400); //Begin serial communications

  Serial.println("Mark0");
  Serial.println(F("Reciever Starting: "));

  delay(100);
  
  //Set up interrupts for timer 2 - Serial.prints stop here!
  noInterrupts();//Stop interrupts

  //set timer2 interrupt at 8kHz
  TCCR2A = 0;// set entire TCCR2A register to 0
  TCCR2B = 0;// same for TCCR2B
  TCNT2  = 0;//initialize counter value to 0
  // set compare match register for 10khz increments
  OCR2A = 49; // (16*10^6 / (32 * interruptFreq)) - 1 ...(must be < 256)
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS21 and CS20 bit for 32 prescaler
  TCCR2B |= (1 << CS21) | (1 << CS20);
  // enable timer compare interrupt
  TIMSK2 |= (1 << OCIE2A);

  interrupts();//Resume interrupts



  Serial.println("Mark2");
  delay(100);
  Serial.println("Mark3");
}

void loop() {
  // put your main code here, to run repeatedly:

}

The output at the serial monitor is this:

Mark0
Reciever Starting:

The interrupt setup is pretty much copied from this instructables.

I have played around and found that beginning serial communication after interrupt setup does not output anything, and that any serial.Print attempts after the interrupt setup do not show up. Additionally I have found that commenting out Wire.h causes the output to repeat itself infinitely despite being in void setup()?

Output with Wire.h commented out:

Mark0
Reciever Starting: 
�XSX®-j
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting: 
Mark0
Reciever Starting:

I have researched/read around, however all resources I have found focus on the actual interrupt (usually relating to trying to delay() or Serial.Print() in an interrupt), or where interrupts had been turned off.

Could anyone throw some light on why Wire.h and setting up interrupts on timer2 affect serial printing?

Thanks,
Chris

You are setting up interrupts for timer2 compare, but not defining the interrupt service routine so it's probably jumping off into unknown territory. Once you define this function, your code works.

ISR (TIMER2_COMPA_vect)
{
    // you code here
}

I just tried it with an empty interrupt and you're right it did work fine. I then copied in my interrupt from my 'full' sketch and it broke again, so I assumed I was crashing during/getting stuck in the interrupt.

Whilst trying to format this post, I realised the issue was I was trying to call an I2C function in an interrupt - whoops...

Anyway, thanks blh64 for the help!