AVR Timers  
Ohhh....Timers are the most useful and also complex to use feature in AVR. Timers in AVR has countless uses, many number of interrupt sources comes from timers. I am telling you that the best way to learn maximum about Timers is to read datasheet of ATmega16 carefully. Timer is so wast that i can't explain all the things here at once.

ATmega16 has total of 3 timers. Two 8 bit and one 16 bit Timer/counter.

AVR timer can be used as precise time delay generator, Counter, Interrupt source and to generate waveforms too.

The main part in timer block of avr is the programmable bi-directional counter unit. Depending on the mode of operation used, the counter is cleared, incremented or decremented at each timer clock tick. Timer clock can be generated using external or internal clock sources.

Timer clock can be prescaled by maximum of 1024 units using the clock prescaling feature of timer block. prescaling factor is configured according to the vaues written by progran in clock select bits (CSx(n)).

When no clock source is selected timer is stopped. However the TCNTx value can be read by CPU, regardless of whether timer clock is present or not.

TOVx is the timer overflow flag associated with each timer which can interrupt the cpu on timer overflow.

Apart from Timer/Counter, AVR has a feature of 'Compare Match' in timer block. it is not necessary that timer overflow can occur only when the TCNTx reaches its maximum, but it can occur on compare match too.

Compare match feature can be used with OCR1A and OCR1B that is Output Compare Register.

When TCNTx value reaches to the value of either OCR1A or OCR1B , both things can interrupt the CPU, thats a big power/tool for us. Due to Output Compare feature Timer block is one of the richest interrupt sources in AVR.

Following are some Definitions -

BOTTOM - The counter reaches the BOTTOM when it becomes 0x00.

MAX - The counter reaches its maximum when it becomes 0xFF (0xFFFF).

TOP - The counter reaches the TOP when it becomes equal to the higest value in the count sequence. The TOP value can be assigned to be fixed value 'MAX' or the value stored in the OCRx registers. Assignment of TOP is dependent on the mode of operation.

As i told you the best way to learn timers is to go through the datasheet. here i am giving a summary of timer0-1-2 related registers.
















































devesh@electroons.com
Best viewed at Firefox 1024x768 resolution