#include <avr/io.h>
#include <avr/interrupt.h>

#define LED PB5

int main(void) {
    DDRB |= _BV(LED);  // Set the LED pin as output

    // Configure Timer 0
    TCCR0B |= _BV(CS02) | _BV(CS00);  // Set prescaler to 1024
    TIMSK0 |= _BV(TOIE0);  // Enable Timer 0 overflow interrupt

    sei();  // Enable global interrupts

    while (1) {
        // Main loop does nothing, just waits for the interrupt
    }
}

ISR(TIMER0_OVF_vect) {
    PORTB ^= _BV(LED);  // Toggle the LED state
}



/*
#include <avr/io.h>
#include <avr/interrupt.h>

#define LED PB5  // Assuming LED at Arduino PIN D13

int main(void) {
    DDRB |= _BV(LED);  // Set the LED pin as output
    TCCR0B |= _BV(CS02) | _BV(CS00);  // Set prescaler for Timer 0 (e.g., 1024)
    TIMSK0 |= _BV(TOIE0);  // Enable Timer 0 overflow interrupt
    sei();  // Enable global interrupts

    while (1) {
        // Main loop does nothing, just waits for the interrupt
    }
}

ISR(TIM0_OVF_vect) {
    PORTB ^= _BV(LED);  // Toggle the LED state
}

*/




/* #include <avr/io.h>
#include <avr/interrupt.h>

#define LED_PIN PB5

// Counter for the number of overflows
volatile uint8_t overflow_count = 0;

// Interrupt Service Routine for Timer0 overflow
ISR(TIMER0_OVF_vect) {
    overflow_count++;  // Increment the overflow counter
    if (overflow_count >= 31) {  // Check if half second has passed
        PORTB ^= _BV(LED_PIN);  // Toggle LED connected to pin 13
        overflow_count = 0;  // Reset the overflow counter
    }
}

int main(void) {
    // Set pin 13 as output
    DDRB |= _BV(LED_PIN);

    // Set initial LED state to OFF
    PORTB &= ~_BV(LED_PIN);

    // Configure Timer0
    // Prescaler = 1024
    TCCR0B |= _BV(CS02) | _BV(CS00);

    // Enable Timer0 overflow interrupt
    TIMSK0 |= _BV(TOIE0);

    // Enable global interrupts
    sei();

    // Main loop does nothing, all work is done in ISR
    while (1) {
    }

    return 0;  // This line will never be reached
} */


/* #include <avr/interrupt.h>

#define LED PB5

ISR(TIM0_OVF_vect) {
    // Toggle port B pin 4 output state
    PORTB ^= _BV(LED);
}

int main(void) {
    // Set port B output 0 as output
    DDRB = _BV(LED);

    // Prescale timer to 1/1024th the clock rate
    TCCR0B |= _BV(CS02) | _BV(CS00);

    // Enable timer overflow interrupt
    TIMSK0 |=_BV(TOIE0);

    // Enable interrupts   
    sei();

    // Loop forever
   while(1) {}
}*/