Timer Introduction:

Everything on earth is synchronized by time, therefore we need a device that can accurately tell about time duration. The earlier generation of such devices(timer) includes Water clock, Sun clock, second’s Pendulum, Sand clock and much more. My point is that anything that can give information about time intervals is called timer. The clock which hangs on your wall, or your wristwatch, or the virtual clock showing on the computer screen all of these devices are the timer. Timers are also important in the electronics world too. Every microcontrollers and microprocessor are synchronized with the clock. Since all of these devices are synchronized by some clock, they all have timers. AVR has very accurate timers which have a resolution of up to microseconds. In this article, I am going to discuss the basic concepts of the timers in Atmega16.

The need of the timer

You may think that, if you have to make a timer for a particular time, you can use delay right? Well, the thing is you can use delay but the use of the delay is not recommended because the delay function is not accurate. It is because the delay function doesn’t consider the operating frequency of the microcontroller. So if you decrease the frequency, the delay will increase and if you increase the frequency, the delay will decrease. Therefore, it is not suitable to use delay where you need the accuracy.

Timers in Atmega16

In the microcontrollers, the timer is a register, but not the normal one. The value of the timer register increases/decreases automatically. In Atmega16, there are two types of timers – 8-bit timer and 16-bit timer. The 8-bit timer has a register memory of 8 bit(1 byte). This means it is capable to count up to 256 steps from 0 to 255. Similarly, the 16-bit has a memory of 16 bit(2-byte). This means it can count 65536 steps from 0 to 65535. Because of this, timers are also known as counters. When the timer reaches its maximum value, the program doesn’t terminate instead, the value of timer register reset. The condition is called timer overflow. There are three types of timers in Atmega16.

There are three modes of the timer which is the normal mode, CTC(Clear on Timer Compare) mode, and PWM(Pulse Width Modulation) mode.

Timer Concepts

How do we are going to generate the timer on the microcontroller? Well, we just need to know that “Operating frequency of the Microcontroller?” and “Required delay”. Once we know this, we will give the initial value to the timer register and start the timer. Ater starting the timer, we will continuously monitor the status of the timer register. If it overflows(reaches the maximum value) changes, that means the timer reaches its maximum value, therefore we will stop the timer. Now, here comes another question-“How are we going to know that what value we have to give during the initialization of the timer?

Timer in AVR

Now suppose that we have to generate a delay of 10ms and the microcontroller is operating at 4Mhz. It means the time period of the microcontroller operating is 1/(4Mhz)=0.0000025 sec or 0.00025 msec. This implies that for every step, it takes 0.00025 msec. Let’s say that the initial value of the timer is 0. It takes 0.00025 msec to change the value from 0 to 1. We should know the number of pulses(Timer Count) required to produce a particular delay. For this:


Timer in AVR

In the example we are taking, to generate a delay of 10ms, the Timer count = (10ms/0.00025msec)-1=39999. Therefore, we need 39999 steps to generate the delay of 10 msec. Needless to say that we can not choose the 8-bit timer which is TIMER-0 because the maximum steps it can go is 255. Therefore, we have to choose TIMER-1 which is 16 bit(2-byte) and is capable to count up to 65536 steps.

Timer Operation in AVR


According to what we have learned, the maximum delay that can be generated from TIMER-1 is 16.384 msec. That’s very less right? What if we have to generate the delay of 100msec? For this, we have a solution called Prescaling. If we can reduce the frequency of microcontroller from 4MHz to 500 KHz(0.5 MHz), we are good to go because the time period increases from 0.00025 msec to 0.002 msec. So, for the delay of 100 msec, we need 49999 steps which can be achieved by using TIMER-1. You may wonder that how are reducing the frequency? Actually, we are not reducing the frequency supplies to the microcontroller, we are just dividing the frequency to run the timer. But the reduction of the frequency comes with the price-which is accuracy. Accuracy decreases when we increase the prescaler. We will learn more about prescaler in the subsequent post. That’s it for this article, subscribe to our blog for more cool tutorial post. Thanks for Reading!

Prakhar Bhatt

Prakhar is a recently graduated Engineer from Noida. As a student of Electronics, he has spent four years of his college life pondering over micro-processers and micro-controllers and bagged two research papers from projects in the same domain. Simply a geek.

Latest posts by Prakhar Bhatt (see all)

Share This