Timers are important electronic components used in microprocessors and microcontrollers to generate precise timing and delays. They work by counting clock signals and can be classified based on their mode of operation and maximum count. Timers are commonly implemented using flip flops and are used across many applications that require timing functions. Microcontrollers typically contain multiple timers that can be configured and programmed for applications requiring accurate timing and delays.
Timers in Microcontrollers: Classification, Operation & Applications
1.
2.
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.
3.
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 .
4.
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.
5.
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.
6.
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.
7.
8.
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
9. 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
10.
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.
11.
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?
12.
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.
13.
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.
14.
Now get the timer count for 100ms using one of
these prescalers.
15.
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
16. 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.
17.
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.
18. 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
19. Thus we have to set bits CS01 and CS00 to 1.
TCCR0|=1<<CS00|1<<CS01;
20.
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;
21.
#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();
23.
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.
24.
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!
25. 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.
26.
// 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();
27.
// 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
}
}
}
}
28. 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
29.
30.
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
31.
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.
32.
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.
33.
34. Both CTC mode are almost the same except the fact that they
store the compare value in different registers.