You are on page 1of 18

TIMERS

Timer Applications

In Digital Clocks
Stop Watch
Sensors
Clock generators
Oscillators
Synchronizing different
devices
Different timing purposes

AVR Timers
Timer 0 : 8 Bit timer
Timer 1 : 16 Bit timer
Timer 2 : 8 Bit timer

Meaning of 8 Bit timer


The register is 8 bit wide
The counter counts in 256
steps
After this counter resets
This is called overflow

Register and counter


0
1
2
3

25
4
25
5

0
0
0
0
1

0
0
0
0
1

0
0
0
0
1

0
0
0
0
1

0
0
0
0
1

0
0
0
0
1

0
0
1
1
1

0
1
0
1
0

How much time would it take to


count upto 255?
Let the clock frequency is 1 MHz
The time period will be calculated as
This time period is the period of one
clock cycle

One clock cycle increments the


value of counter by one unit
That means total 256 clock
cycles will be consumed in
counting- from 0 to 255

The total time in counting from 0 to 255


will be
256 Time period of one clock cycle
where time period of one clock cycle

This will be the overflow time.

Lets take an example


Frequency f= 15.625 KHz
Time period T= ?
Overflow time= ?

After counting upto 255


the counter value again
sets to 0
This is called an overflow

How many number


of overflows are
required to count
1 sec or 1000 ms?

Let the frequency be 15.625 kHz


Time period
= 1/f
= 0.064 ms
over flow time
0.064*256 = 16.384 ms
how many overflow are required to
count 1 sec=
1000/16.384=61.035

Conclusion
Total 61 overflow are required
to count 1 sec
Value is 61.035

How to count overflows?


interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
Count++;
}

The value was 61.035


We have take 61 which is
approximate
So the calculation is not
accurate
For making it accurate 0.035
must be considered

Int count=0, sec=0;


interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
Count++;
}
While(1)
{
if(count>=61 && TCNT0==9)
{
sec++;
count=0;
}
}

You might also like