/*
Generating a delay of 1000 ms using ATmega16 Timer0
www.electronicwings.com
*/
#include <avr/io.h>
void T0delay();
int main(void)
{
DDRF = 0xFF; /* PORTB as output */
PORTF=0;
volatile long i;
while(1) /* Repeat forever */
{
PORTF= ~ PORTF;
for(i=0; i<30;i++){
T0delay();}
}
}
void T0delay()
{
TCCR0B = (1<<CS02) | (1<<CS00); /* Timer0, normal mode, /1024 prescalar */
TCNT0 = 0x00; /* Load TCNT0, count for 10ms */
while((TIFR0&0x01)==0); /* Wait for TOV0 to roll over */
TCCR0B = 0;
TIFR0 = 0x01; /* Clear TOV0 flag */
}