user12039: I am intending to write a code where a user will send lines through serial to affect timer interrupts. I am noticing a problem with serial where as soon as I enable the timer interrupts, the serial communication becomes garbled. More specifically, as soon as I enable the timer interrupts and I open the serial monitor, the monitor gets flooded with garbage. I haven't written too much code and this is already happening. Am I making a silly mistake? I am using an Arduino Mega 2560. My code is below:
void setup() {
  Serial.begin(9600);
  Serial.println("Hello World!");
  timer_setup();
}

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

void timer_setup() {
  // waveform generation = 0100 = CTC
  TCCR1B &= ~(1 << WGM13);
  TCCR1B |=  (1 << WGM12);
  TCCR1A &= ~(1 << WGM11);
  TCCR1A &= ~(1 << WGM10);
  // output mode = 00 (disconnected)
  TCCR1A &= ~(3 << COM1A0);
  TCCR1A &= ~(3 << COM1B0);
  // Set the timer pre-scaler
  TCCR1B = (TCCR1B & ~(0x07 << CS10)) | (2 << CS10);
  OCR1A = 0x4000;
  TCNT1 = 0;
  // Enable timer interrupt mask 1
  TIMSK1 |= (1<<OCIE1A);
}

If I comment out the TIMSK1 |= (1<<OCIE1A) line, serial doesn't mess up. 

user10794: You've enabled interrupts for Timer 1 Compare A interrupt. Where is the interrupt handler?
For example:
ISR(TIMER1_COMPA_vect)
{
  // whatever
}  // end of TIMER1_COMPA_vect

Without that, the processor is just restarting. No wonder serial output is messed up.

12039: <p>I am intending to write a code where a user will send lines through serial to affect timer interrupts. I am noticing a problem with serial where as soon as I enable the timer interrupts, the serial communication becomes garbled. More specifically, as soon as I enable the timer interrupts and I open the serial monitor, the monitor gets flooded with garbage. I haven't written too much code and this is already happening. Am I making a silly mistake? I am using an Arduino Mega 2560. My code is below:</p>

<pre><code>void setup() {
  Serial.begin(9600);
  Serial.println("Hello World!");
  timer_setup();
}

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

void timer_setup() {
  // waveform generation = 0100 = CTC
  TCCR1B &amp;= ~(1 &lt;&lt; WGM13);
  TCCR1B |=  (1 &lt;&lt; WGM12);
  TCCR1A &amp;= ~(1 &lt;&lt; WGM11);
  TCCR1A &amp;= ~(1 &lt;&lt; WGM10);
  // output mode = 00 (disconnected)
  TCCR1A &amp;= ~(3 &lt;&lt; COM1A0);
  TCCR1A &amp;= ~(3 &lt;&lt; COM1B0);
  // Set the timer pre-scaler
  TCCR1B = (TCCR1B &amp; ~(0x07 &lt;&lt; CS10)) | (2 &lt;&lt; CS10);
  OCR1A = 0x4000;
  TCNT1 = 0;
  // Enable timer interrupt mask 1
  TIMSK1 |= (1&lt;&lt;OCIE1A);
}
</code></pre>

<p>If I comment out the <code>TIMSK1 |= (1&lt;&lt;OCIE1A)</code> line, serial doesn't mess up. </p>
 10794: <p>You've enabled interrupts for Timer 1 Compare A interrupt. Where is the interrupt handler?</p>

<p>For example:</p>

<pre><code>ISR(TIMER1_COMPA_vect)
{
  // whatever
}  // end of TIMER1_COMPA_vect
</code></pre>

<p>Without that, the processor is just restarting. No wonder serial output is messed up.</p>
