Timer Interrupt in Atmega16-AVR

Now, we know about timers in AVR and Interrupt in AVR, we are ready to learn about Timer Interrupts in Atmega16- AVR. In this article, we are going to learn how to initialize the timer interrupt for Timer0 as well as Timer1. I am not discussing the concepts of timers in detail as I have already discussed it in my previous articles. If you want to learn the basic of timers, check out my previous articles before continuing to this.

Problem Statement

Suppose we have to generate a blink the LED in 1 sec interval. The operating frequency of the microcontroller is 4 MHz. I have connected the LED in PIN0 of Port D.

Solution using Timer0

We need a bit of calculation before writing the code. See, we know that the delay is very large so, I am choosing 8 as a prescaler. After that, I am calculating the number of times it should overflow to generate the delay of 1sec.

How am I going to calculate the value?

It’s easy to find out the value. As I have already stated above, the prescaler I am choosing is 64. This results the frequency of the controller is 64.5 KHz. As we know, the frequency is an inverse of the Time period (T=1/F). Therefore, the time period of the controller should be 16 microseconds. Now, because the timer here I am using is 8 bit, therefore to find out the total time it takes to count till 8 bit multiplies the time period with 256(0-255 total 256 counts) which is 4096 microsecond.

Now, we know how much time the controller takes to count the value till 8 bit. But, if we divide the value to the delay required, we can know how many time the timer have to overflow to generate the delay of 1sec. On dividing, the value is 244.14 which is approximately equal to 244.

Therefore, the microcontroller has to overflow 244 times for the delay generation of 1sec. 

Now we just need to know how to initialize the timer interrupt.

TIMSK Register

Timer in AVR

TIMSK or Timer/Counter Interrupt Mask Register is a control register used to mask or unmask the timer interrupts. The bit 0 of TIMSK Register controls the overflow interrupt of Timer 0. Setting this bit to ‘1’ enables the overflow interrupt of timer 0.

The Program-using Timer-0

Now we know how to set the timer interrupt of timer 0, we are ready to program. Do note that, I have taken 64 as a prescaler. If you want to know about the basic of prescaler and how to initialize it, do check out this article.

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

char a;
int main(void)
{
	DDRD=0xff;
	PORTD=0x00; // LED is connected in this port
	TIMSK=(1<<TOIE0); // enabling the interrupt
	TCCR0=(1<<CS01)|(1<<CS00); // setting the prescaler-64
	sei();
    while(1)
    {
       
    }
}
ISR(TIMER0_OVF_vect)
{
	a++;
	if(a>244)
	{
	PORTD=~PORTD;
	a=0;
	}
}

Solution using Timer-1

The same register is used to control the interrupt of timer 1. The 3rd bit of the TIMSK Register controls the timer 1 overflow interrupt. Setting this bit to ‘1’ enables the timer 1 overflow interrupt. We just need to calculate the values required for the timer.

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
char a;
int main(void)
{
	DDRD=0xff;
	PORTD=0x00; // LED is connected in this port
	TIMSK=(1<<TOIE1); // enabling the interrupt
	TCCR1B=(1<<CS00); // setting the prescaler
	sei();
	while(1)
	{
		
	}
}
ISR(TIMER1_OVF_vect)
{
	a++;
	if(a>61)
	{
		PORTD=~PORTD;// for blinking the LED
		a=0;
	}
}

Calculation

We know that the timer 1 is a 16-bit timer. So, it can count up to 65535 number or 65536 counts (0 to 65535). What we need here is to calculate the number of time it needs to overflow to produce a delay of 1 sec. Here, I am not selecting any prescaler.

The microcontroller is operating at 4 MHz.It means the time period of the microcontroller would be-0.25 microsecond. The time period it is taking to count up to 65535 is 16,384 microseconds. On dividing this value by 1(Required delay), we get to know that the number of time it needed to overflow is 61.

Execution of the Program

Both the programs are executed well in Proteus as well as in Hardware. For assistance in Proteus and Programming the microcontroller, do check out linked articles.

Well, that’s it for this article, please note that I have tried my best to not make any errors on calculations. But If there is any, you can tell me in the comment section below.

Leave a Comment

Your email address will not be published. Required fields are marked *

Share This
Scroll to Top