40

Avr timers

Embed Size (px)

Citation preview

Page 1: Avr timers
Page 2: 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.

Page 3: Avr timers

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 .

Page 4: Avr timers

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.

Page 5: Avr 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.

Page 6: Avr timers

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.

Page 7: Avr timers
Page 8: Avr timers

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

Page 9: Avr timers

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

Page 10: Avr timers

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.

Page 11: 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?

Page 12: Avr timers

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.

Page 13: Avr timers

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.

Page 14: Avr timers

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

Page 15: Avr timers

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

Page 16: Avr timers

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.

Page 17: Avr timers

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.

Page 18: Avr timers

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

Page 19: Avr timers

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

Page 20: Avr timers

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;

Page 21: Avr timers

#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();

Page 22: Avr timers

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

Page 23: Avr timers

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.

Page 24: Avr timers

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!

Page 25: Avr timers

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.

Page 26: Avr timers

// 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();

Page 27: Avr timers

// 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 } } } }

Page 28: Avr timers

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

Page 29: Avr timers
Page 30: Avr timers

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

Page 31: Avr timers

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.

Page 32: Avr 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.

Page 33: Avr timers
Page 34: Avr timers

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

Page 35: Avr timers

The value to be compared is stored over here.

Page 36: Avr timers

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

Page 37: Avr timers

Set these bits if you are using the interrupts.

ISR (TIMER1_COMPA_vect) { //to do }

Page 38: Avr timers

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

Page 39: Avr timers

Setting the above bits will enable the hardware mode.

We go back to TCCR1A register.`

Page 40: Avr timers

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

Hope you guys enjoyed it.

Image Courtesy: maxembedded.com