void init_port();
void init_timer();
void setup() {
init_port();
init_timer();
sei(); // Enable global interrupts
}
void loop() {
// Main code, can be left empty as the ISR handles the LED toggling
}
void init_port() {
DDRD |= (1 << PD4); // Set PD4 as an output pin
}
void init_timer() {
// Configure Timer1 in CTC mode (WGM12 set, WGM10 and WGM11 cleared)
TCCR1A = 0; // Clear WGM11 and WGM10, no need to set these in CTC mode
TCCR1B = (1 << WGM12); // Set WGM12 for CTC mode
// Set prescaler to 1024
TCCR1B |= (1 << CS12) | (1 << CS10);
// Set the compare value for 1 second at 16 MHz clock
OCR1A = 15624;
// Enable Timer1 compare match interrupt
TIMSK1 = (1 << OCIE1A);
// Clear the timer counter
TCNT1 = 0;
}
// ISR for Timer1 Compare Match A
ISR(TIMER1_COMPA_vect) {
PORTD ^= (1 << PD4); // Toggle PD4 (LED)
}