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:
- 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 - It then proceeds to void loop() for main program.
Problem:
- 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 - The arduino IDE is 1.8.5, the port is com3, the driver is USB-Serial CH340 (COM3),baud is 19200
- If I disable the program step
"TIMSK2 |= (1 << OCIE2A);"
The serial link works ok and output is "Serial link activated........" - 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
- Can you please assist on this matter.
- Why is timer2 setup affecting serial link?
Regards