// A test for the Discord channel.
// Original: https://wokwi.com/projects/432367544589072385
// Removed everything except the timer with interrupt.
//
// Timer1 runs, interrupts works, all well.
// It is a 1Hz timer, usually a 1kHz (1000Hz)'tick' is used
// in a embedded system.
volatile unsigned int counter;
void setup()
{
Serial.begin(115200);
Serial.println("Test timer with interrupt");
setupTimer1();
}
void setupTimer1()
{
cli();
TCCR1A = 0;
TCCR1B = 0;
OCR1A = 15624; // Compare match register (1 Hz)
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12) | (1 << CS10); // Prescaler 1024
TIMSK1 |= (1 << OCIE1A); // Enable Timer1 compare interrupt
sei();
}
void loop()
{
// This is not 100% safe, since an interrupt might
// happen between retrieving the two bytes
// of the variable.
Serial.print(counter);
Serial.print(", ");
delay(100);
}
//ISR
ISR(TIMER1_COMPA_vect)
{
counter++;
}