AVR Timers – An Introduction
Timers are standard features of almost every microcontroller. So it is very important to learn their use. Since an AVR microcontroller has very powerful and multifunctional timers, the topic of timer is somewhat “vast”. Moreover there are many different timers on chip. So this section on timers will be multipart. I will be giving basic introduction first.
What is a timer ?
A timer in simplest term is a register. Timers generally have a resolution of 8 or 16 Bits. So a 8 bit timer is 8Bits wide so capable of holding value withing 0-255. But this register has a magical property ! Its value increases/decreases automatically at a predefined rate (supplied by user). This is the timer clock. And this operation does not need CPU’s intervention.Fig.: Basic Operation Of a Timer. |
Fig.: Basic Operation Of a Timer. |
Using The 8 BIT Timer (TIMER0)
The ATmega16 and ATmega32 has three different timers of which the simplest is TIMER0. Its resolution is 8 BIT i.e. it can count from 0 to 255. Note: Please read the “Internal Peripherals of AVRs” to have the basic knowledge of techniques used for using the OnChip peripherals(Like timer !) The Prescaler The Prescaler is a mechanism for generating clock for timer by the CPU clock. As you know that CPU has a clock source such as a external crystal of internal oscillator. Normally these have the frequency like 1 MHz,8 MHz, 12 MHz or 16MHz(MAX). The Prescaler is used to divide this clock frequency and produce a clock for TIMER. The Prescaler can be used to get the following clock for timer. No Clock (Timer Stop). No Prescaling (Clock = FCPU) FCPU/8 FCPU/64 FCPU/256 FCPU/1024 Timer can also be externally clocked but I am leaving it for now for simplicity.TIMER0 Registers.
As you may be knowing from the article “Internal Peripherals of AVRs” every peripheral is connected with CPU from a set of registers used to communicate with it. The registers of TIMERs are given below.
TCCR0 – Timer Counter Control Register. This will be used to configure the timer.
Fig.: TCCR0 - Timer Counter Control Register 0 |
As you can see there are 8 Bits in this register each used for certain purpose.
For this tutorial I will only focus on the last three bits CS02 CS01 CS00 They
are the CLOCK SELECT bits. They are used to set up the Prescaler for timer.
TCNT0 – Timer Counter 0
Timer Interrup Mask Register TIMSK
This register is used to activate/deactivate interrupts related with timers. This register controls the interrupts of all the three timers. The last two bits (BIT 1 and BIT 0) Controls the interrupts of TIMER0. TIMER0 has two interrupts but in this article I will tell you only about one(second one for next tutorial). TOIE0 : This bit when set to “1” enables the OVERFLOW interrupt. Now time for some practical codes !!! We will set up timer to at a Prescaler of 1024 and our FCPU is 16MHz. We will increment a variable “count” at every interrupt(OVERFLOW) if count reaches 61 we will toggle PORTC0 which is connected to LED and reset “count= 0”. Clock input of TIMER0 = 16MHz/1024 = 15625 Hz Frequency of Overflow = 15625 /256 = 61.0352 Hz if we increment a variable “count” every Overflow when “count reach 61” approx one second has elapse.
Setting Up the TIMER0
// Prescaler = FCPU/1024 TCCR0|=(1<<CS02)|(1<<CS00);
//Enable Overflow Interrupt Enable TIMSK|=(1<<TOIE0);
//Initialize Counter TCNT0=0;Now the timer is set and firing Overflow interrupts at 61.0352 Hz
The ISR
ISR(TIMER0_OVF_vect)
{
//This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
//CPU automatically call this when TIMER0 overflows.
//Increment our variable
count++;
if(count==61)
{
PORTC=~PORTC; //Invert the Value of PORTC
count=0;
}
}
Demo Program (AVR GCC)
Blink LED @ 0.5 Hz on PORTC[3,2,1,0]
#include <avr/io.h>
#include <avr/interrupt.h>
volatile uint8_t count;
void main()
{
// Prescaler = FCPU/1024
TCCR0|=(1<<CS02)|(1<<CS00);
//Enable Overflow Interrupt Enable
TIMSK|=(1<<TOIE0);
//Initialize Counter
TCNT0=0;
//Initialize our varriable
count=0;
//Port C[3,2,1,0] as out put
DDRC|=0x0F;
//Enable Global Interrupts
sei();
//Infinite loop
while(1);
}
ISR(TIMER0_OVF_vect)
{
//This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
//CPU automatically call this when TIMER0 overflows.
//Increment our variable
count++;
if(count==61)
{
PORTC=~PORTC; //Invert the Value of PORTC
count=0;
}
}
Hardware
ATmega16 or ATmega32 running @ 16MHz. Connet LEDs using 330ohms resistors on PORTC[3,2,1,0]. If you are using xBoard you can connect four onboard LEDs to PORTC using four PIN Connectors. Thats it for now meet in next tutorial. And please don't forget to post a comment, I am waiting for them !Download PDF version | Get Adobe Reader Free !!!

[...] The compare feature is not present in the 8 bit TIMER0 of the ATmega8 so we will use the TIMER1 which is a 16 Bit timer. First we need to setup the timer’s prescaler as described in the Timer0 tutorial. Please see this tutorial for a basic introduction of TIMERs. [...]
October 24th, 2008 at 6:07 pmcan you send me a program to make a reaction tester please
November 21st, 2008 at 2:15 am@Van de Poel Dominic
November 21st, 2008 at 12:16 pmI don’t understand by “reaction tester” pls give details!
[...] Introduction to AVR Timers [...]
January 14th, 2009 at 10:35 amWhen you write example code you should not use shortcuts. Shortcuts allow advanced programmers to code faster, but make it extremely difficult for beginning programmers to follow.
Consider:
x++; vs. x = x + 1;
A beginner might not know what ++ does, and even if they do, x = x + 1 is still more clear and easier to follow.
Now consider:
TCCR0|=(1<<CS02)|(CS01);
I know you are performing a bitwise operation on the timer counter control register. I know from your commenting the code that you are setting the prescaler to FCPU/1024. I know from the chart that means you are setting the last 3 bits to 1 0 1. Yet I have no idea what that line of code is doing. So if I wanted to set the clock prescaler to /512 I have no idea the change that would be needed.
I’ve studied it and concluded that it appears this might do the same thing:
// set prescaler to FCPU/1024 by setting last 3 bits of TCCR0 to 101
February 23rd, 2009 at 2:36 amTCCR0 = TCCR0 & 0b11111101 // turn off bit 1
TCCR0 = TCCR0 | 0b00000101 // turn on bits 0 and 2
MtAVRest, possible somewhere you’re right. But..
1. Please agree that a real noob will not start it’s programmers career from firmware programming. Even if AVR’s are easiest.
2. C++ is not Basic. I don’t see any reason to rewrite this code: x++. It is not obfuscated. It is syntax. And really simple one. Even more, there is a clear comment in the code:
//Increment our variable
count++;
3. Now consider this:
TCCR0|=(1<<CS02)|(CS01);
And this:
TCCR0 = TCCR0 & 0b11111101;
TCCR0 = TCCR0 | 0b00000101;
What are simpler to read? It is impossible to understand your code without looking at register documentation. First example, at least, have the register bit names.
IMHO, author is good, but your comment is simply ignored.
P.S. My blog is not such descriptive, but what the heck
:
April 26th, 2009 at 5:31 pmhttp://blog.stranadurakov.com/2009/04/01/set-up-avr-timer-interrupt/
Hello Andrejs,
Nice and Correct response to MtAVRest!
I thank you for time and effort to answer his doubts precisely.
Thank you again
April 26th, 2009 at 6:39 pmi couldn’t understand how have you obtained 0.5 Hz in the last code?
May 29th, 2009 at 8:29 am@pankaj
June 18th, 2009 at 4:40 pminterrupts occur at around 61 Hz so in 1 sec there are 61 interrupts.therefore counter is set to 0 every 1 sec.that means portc is toggled every 1 sec.that means it is on for 1 sec and off for 1 sec.so time period is 2 sec and freq is 1/2=0.5Hz
@Nischey Grover
Very Nice work to help out Pankaj.
June 18th, 2009 at 5:03 pm[...] Using AVR Timers [...]
July 4th, 2009 at 9:51 amHi, this is propably quite stupid question but i’ve been figuring it out several hours now with no luck.
i’m getting this error: ‘TCCR0′ undeclared (first use in this function)
So my compiler thinks TCCR0 is a variable that havent been introduced but it’s a register. right?
how do i tell it to my compiler?
I’m using AVR studio 4 as my compiler.
August 27th, 2009 at 3:30 am@Kalle,
TCCR0 is a register associated with TIMER0. It will only be available in chips which has Timer 0. If you try to compile when a chip which does not has a specific peripheral (like TIMER0 in this case) you will get the above error. So please try to select appropriate chip like ATmega8 or ATmega16 from the configuration dialog of AVR Studio.
AVR Studio usages is given here
August 27th, 2009 at 5:52 amhttp://extremeelectronics.co.in/avr-tutorials/part-iv-the-hello-world-project/
Kaile, first thing you need to tell us is what chip you are using, because there is some differences between some avr versions.
August 27th, 2009 at 12:56 pmFor example, TCCR0 should be TCCR0A in ATmega164P/324P/644P.
It works now, had to rename TCCR0 to TCCR0A for my chip. Thanks for fast replies.
August 27th, 2009 at 7:01 pmNo problem, glad this helped.
August 27th, 2009 at 7:04 pmHi, i am not getting clear idea about this statement
TCCR0|=(1<<CS02)|(CS01);
I understood the logic that this command sets the TIMER0 to 00000101 by bitwise operation (for prescaler to be FCPU/1024), but how this is happening, please elaborate.
I understood other two syntaxs-
September 2nd, 2009 at 9:41 pmTCCR0 = TCCR0 & 0b11111101;
TCCR0 = TCCR0 | 0b00000101;
volatile uint8_t count;
‘volatile’ I have not heard about this datatype, what this is for?
September 2nd, 2009 at 9:44 pm@Aditya Sharma
Hey man see this
September 3rd, 2009 at 9:32 amhttp://extremeelectronics.co.in/avr-tutorials/programming-in-c-tips-for-embedded-development/
First of all volatile is NOT a data type. It is used to mark a varriable as volatile (what is this? Please read on).
Suppose we write
while(1)
{
if(a==250)
{
print("Done")
break;
}
else
{
z++;
}
}
When the compiler compiles the above code it tries to optimize the code. It sees that a is not modified any where in the loop, so instead of getting the value of ‘a’ from memory to register in each loop it just gets the value into register ONLY one time. It does same with all varriables which are not modified in the NORMAL course of loop execution. But what if the value of ‘a’ is modified by an ISR (interrupt service routine) !!! Interrupt can occur at any time thus breaking the normal course of execution and executing the ISR (which say increments ‘a’) and then silently continues execution from where it left.
This kind of varriable that can be updated by Hardware are called volatile varriables. A very common example is PORT input PINs their value can be changed by the external environment (like presssing a button). Other varriables that are controlled 100 % by the software are simple varriables
Got it ???
September 3rd, 2009 at 9:42 amAll varriable that are shared between ISR and main program MUST be “volatile”
September 3rd, 2009 at 9:45 amThanks for this really helpful explanation, the ‘volatile’ concept is clear to me now.
Regarding bitwise operations i have understood the tutorial well before asking you the query. The problem was that earlier the statement was
TCCR0|=(1<<CS02)|(CS01);
and now you have changed it to..
TCCR0|=(1<<CS02)|(1<<CS00);
This one is of-course right, but the former one was wrong as i suppose so i pointed out.
Thanks again;
September 3rd, 2009 at 12:34 pmAditya Sharma
Also, can you please answer the query posted by one of the user on this link…
September 3rd, 2009 at 12:37 pmhttp://robozeal.blogspot.com/2009/03/basic-mechanisms.html
he is having some doubts in Atmega8 programmer and some more stuff, i have not used Atmega8 and USB programmer so unable to clarify his doubt, Please do have a look if you don’t mine!
which timer should i use to simulate a set of traffic lights? any idea on how i could implemnent in?
September 18th, 2009 at 7:14 pmYuveer,
Due to high delay you need to use 16-bit timer. You can use timer output from one side and 3 PIO outputs from other side to build simple traffic light matrix.
In software, you just build interrupt which will analyze when and what to switch on or off depending on the global variables you set.
Hope this will help.
September 22nd, 2009 at 11:49 amHI,
October 5th, 2009 at 7:14 ami am new to this AVR controllers. If frequency of Overflow = 61.03Hz & when “count reach 61” approx one second has elapse.
How did you calsulate one second elapse? Can’t it be 1ms?
I dont understand this. Can you please help me?
@abcd
Pls don’t flood with silly comments !!!!!!!!!!!!!!!!
October 5th, 2009 at 11:27 amIf you don’t know relation between time and frequency pls go for some other profession.
@abcd,
Check Nischey Grover comment on this page dated June 18th, 2009 at 4:40 pm, it will explain you how this code is working.
October 5th, 2009 at 1:27 pmI highly appreciate
Mr Andrejs work of Finding and giving a solution to “abcd”.
October 5th, 2009 at 4:48 pm-Admin
Hello Avinash,
October 24th, 2009 at 2:07 amI read your AVR timer Introduction tutorial. For every 1 sec here timer 61 overflow interupts. Can you please explain me how to Initialize a timer to generate a overflow interrupt for every 15ms?
Hi, what blog platform is this? Is it working for you or..? I would really like it if you could answer this question! Regards!
December 24th, 2009 at 11:50 pm@Verda
This is Wordpress !!!
December 25th, 2009 at 9:54 ami found it quite helpful for me ….it helped me clear my concepts and hope more n more such tutorials ..thanx a lot
December 28th, 2009 at 7:21 pmFrequency of Overflow = 15625 /256 = 61.0352 Hz
February 19th, 2010 at 1:06 pmCan some explain to me the logic behind dividing the Clockfrequency by 256.
@Jay
Didn’t you read 2nd Paragraph? Timer Overflow?
I get angry when I see questions like this.
When you not get anything you should try to read again.
February 19th, 2010 at 1:11 pm@Jay
Why would anyone explain you the same thing that is written above?
Can’t you get simple arithmetic ???
February 19th, 2010 at 1:12 pm@Jay
same question throws in my head … but like Avinash said, you have to read again to understand.
(Answer: because the TIMER0 is 8-bit wide so it can count 2^8=256 clocks, but only one time it generate an overflow in those 256 clocks)
@Avinash
March 3rd, 2010 at 7:11 pmKeep up the good work … you are amazing !!!