You are on page 1of 29

AVR Microcontroller

Prepared by:
Eng. Ashraf Darwish

Ashraf.emad.darwish@gmail.com

Session 5

Timers /
Counters

Introduction

We use timers everyday, we can timer simply


in our Digital clock.

AVR timers do a similar job, measuring a


given time interval.

An AVR timer in simplest term is a register


have a resolution of 8 or 16 bits .

8 bit timer is 8 bits wide, and is capable of


holding value within 0-255.

Introduction

16 bit timer is 18 bits wide, and is capable of


holding value within 0-65536

AVR Timers increases/decreases automatically


at a predefined rate (supplied by user).

The AVR timers are very useful as they run


asynchronous to the main AVR core
(independent to CPU)

Introduction

System Clock:
Microcontroller is a Sequential circuit which
need clock to operate
The clock source is the oscillator circuit,
AVR can be clocked internally or externally
ATmega 16 has internal oscillator of values
1,2,4,8,12 MHz
ATmega 16 can be clocked externally up to
16 MHz

Timer / Counter

what is the difference between timer


& counter?
Timer increases automatically after a

specific time interval determined by user


Counter increases manually according to

external events

Timer / Counter

Timer / counter Applications :


Digital Clock , Stopwatch , Alarm
Propeller Clock
Traffic light
Gate counters
Dimmer circuits (PWM)
Frequency generator
Frequency meter

Timer / Counter

Prescaler :
It is an internal device that divides the system

clock to slow down the input pulses


ATmega 16 timer has its prescaler which allow

Timer to run at many speeds, the same clock


speed and less ( not more )
Prescaler can divide the clk frequency by 1, 8, 64,

256 and 1024 .


Ex: If we are working on 1MHZ clock system and

we make the prescaler value 8 ,therefore the timer


input frequency will be 125 KHz

Timer / Counter

ATmega 16 has 3 timers


Two 8-bit timers (Timer0, Timer2)
One 16-bit timer (Timer1)

8-bit timer means it counts from


0X00 to 0XFF (0 to 255)
16-bit timer means it counts from
0X0000 to 0XFFFF (0 to 65535)

Timer / Counter

What about Timer speed ?


Timer speed determined by two parameters
Chip clk frequency ( internal or external oscillator )
Prescaler of the Timer

Ex : If our chip clk is 1MHz, and we used

prescaler of 8
therefore timer speed is 1/8 MHz (125KHz)
Which mean timer increases by 1 every 8 micro seconds

Timer / Counter

1.
2.
3.
4.
.

Modes of operation
Normal mode
Clear Timer on Compare match mode
(CTC)
Fast PWM
Phase correct PWM
In our Course we will talk about the first
two modes , Normal & CTC

Timer / Counter

Normal mode :
Timer counts from 0 (or initial value determined by

user ) to maximum value ( 0xff in case of 8-bit timer


)
When timer reaches to its maximum Over flow

event occurred .
When timer overflow occurred the timer resets its

value and starts from initial value again


Overflow flag (TOV0) is set after 0XFF we can here use

Timer overflow interrupt

Timer / Counter

CTC mode :
Timer counts from 0 (or initial value determined by

user ) to specific value (determined by user)


When timer reaches to this value compare match

event occurred
Then timer resets to its initial value again
OC0 pin can toggled on compare match
We can use compare match interrupt

Timer / Counter

Timer 0 registers :

1.

Timer counter register (TCNT0)


Output compare register (OCR0)
Timer/Counter control register
(TCCR0)
Timer interrupt mask register
(TIMSK)
Timer interrupt flag register (TIFR)

2.
3.
4.
5.

Timer / Counter
1.

Timer counter register (TCNT0):

The timer counts in this register

( Timer value at any time is saved in this


register)
We can put an initial value for the timer in this

register, in this case timer will start count from


this value

Timer / Counter
2.

Output compare register (OCR0):

This register is only used in CTC mode


It contains the top value that timer will count

to it
Timer restarts when TCNT0=OCR0
When compare match occurred it can generate
toggle signal output on OC0 pin
(pin 4)

Timer / Counter
3.

Timer/Counter control register (TCCR0)

Mode selection

Compare output mode

Prescaler

Bit7-FOC0: is only active in CTC or Normal mode


zero in PWM mode

Timer / Counter
Bits 0,1,2 : cs00,cs01,cs02 : choose the
prescaler value

Timer / Counter
Bit 6, Bit 3 WGM00, WGM01 : mode selection

Timer / Counter
Bits 5, Bit 4-COM00, COM01 (Compare Output
Mode)

Timer / Counter
4.

Timer interrupt mask register (TIMSK)

Output compare match interrupt enable

Timer Overflow
Interrupt enable

Bit 0 TOIE0: Timer/Counter0 Overflow

Interrupt Enable
Bit 1 OCIE0: Timer/Counter0 Output
Compare Match Interrupt Enable

Timer / Counter
5.

Timer interrupt flag register (TIFR)

Output compare match Flag

Timer Overflow Flag

Bit 0 TOV0: Timer/Counter0 Overflow Flag

this bit is 1 when overflow occurred


Bit 1 OCF0: Timer/Counter0 Output Compare Match
flag
this bit is 1 when compare match occurred
(TCNT0=OCR0)

Timer / Counter

How to make 1 second delay using


timer
(we will need it to make digital clock)
assume we are working on ATmega 16 and the

clock frequency is 1 MHz


If we use prescaler of 1 , that mean we divide chip

clk by one , therefore our timer frequency is 1 MHz


To make calculation easy we use CTC mode and ask

timer to count from 0 to 99 ( 100 clock )

Timer / Counter

How to make 1 second delay using timer


(we will need it to make digital clock)
as we can calculate the time of one count 1/f

timer

( 1 micro second ) , the 100 counts will take 100 micro


second
That means timer count from 0 to 99 in 100 micro

second , so we need to repeat this operation 10,000 times


Here we can use timer compare match interrupt to

calculate how many times the timer reached to its


compare match value

Timer / Counter

How to make 1 second delay using timer


(we will need it to make digital clock)
simply we declare a global variable (ex: long int x )
We make this variable increase every time the code enter the compare

match interrupt
Ex:

interrupt [TIM0_COMP] void timer0_comp_isr(void)


{
x++;
If( x==10000)
{x=0; second++;}
}

Timer / Counter

How to use Counter


The counter button is connected to pin T0 (Pin 1)
when we use counter, bits 2,1,0 :

cs02,cs01,cs00 in TCCR0 register will be 1,1,0


for falling edge or 1,1,1 for rising edge
In this case counter only increase when you

press the button on T0, the value of counter is


saved in the register TCNT0

Timer / Counter

simple code :

#include <mega16.h>
int x=0, sec=0,min=0;
// Timer 0 output compare
interrupt service routine
interrupt [TIM0_COMP] void
timer0_comp_isr(void)
{
x++;
if(x==10000)
{x=0;
sec++;}
if(sec==60)
{sec=0;
min++;}
if(min==60) min=0;
}

void main(void)
{
PORTA=0x00;
DDRA=0xFF;
PORTB=0x00;
DDRB=0xFF;
TCCR0=0x09;
TCNT0=0x00;
OCR0=0x63;
TIMSK=0x02;
// Global enable interrupts
#asm("sei")
while (1)
{
PORTA=sec;
PORTB=min;
};
}

Timer / Counter

simple counter code :

#include <mega16.h>
void main(void)
{
PORTA=0x00;
DDRA=0xFF;
// Timer/Counter 0 initialization
// Clock source: T0 pin Rising Edge
// Mode: CTC top=OCR0
// OC0 output: Disconnected
TCCR0=0x0F;
TCNT0=0x00;
OCR0=0x0A;
TIMSK=0x00;

// Global enable interrupts


#asm("sei")
while (1)
{
PORTA=TCNT0;
};
}

Thank you
Contact:
http://www.facebook.com/groups/263197427113025
/
Ashraf.darwish@ymail.com
Ashraf.emad.darwish@gmail.com
+201064897791

You might also like