Hi Friends, today we are going to start a very important series of five tutorials based on AVR Timers. In first tutorial we are going to see an introduction to AVR timers. Next Two will be specifically for 8-bit timer and 16-bit timers and rest two practical use of timers. So here we start ….
What is Timer?
Timers are an important concept in the field of electronics. All the microcontrollers work at some predefined clock frequency, they all have a provision to set up timers. Timers can do counting, monitoring external events, keeping time, frequency generation, generating accurately timed pulses, increment, decrement, Pulse width modulation (PWM) and for registering the timestamp of external events(Input capture mode).
We are going to have a series of tutorial on Timers. In first tutorial it is going to be an introduction to AVR Timers, in second tutorial we would be having in depth discussion about AVR Timer Registers. And remaining two tutorials we would practically trying to understand how to use timers 8bit and 16bit in different modes. So here we start…..
In principle, a timer is a simple counter. The input clock of microcontroller and operation of the timer is independent of the program execution. The deterministic clock makes it possible to measure time by counting the elapsed cycles and take the input frequency of the timer into account.
All the Atmel microcontrollers have Timers as an inbuilt peripheral. In this article we are going to use the target controller ATmega16. ATmega16 comes with two 8 bit and one 16 bit timer. This means that there are 3 sets of timers, each with the ability to count at different rates. The two 8-bit counters can count to 255 whilst the 16 bit counter can count to 65,536. We are also considering that we have LEDs connected to the PORTB of the Atmega16.
Timers can run asynchronous to the main AVR core it means timers are totally independent of CPU. A timer is usually specified by the maximum value to which it can count called MAX beyond which it overflows and resets to zero is called BOTTOM. The speed of counting can be controlled by varying the speed of clock input to it. In Atmega16 we have three different kinds of timers as follows
|Timer 0||8 bit counter||Can count up to 255|
|Timer 1||16 bit counter||Can count up to 65,536|
|Timer 2||8 bit counter||Can count up to 255|
Most of us know this following formula
Let us first have a look at the basics of how a timer works. There are basically two types of timers, 8 bit(counter 0) and 16 bit(counter 1) timers. The only major difference between them is that they have different maximum values up to which they can count. As you may already know, an 8 bit binary number can have a maximum value of 255 and a 16 bit timer can go up to 65535. The 16 bit register stores the value of the count using two 8 bit registers.
So the time that our timer takes for one count is 1/1000000 sec, i.e. 1.000001sec. The required time period for the flashing LED is 1/20 i.e. .05 sec. Therefore, in order for the frequency to be 20Hz, the LED should be toggled every 0.025 sec.
Let us find out the number of timer counts needed to reach 0.025sec.
But since the counting starts from 0 and not from 1 , we need to go only up to 24999.
As this value cannot be accommodated inside an 8 bit timer we will have to use counter 1 which is of 16 bits.
Relevant registers for Timer0 and Timer 2
|Timer 0||Timer 2||Description|
|TCCR0A||TCCR2A||Timer/Counter Control Register A|
|TCCR0B||TCCR2B||Timer/Counter Control Register B|
|OCR0A||OCR2A||Output Compare Register A|
|OCR0B||OCR2B||Output Compare Register B|
|TIMSK0||TIMSK2||Timer/Counter Interrupt Mask Register|
|TIFR0||TIFR2||Timer/Counter Interrupt Flag Register|
Relevant registers for Timer1
|TCNT1||16-bit counter register|
|TCCR1A||Mode of operation and other settings|
|TCCR1B||Mode of operation, prescaler and other settings|
|OCR1A||16-bit Compare Register A|
|OCR1B||16 bit Compare Register B|
|TIMSK||Interrupt Mask Register|
|TIFR0||Timer/Counter Interrupt Flag Register|
Timers are usually used in one of the following modes:
- Fast PWM
- Phase correct PWM
(add image hrere Image number two)
As we discussed earlier there are three different timers available in Atmega16 and all of them works in almost same way. As we know a timer is an 8 or 16 bit register that keeps on increasing its value, so one of the basic condition is when timer register overflows, means it counts reaches to its max value (255 for 8bit and 65535 for 16bit timers) and gets reset back to 0. At this situation timer can issue an interrupt.
What is Prescaler?
A technique to derive a lower frequency from F_CPU, without effecting actual F_CPU to run timer is called prescaler. In other words it is a mechanism for generating clock for timer by F_CPU clock. Atmega series of microcontrollers are available in several frequencies such as 1MHz, 8MHz, and 12MHz etc.
Why we need prescaler?
Assume we supply a 2MHz signal to timer and a 16 bit timer which can count max upto 65535. Using the formula, we discussed that we can get a maximum delay of 32ms. Just imagine, what if we need delay greater than 32ms ? One of the easiest thing we can do is to use the timers prescaler. Prescaler allows us to divide up the incoming clock signal by power of 2. It reduces the resolution which means that the accuracy has decreased but giving us the longer timer range.
Prescalar can be set to produce the following clocks:
- No Clock – Timer Stop
- No prescaling – Clock frequency = F_CPU
Timer in Compare Mode (CTC Mode)
There is another mode of operation for AVR timers are called CTC (Clear on Timer Capture mode). This mode is called Clear Timer on Compare Match, or CTC. Instead of counting until an overflow occurs, the timer compares its count to a value that was previously stored in a register. When the count matches that value, the timer can either set a flag or trigger an interrupt, just like the overflow case.
Here we finish with introduction of AVR Timers. Please click here AVR Timers: In depth timer registers 8-bit to continue with this series of tutorials
One sincere request to all the readers, please comment down and correct us, this site is trying to help people to understand embedded technology, this is only possible with your comments and corrections. Please subscribe to the newsletter to get latest project/article updates right in your email inbox. Take few moments and like zembedded facebook page, follow on tweeter, thanks!