Conflict between Serial and Timer 2

I kindly request assistance on the following program:

void setup() {

//-----------set timer2 ctc mode---------------
cli(); //disable all interrupts

// initialize
TCCR2A = 0; //timer/counter2 control register A
TCCR2B = 0; //timer/counter2 control register B
TCNT2 = 0; //timer/counter2 register

OCR2A = 50; //set this value to give a waveform of frequency =
// 16MHz/2/prescaler/(OCR2A value +1)
//16MHz/2/1024/(50+1) 153.186Hz = 6.528msec
TCCR2A |= (1 << WGM21); //set CTC mode
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20); //set prescaler to 1024
TIMSK2 |= (1 << OCIE2A); //enable timer compare interrupt for counter A

sei(); //enable all interrupts

//---------end of timerr2 setting-------------

//----------Set up serial link------------------
Serial.begin(19200); //set serial link baud rate
while (!Serial) {;} //wait for serial port to connect.
// Needed for native USB port only
Serial.println();
Serial.println(F("Serial link activated........")); //acknowledge serial link is working
//----------------------------------------------

}

void loop() {;}

Program Explanation:

  1. In void setup()
    a) It sets up timer2 in ctc mode
    b) It checks if serial link is working and outputs a prompt "Serial link activated..."
    when link is established
  2. It then proceeds to void loop() for main program.

Problem:

  1. When program is run, the serial link mis-behaves and I get serial data as shown in following
    Seria
    Seri
    Seri
    Seria
    Seri
    Seria
    etc
  2. The arduino IDE is 1.8.5, the port is com3, the driver is USB-Serial CH340 (COM3),baud is 19200
  3. If I disable the program step
    "TIMSK2 |= (1 << OCIE2A);"
    The serial link works ok and output is "Serial link activated........"
  4. This problem has happened recently. Program was running ok before. The only thing I can think
    of that has changed is I updated arduino IDE to 1.8.5 and updated the libraries.

Questions

  1. Can you please assist on this matter.
  2. Why is timer2 setup affecting serial link?

Regards

You need an ISR to go with the enabled interrupt

TIMSK2 |= (1 << OCIE2A);                         //enable timer compare interrupt for counter A
ISR(TIMER2_COMPA_vect)
{}

Without an interrupt routine I believe there is a default jump to the beginning of the program rather than to the ISR.

I'm not certain why this would have worked with a previous version of the ide.

CattleDog,

Thank you for your reply.

I had been working on this part of the program and had separated from my
full program to test it. This is when the problem started. I had not kept the ISR.
That is reason why it was working before.

Silly mistake.

Thank you again for your help.

What happens with the interrupt- it's just a jump to the vector table. Without the ISR in your code the compiler will not insert a jmp instruction into the table and simply treat it as program memory.

"If the program never enables an interrupt source, the Interrupt Vectors are not used,
and regular program code can be placed at these locations."

Your jmp to the vector table will then just process the next location as the next instruction on which case you have NFI what will happen. Bottom line, if you enable an interrupt, make sure you have the ISR, even if it's empty.