Nick Gammon's frequency counter sketches (Gammon Forum : Electronics : Microprocessors : Timers and counters) got me interested in using the Arduino's Timers/Interrupts and I ran into some unexpected behavior. The following code is the bare bones (or close to it) of what it takes to repeat the behavior. I first noticed that a Serial.print in void setup() was repeating after I had introduced code in order to use Timer 1 in CTC mode. After deleting the code that did not seem to affect this behavior, I added some comments about observations made while commenting and un-commenting one line at a time. Actually, the code in void setup() was not repeating, rather it was restarting or resetting without executing the counter++ that I added to debug. Adding the delay() after the Serial.begin() seems to have ended the behavior but I have no idea why. I would much appreciate anyone's insight into the relationship between the timer registers and the serial library and what would cause the Arduino to reset rapidly. I replicated the behavior in an UNO R3 and a Mega2560(with timer 3 instead of timer 1). Thank you.
boolean notFinished = true;
int counter = 0;
void setup()
{
Serial.begin(9600); // Changing baud to 115200 repeated gibberish instead of "Counter"
//delay(100); //Un-commenting this line stops repeat but prints gibberish
Serial.print("Counter ");
//delay(100); //Un-commenting this as well, stops repeat and prints properly(only once)
Serial.println(counter); //Added for debugging
//delay(100); //Un-commenting this line repeats and prints gibberish with no new line
counter++; //Added for debugging. With no delays, counter does not increment...resetting.
}
void loop()
{
if(notFinished){
notFinished = false;
setupTimer();
}
}
// Why would this result in repetition of void setup()?
void setupTimer()
{
TCCR1A = 0; //Commenting out this line speeds up the repeat
TCCR1B = 0; //Commenting out this line stops repeat
OCR1A = 1846; //Commenting out this line stops repeat
TCCR1B |= (1 << WGM12); // Commenting out this line slows down the repeat
TCCR1B |= (1 << CS10);// Commenting out this line speed up repeat
TCCR1B |= (1 << CS12); //Commenting out this line speeds up the repeat of void setup()
TIMSK1 |= (1 << OCIE1A);// Commenting out this line stops repeat
}