#include<avr/io.h>
#include<util/delay.h>
#define F_CPU 16000000UL
void Timer_init()
{
// Set Timer0 to Normal Mode (WGM02:0 = 0)
TCCR0A=0X00;
TCCR0B=0X00:
// Set prescaler to 1024 (CS02=1, CS01=0, CS00=1)
TCCR0B |= (1 << CS02) | (1 << CS00);
// Calculate initial value for a 10ms delay (example)
// F_timer = 16MHz / 1024 = 15625 Hz
// T_tick = 1 / 15625 Hz = 64 us
// Ticks_needed = 10ms / 64us = 156.25 -> approx 156 ticks
// TCNT0_initial = 256 - 156 = 100
TCNT0 = 100; // Load the initial value
// Enable Timer0 Overflow Interrupt (optional)
TIMSK0 |= (1 << TOIE0);
sei(); // Enable global interrupts
}
ISR(TIMER0_OVF_vect)
{
PORTB|=(1<<PB5);
_delay_ms(1000);
PORTB&=~(1<<PB5);
_delay_ms(1000);
// Reload TCNT0 for continuous timing (if needed)
TCNT0 = 100;
PORTB|=(1<<PB5);
// Your code to execute on timer overflow
}
int main()
{
Timer_init();
PORTB|=(1<<PB5);
_delay_ms(1000);
PORTB&=~(1<<PB5);
_delay_ms(1000);
Timer_init();
DDRB|=(1<<PB5);
PORTB&=~(1<<PB5);
while(1);
return 0;
}