void setup() {
DDRB |= B00100000; // Set pin 13 (PB5) to output using direct port manipulation
// Equivalent to pinMode(13, OUTPUT), but faster and more efficient.
cli(); // Disable global interrupts during timer setup
TCCR1A = 0; // Clear Timer1 control register A
TCCR1B = 0; // Clear Timer1 control register B
OCR1A = 15624; // Set Compare Match value for 1-second intervals
// Calculation:
// Target time = 1 second
// Prescaler = 1024
// Timer frequency = 16 MHz / 1024 = 15625 Hz
// OCR1A = (Target time × Timer frequency) - 1 = (1 × 15625) - 1 = 15624
TCCR1B |= (1 << WGM12); // Configure Timer1 in CTC (Clear Timer on Compare Match) mode
TCCR1B |= (1 << CS10); // Set prescaler bits CS10 and CS12 for 1024 prescaler
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A); // Enable Timer1 Compare Match A interrupt
sei(); // Re-enable global interrupts
}
void loop() {
// Main program code can run here, unaffected by the LED blinking
}
ISR(TIMER1_COMPA_vect) {
PORTB ^= B00100000; // Toggle pin 13 using XOR
}