Timer1 in Atmega16-AVR

In this article, we will learn about timer-1 in Atmega-16/32 of AVR family of microcontrollers. I hope that you guys have read my previous posts on Timer-introduction and Timer0 in Atmega-16. Basically, familiarise yourself with the timer concept by reading my previous articles because I am not going to explain much on timer concept. In this article, I am going to use prescaler and timer in the same program. If you want to understand the concept of prescaler and also, about the code structured-read my previous article about timer introduction and Timer0 in Atmega16.

Problem Statement

Let’s say, we have to blink an LED for every 5 seconds. The frequency at which the microcontroller is operating is 8MHz.

Method- using Prescaler

We need a bit of calculation before the code. Basically, I know that our timer will overflow, because the delay we required is too large for the microcontroller. What I am going to do is first is, I am going to divide the frequency by 8(Choosing 8 as a prescaler) and then I am going find out the number of times the timer will overflow to generate the required delay. If I can calculate that(which is quite tough but I know I can do it), the job is done. How am I going to find the value?   It’s fairly easy to find the value. I have stated above that I am choosing 8 as a prescaler therefore, the frequency of the microcontroller becomes (1 MHz). This means the clock time period of the microcontroller would be(1/1 MHz) 1 µsecond. Therefore, it takes 65536 µsec to overflow the 16-bit timer. It is so because the microcontroller has a time period of 1 µsec and the maximum value of the 16-bit timer is 65536. So, if I have to calculate the number of times it overflow to generate the delay of 4 sec, I have to divide 4 sec with 65536 µsec. So, by dividing 4 sec with 65536 µsec(4 ÷ (65536 × 10^-6)), the answer is 61.035 or 61. The timer needs to overflow 61 times to generate the delay of 4s. Now, we just need to know how to initialize the timer and how to set the prescaler?

The TCCR1B Register

Timer 1 in Atmega 16

This is the register which controls all the operations and modes on Timer1 of Atmega16. Here, we only need to set the value of prescaler, therefore the bits that should concern us right now is CS10, CS11, and CS12. The value of them is zero by default. We need to set the value according to the prescale we select. The table is given below- According to the prescaler we have selected(8), we have to set the value of CS11 to ‘1’.

TCNT1 Register

Timer 1 in Atmega16

This register holds the value of timer1 which is 16 bit. TCNT1H holds the upper bit while TCNT1L holds the lower bit. We have to give the value to this register to initialize the timer. Now we have everything we need.

The Program

I have written this program in ATMEL Studio. If you want to know how to use it, click here.

#include <avr/io.h>

int main(void)
{   char i=0;// used as a counter
	DDRD=0xff;// output port for LED
	TCCR1B|=(1<<CS11);// setting the prescaler
	TCNT1=0;// starting the timer
		i++;	// count number of times timer overflows
			PORTD^=(1<<0);// complement the port every time
			i=0;// reset the counter

Execution of the Program

This program has been executed well in Proteus. If you want to know more about Proteus, click here.  This sums up the basic of Timer-1 in Atmega-16. If you like this article hit subscribe to never miss another article. See you next time.

Timer1 in Atmega16 Timer1 in AVR

Leave a Comment

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

Share This
Scroll to Top