Previously we learn about timer CTC. Now what happen if we do not compare the value? In that case the timer starts from BOTTOM to the MAXimum value i.e. 0xFF (for Timer0/2) or 0xFFFF (Timer1) and that is timer overflow. In this case when the timer overflows an interrupt available i.e. Timer Overflow Interrupt vector.
Lets make 1s delay using Timer1, with F_CPU=8MHz and Prescaler=8
Maximum count Time=(Prescaler/F_CPU)*(2^n) =(8/8MHz)*(2^16) =65.536ms
So we need to overflow the Timer(1000/65.536)=15.2587≈15 times. The main program is
#include<avr/io.h>
#include<avr/interrupt.h>
uint8_t sample=0;
int main(void)
{
DDRD=0xFF;
TCCR1A=0; //since no source require
TCCR1B=(1<<CS11); //normal operation with 8 presaling
TCNT1=0; //initialize timer1
sei(); //set global interrupt
TIMSK=(1<<TOIE1); //set overflow interrupt
while(1);
return 0;
}
ISR(TIMER1_OVF_vect)
{
sample++;
if(sample>=15){
PORTD^=0x0F;
sample=0;
}
}
The flag register of individual interrupt is active when an interrupt active that TCNTx is over than 0xFF/0xFFFF. Let’s modify the above program without interrupt handling. Since the flag is set when TCNTx value over than 8bit or 16bit value, so in if() statement we check the overflow flag bit and clear the flag by writing 1 to it i.e. TIFR|=(1<<TOV1);
#include<avr/io.h>
uint8_t sample=0;
int main(void)
{
DDRD=0xFF;
TCCR1A=0; //since no source require
TCCR1B=(1<<CS11); //normal operation with 8 presaling
TCNT1=0; //initialize timer1
while(1)
{
if(TIFR&(1<<TOV1)){
sample++;
if(sample>=15){
PORTD^=0x0F;
sample=0;
}
TIFR|=(1<<TOV1);
}
}
return 0;
}