Avr timers

Post on 26-May-2015

497 views 8 download

Tags:

Transcript of Avr timers

A timer is a circuit that counts. This is said from an electronics point of view.

It is widely used and is a very important component of microprocessors and microcontrollers.

Timers are classified based on their mode of operation into synchronous and asynchronous timers.

Synchronous timers count with respect to the clock while asynchronous timers depend upon the change in the input.

They are also classified according to the maximum number they can count. Eg. 8 bit timer,16 bit timers etc

Timers are easy to implement and are done using basic flip flop circuits .

They are used everywhere from modulating signals to implement digital clocks, gaming, phone/pc/tablet applications etc.

Small projects like range finders etc will also use timers.

IC’s such as 555 are readily available in the

market and can be used to easily implement timers.

There are 3 timers in the AVR of which 2 are 8 bit timers the other one is a 16 bit timer.

The timers found in the AVR or mostly in a microcontroller or processor is of synchronous type.

what do they mean? An 8 bit timer can count to 2 to the power of 8 and a 16 bit timer can count upto the 2 to the power of 16.

Basically timers in the controller are registers and an 8 bit timer is a 8bit register and 16 bit timer is 16bit register.

The 8 bit timer starts counting from zero and goes upto 255.(that’s 256 counts)

The 16 bit timer starts counting from zero and goes uptp 65535(65536 counts)

Once the timer reaches the maximum value it “overflows” i.e. it restarts.

ANALOGY

For the timer to increase by one it takes one clock. That is the time period gives the time it takes for the circuit to increment the timer register by one

Consider a processor with 4MHz clock. We need a delay of 10ms. What will the timer count be??

What will the timer count be when the “required delay” is 50us

Which timer will you use ??

Can a delay of 100ms be directly implemented with one of AVR timers.

So to know the maximum delay you could get from the processor with a given clock substitute TOP value of that particular timer in the formula.

Maximum delay for a 4MHz processor

16bit timer?

8 bit timer?

What will we do for cases like the 100ms delay?

The solution lies in reducing the frequency.

How do we do that?

That’s where tht prescaler comes into play.Do understand that we don’t actually reduce the frequency of the clock but we make the timer to behave as if it is in a reduced frequency.

Also note that there will be a trade-off between resolution and accuracy if you use a precaler.

The prescaler is set by manipulating some bits.

Now get the timer count for 100ms using one of these prescalers.

PROBLEM STATEMENT:

Make an LED flash for every 10ms with your atmega 8 internal oscillator featuring a 1MHz clock. Use only 8 bit timer TIMER

Do the calculations first.

Without using a prescaler maximum delay=256us

Using a prescaler of 8 we’ll get a maximum delay =2048us

Using a prescaler of 64 max delay= 16.3ms

our requirement of 10ms delay fits in this range.

No that we know our prescaler we should now calculate our timer count.

Substituting 10ms in the formula we ge timer count to be 155.25. we’ll round it out to 156 counts.

The most important one is TCCR0 register which is the Timer/counter Control register for timer 0. First we start the clock and set the prescalar which is done by setting the three high lighted bits

Thus we have to set bits CS01 and CS00 to 1. TCCR0|=1<<CS00|1<<CS01;

The register where the counting takes place is the TCNT0 register.It counts automatically and overflows and restarts again.

We intitalise this too. TCNT0=0;

#include <avr/io.h>

void timer0_init()

{

// set up timer with no prescaling

TCCR0 |= ((1 << CS00)|(1<<CS01));

// initialize counter

TCNT0 = 0;

}

int main(void)

{

// connect led to pin PC0

DDRC |= (1 << 0);

// initialize timer

timer0_init();

// loop forever while(1) { // check if the timer count reaches 156 if (TCNT0 >= 156) { PORTC ^= (1 << 0); // toggles the led TCNT0 = 0; // reset counter } } }

There’s an alternative way of doing this. Without using the prescalar or if the delay cannot be got with the given prescalers.

Let’s do the same example without using prescalars.

We previously calculated that without prescaler maximum delay we get is 256us

A point to be noted is that everytime the timer overflows an optional ISR is executed.

We will use that interrupts to get the delay

Let’s see how!

10ms/256us=39.0625

So when the timer overflows 39 times it would have counted

256*10^-6 *39= 9.984ms

The remaining time is 10ms- 9.984ms=16us

Now we substitute this again in the formula and we’ll get 15 as the timer count.

Thus at the 40th iteration and 15th tick we’ll achieve our 10ms delay.

// global variable to count the number of overflows

volatile uint8_t tot_overflow;

// TIMER0 overflow interrupt service routine

// called whenever TCNT0 overflows

ISR(TIMER0_OVF_vect)

{

// keep a track of number of overflows

tot_overflow++;

}

void timer0_init()

{

// set up timer with no prescaling

TCCR0 |= (1 << CS00);

sei();

TIMSK|=1<<TOIE0;

// initialize counter

TCNT0 = 0;

}

int main(void)

{

// connect led to pin PC0

DDRC |= (1 << 0);

// initialize timer

timer0_init();

// loop forever while(1) { // check if no. of overflows = 39 if (tot_overflow >= 39) // NOTE: '>=' is used { // check if the timer count reaches 15 if (TCNT0 >= 15) { PORTC ^= (1 << 0); // toggles the led TCNT0 = 0; // reset counter tot_overflow = 0; // reset overflow counter } } } }

The concept is the same with very minute differences. For example the counting register TCNT1 will be a 16 bit register.

The control register is split into 2 8bit registers TCCR1A and TCCR1B. For normal mode of operation knowledge about TCCR1B will suffice

TIMSK-TIMER/COUNTER INTERRUPT MASK REGISTER

This is used to enable the timer interrupt.TOIE0 is timer overflow interrupt enable for 0 and TOIE1 is for timer 1. It is necessary to set that bit inorder to enable the interrupt and execute the ISR

TIFR- timer interrupt flag register.

We are interested in the TOV bit. The Timer overflow bit. This is set to 1 whenever the timer overflows . It is cleared after it overflows. The above two registers are shared by all the three timers.

CTC stands for Clear timer on Compare.

Remember previous example with the 10ms with timer0?

The comparing can be done by the processor itself and the corresponding instructions can be executed.

Both CTC mode are almost the same except the fact that they store the compare value in different registers.

The value to be compared is stored over here.

The OCF1A/B bit is set to 1 when the value stored in OCR1A/B equals TCNT1 register.

Set these bits if you are using the interrupts.

ISR (TIMER1_COMPA_vect) { //to do }

Those pins can be used to directly be set or cleared without any extra code.

Setting the above bits will enable the hardware mode.

We go back to TCCR1A register.`

FINAL coding lies in your hands. You can use the timer as you wish.

Hope you guys enjoyed it.

Image Courtesy: maxembedded.com