# AVR Timers – TIMER2

Hello friends! Welcome to the tutorial on the TIMER2 of AVR ATMEGA16/32. I hope that you have already come across and read the following posts, in which the basic concepts and applications of AVR Timers are discussed.

In this post, we will discuss about TIMER2. Since TIMER2 is an 8-bit timer (like TIMER0), most of the registers are similar to that of TIMER0 registers. Apart from that, TIMER2 offers a special feature which other timers don’t – Asynchronous Operation. We will discuss about it later.

Since you are already aware of the concepts (I assume so, or else refer to my previous posts), we will proceed the way we did in TIMER1 tutorial. We will implement both prescalers and interrupts in the same problem statement.

# Problem Statement

We need to flash an LED every 50 ms. We have an XTAL of 16 MHz. This is the same problem statement that we discussed in TIMER0 (the last one). We will implement the same using TIMER2.

### Methodology – Using Prescaler and Interrupt

As discussed in the TIMER0 tutorial, we use a prescaler of 256. For this, the overflow time is 4.096 ms. Thus the timer should overflow 12 times (MAX = 255) and count up to 53 in the 13th iteration, and then reset the timer. The formula used is as follows:Now let’s have a look at the TIMER2 registers.

# TCCR2 Register

The Timer/Counter Control Register – TCCR2 is as follows:

TCCR2 Register

Since we will be dealing with the CTC mode later, we are only concerned with Bits2:0 – CS22:20 – Clock Select Bits. Unlike other timers, TIMER2 offers us with a wide range of prescalers to choose from. In TIMER0/1 the prescalers available are 8, 64, 256 and 1024, whereas in TIMER2, we have 8, 32, 64, 128, 256 and 1024!

Clock Select Bit Description

Since we are choosing 256 as the prescaler, we choose the 7th option (110).

# TCNT2 Register

In the Timer/Counter Register – TCNT2, the value of he timer is stored. Since TIMER2 is an 8-bit timer, this register is 8 bits wide.

TCNT2 Register

# TIMSK Register

The Timer/Counter Interrupt Mask – TIMSK Register is as follows. It is a register common to all the timers.

TIMSK Register

Here we are concerned with the 6th bit – TOIE2 – Timer/Counter2 Overflow Interrupt Enable. We set this to ’1′ in order to enable overflow interrupts.

# TIFR Register

The Timer/Counter Interrupt Flag Register – TIFR is as follows. It is a register common to all the timers.

TIFR Register

Here we are concerned with the 6th bit – TOV2 – Timer/Counter2 Overflow Flag. This bit is set (one) whenever the timer overflows. It is cleared automatically whenever the corresponding Interrupt Service Routine (ISR) is executed. Alternatively, we can clear it by writing ’1′ to it.

# Code

To learn about I/O port operations in AVR, view this. To know about bit manipulations, view this. To learn how this code is structured, view the TIMER0 post. To learn how to use AVR Studio 5, view this.

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

// global variable to count the number of overflows
volatile uint8_t tot_overflow;

// initialize timer, interrupt and variable
void timer2_init()
{
// set up timer with prescaler = 256
TCCR2 |= (1 << CS22)|(1 << CS21);

// initialize counter
TCNT2 = 0;

// enable overflow interrupt
TIMSK |= (1 << TOIE2);

// enable global interrupts
sei();

// initialize overflow counter variable
tot_overflow = 0;
}

// TIMER0 overflow interrupt service routine
// called whenever TCNT0 overflows
ISR(TIMER2_OVF_vect)
{
// keep a track of number of overflows
tot_overflow++;
}

int main(void)
{
// connect led to pin PC0
DDRC |= (1 << 0);

// initialize timer
timer2_init();

// loop forever
while(1)
{
// check if no. of overflows = 12
if (tot_overflow >= 12)  // NOTE: '>=' is used
{
// check if the timer count reaches 53
if (TCNT2 >= 53)
{
PORTC ^= (1 << 0);    // toggles the led
TCNT2 = 0;            // reset counter
tot_overflow = 0;     // reset overflow counter
}
}
}
}```

So friends, this is how to operate TIMER2 in general mode. Other modes of operation will be discussed in upcoming posts. In the next post, we will learn about the Clear Timer on Compare (CTC) Mode and learn how to apply it to different timers.

## 14 responses on “AVR Timers – TIMER2”

1. Pingback: AVR Timers – TIMER1 « maxEmbedded·

2. Pingback: Introduction to AVR Timers « maxEmbedded·

3. Pingback: AVR Timers – TIMER0 « maxEmbedded·

4. Pingback: AVR Timers – CTC Mode « maxEmbedded·

5. Pingback: Transition timing·

6. Pingback: AVR Timers – TIMER0 | maxEmbedded·

7. Hi Mayank,
I am wondering if you are familiar with the various sleep modes of the AVR? I would like to have the AVR enter Power-Save mode for say 5 minutes, then become active again for only 1 minute. In looking at the datasheet, I am not sure if this is possible or not. It looks like it will only do a single overflow on TIMER2.

• Hi Devin,
Sorry, but I haven’t worked with them yet. But you can refer the AVR Library and this document for that. I would suggest you to go through the second pdf, it explains about the three different sleep modes as well as their implementation using C. You will have to set a few bits in the MCUCR register.

• Thanks Richard!

8. Hi Mayank,
I am working on PWM generation using 8 bit. But I want to maximum value to TCNT0. Say if I set TCNT0=100, then when I call pwm=100 then it should generate PWM with 100% duty cycle…
Will you help me in this code….

• Hi Anup,
You should check out my PWM tutorial, where I discussed the same concept.