Timer0 in Atmega16-AVR

In the previous post of timer-introduction, we had learned the basic concepts of timers and timer register in Atmega16. If you are not familiar with the timer, do read my post (Introduction to timers)before continuing further. In this article, we will learn a bit more about TIMER-0 in Atmega16 of AVR as well as the use of prescaler to generate the desired delay. We will do this by learning how to use timer0 without prescaling, and then with prescaling.

Timer0 without Prescaling

Problem Statement

Let’s say, we have to generate the delay 4 ms when our microcontroller is operating at 32KHz. We know now from the previous article that we have to find the timer count before continue. We can find the timer value by the following formula:

In our case, the value of the time required is 4 ms and our microcontroller is operating at 32 KHz. By calculating the value, we found that the required timer count is 127. It can easily be achieved by Timer-0 which is an 8-bit timer. What we need to do is, we need to keep track of the timer value, as soon as it reaches the timer value we found, it gets resets.

The TCNT0 Register

Timer0 in Atmega16

This is the register where the value of 8-bit timer stored. The value of this register increases/decreases automatically. Also, you can write as well as read the data from this register. Now you may think- How are going to start the timer? Well, there is another register for that, which is-TCCR0-(Timer Counter Control Register).

The TCCR0 Register

The tccr0 register is:

Timer0 in Atmega16

The last three bits of TCCR0 register is CS00, CS01, and CS02. The value of these bits decides the value of prescaler. The value of these bits and the prescaler is given below in the table.

Timer0 in AVR

As, we are not using prescaler in this example, therefore we have to high the CS00 bit, which can be done by using shift register, TCCR0 |= (1 << CS00); Note that, we have to initialise this register because if we don’t initialise it, the value of CS00, CS01 and CS02 will remain ‘0’ which is the default and according to the table, the timer will remain stopped.

The Program

#include <avr/io.h>
 
void timer0_init()
{
TCCR0 |= (1 << CS00);// initialising the timer with no prescaling
TCNT0 = 0;// initialize timer
}

int main(void)
{
timer0_init();// calling timer function
while(1)// infinite loop
    {
      if (TCNT0 >= 127)// check if the timer count reaches 127
        {
          TCNT0 = 0;  // reset the timer
        }
    }
}

The Prescaler

Let’s understand a bit more about prescaler. We have an introduction of this in my previous post-introduction to timers. Here, we will learn about selecting the suitable value of prescaler and it’s implementation in timer. Those who are thinking what the heck is this prescaler? A prescaler is a factor by which you are dividing the frequency of the microcontroller. So, if the prescaler is 8, it means you are dividing the frequency by the factor of 8. Suppose we have to produce the delay of 10 ms and the operating frequency of the microcontroller is 4 MHz and since this delay is so large, it is not possible to achieve it without prescaling. What should the value of prescaler we should choose?

Timer0 in Atmega16

We should select the value of the prescaler according to the need. Because selecting the large value of prescaler means you are trading it with accuracy and resolution. In the problem discussed above, I can take 256 as well as 1024 as a prescaler, but I am selecting 256 because selecting the higher value “prescaler” means trading off with accuracy and resolution.

Program with Prescaler

#include <avr/io.h>
 
void timer0_init()
{
TCCR0 |= (1 << CS02);// setting up the timer with the prescaling of 256
TCNT0 = 0;// initialize timer
}

int main(void)
  {
   timer0_init();// calling function to initialize timer
   while(1)// infinite loop
    {
      if (TCNT0 >= 155)  // check if the timer count reaches 155
        {
         TCNT0 = 0;            // reset the timer
        }
    }
}

I am continuing with the problem mentioned in the Prescaler section. In this program, as I have explained above too, I am choosing 256 as a prescaler and I have assigned the value to the CS00, CS01, and CS02 accordingly. I think the program is pretty much straightforward. Just go through it, it is nothing new. Everything is already explained to you. If there is any problem, you can ask for it in the comment section.

Leave a Comment

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

Share This
Scroll to Top