5a-AVR-Timers (8 Bit)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

AVR Timers

Lecture 5a
Timers/Counters in Atmega16
◼ Atmega16 has three timers/counters
❑ 8-bit Timer/Counter0
❑ 16-bit Timer/Counter1
❑ 8-bit Timer/Counter2

2 / 39
Timers/Counter Functions
◼ The Timers can be used for the following
functions
❑ Normal Mode (Delay generation or Measuring
time)
❑ Clear timer on compare match CTC (Delay
generation or Measuring time) (Auto reload)
❑ Count External events
❑ PWM generation
◼ Phase correct PWM
◼ Fast PWM
❑ Waveform Generation using Normal and CTC
mode

3 / 39
Registers Associated with Timers
◼ Timer/Counter Control Register – TCCR0

This Register selects the modes (WGMxx) of the


timer and the clock/prescaler (CSxx) to the timer

4 / 39
Registers Associated with Timers
◼ Timer/Counter Register – TCNT0

The Timer/Counter Register gives direct access, both for read and
write operations, to the Timer/Counter unit 8-bit counter
◼ Output Compare Register – OCR0

The Output Compare Register contains an 8-bit value that is


continuously compared with the counter value (TCNT0). A match
can be used to generate an output compare interrupt, or to generate
a waveform output on the OC0 pin.

5 / 39
Registers Associated with Timers
◼ Timer/Counter Interrupt Flag Register – TIFR

Contains the Flags for compare match and overflow

6 / 39
Flags Associated with Timers
◼ Timer/Counter Interrupt Flag Register – TIFR

❑ Bit 0 – TOV0: Timer/Counter0 Overflow Flag


◼ The bit TOV0 is set (one) when an overflow occurs in
Timer/Counter0. TOV0 is cleared when executing the
corresponding interrupt handling vector. Alternatively, TOV0 is
cleared by writing a logic one to the flag.
❑ Bit 1 – OCF0: Output Compare Flag 0
◼ The OCF0 bit is set (one) when a compare match occurs
between the Timer/Counter0 and the data in OCR0 – Output
Compare Register0. OCF0 is cleared when executing the
corresponding interrupt handling vector Alternatively, OCF0 is
cleared by writing a logic one to the flag.

7 / 39
Timer/Counter0 Interrupts
◼ Timer/Counter Interrupt Mask Register – TIMSK

❑ Bit 0 – TOIE0: Timer Overflow Interrupt Enable 0


◼ When the TOIE0 bit is written to one, and the I-bit in the Status
Register is set (one), the Timer/Counter0 Overflow interrupt is
enabled. The corresponding interrupt is executed if an overflow in
Timer/Counter0 occurs, i.e., when the TOV0 bit is set in the
Timer/Counter Interrupt Flag Register – TIFR.
❑ Bit 1 – OCIE0: Timer/Counter0 Output Compare Match Interrupt
Enable
◼ When the OCIE0 bit is written to one, and the I-bit in the Status
Register is set (one), the Timer/Counter0 Compare Match interrupt is
enabled. The corresponding interrupt is executed if a compare match
in Timer/Counter0 occurs, i.e., when the OCF0 bit is set in the
Timer/Counter Interrupt Flag Register – TIFR.

9 / 39
ISR For Timers/Counter0
◼ ISR For Timer Overflow Interrupt
ISR(TIMER0_OVF_vect)
{
…..
}

◼ ISR For Timer Output Compare Match Interrupt


ISR(TIMER0_COMP_vect)
{
……
}

10 / 39
Associated PIN with Timer 0
Timer 0 Input

PWM Channel
timer0

11 / 39
Selection of Modes
◼ TCCR0 (Timer/counter Control Register)
Selects mode of Timer

◼ WGM01 and WGM00 bits in the TCCR0 register selects


the mode of the timer

15 / 39
Normal Mode (1)
◼ TCCR0(Timer/counter Control Register)
Selects mode of Timer

◼ WGM01 and WGM00 selects the mode of the timer


◼ For Normal Mode
❑ WGM01=0

❑ WGM00=0

16 / 39
Normal Mode

TOV0

17 / 39
Normal Mode
◼ Normal Mode(WGM01=0 & WGM00=0)
❑ The simplest mode of operation is the normal mode
❑ In this mode the counting direction is always up
(incrementing)
❑ The counter simply overruns when it passes its
maximum 8-bit value (TOP = 0xFF) and then restarts
from the bottom (0x00)

18 / 39
Normal Mode
◼ Normal Mode(WGM01=0 & WGM00=0)
❑ In normal operation the Timer/Counter Overflow
Flag(TOV0) will be set in the same timer clock cycle
as the TCNT0 becomes zero
❑ This mode can be used to create delays or measure
time between two events.

19 / 39
Normal Mode

◼ An interrupt can be generated each time the counter


value overflows by using the TOV0 flag
◼ If the interrupt is enabled, the interrupt handler routine
can be used for updating (reloading) the TCNT0 value

20 / 39
8-bit Timer/Counter0
◼ Clock Source Select
❑ The Timer/Counter can be clocked by an internal or an
external clock source
❑ The clock source is selected by the clock select (CS02:0)
bits located in the Timer/Counter Control Register (TCCR0)
Timer/Counter Control Register – TCCR0

❑ Bit 2:0 – CS02:0: Clock Select


The three Clock Select bits select the clock source to be used by
the Timer/Counter
21 / 39
Prescaler Options
◼ CS02,CS01 and CS00 selects the prescaler

22 / 39
Calc of No for TCNT0 Register
◼ Procedure
❑ Nc = Time of delay (us) x Crystal freq (MHz)
❑ If Nc < 256 use Timer0 without prescaler else use prescaler.
❑ Nr = 256 – Nc (value to be loaded in TCNT0)

◼ Use Prescaler if Nc > 256


❑ Np = Nc / prescaler
❑ Nr = 256- Np

23 / 39
Example
Use Timer0 in normal mode to create a delay of
25us. Controller frequency is 8mhz. Calc number
for TCCR0 (pre-scaler), TCNT0;
◼ Solution
❑ Nc = 25u x 8M = 200
❑ Nc<256, hence no prescaler is required.
❑ Nr = 256-200 = 56

TCCR0=0b00000001; (no Pre-scaler)


TCNT0=Nr=56;

24 / 39
Example
◼ Generate a delay of 5ms using Timer0. Controller
frequency is 8Mhz. Calculate value of Pre-scaler and
TCNT0.
◼ Solution
❑ Nc = Time of delay (us) x Crystal freq (MHz)
❑ Nc = 5000x8 =40,000
❑ NC > 256 use Timer0 with prescaler

❑ Np = Nr / prescaler
❑ Np = 40000 / 8=5000
❑ Since, Np > 256 we choose the next prescaler value
❑ Np = 40000 / 64=625
❑ Np is still > 256 we choose the next prescaler value
❑ Np = 40000 / 256=156.25
❑ Nr = 256- Np
25 / 39
Example
◼ Timer0 is operated in normal mode with a prescaler of 1024.
Calculate the max delay generated (in ms). Controller frequency
is 8Mhz.
◼ SOLUTION
❑ Nc = Time of Delay(Td) x (Crystal Frequency/Prescaler)
❑ Td = (Nc x Prescaler)/ Crystal Frequency
❑ Td = (256 x 1024)/8
❑ Td = 32768us = 32.768ms

28 / 39
Example – Normal Mode (using interrupt)
#include<avr/io.h>
#include<avr/interrupt.h>
int main()
{
DDRB=0xFF;
TCNT0=186;
TIMSK=0x01;
SREG|=0x80;
TCCR0=0x02;
while(1)
{
}
}
ISR(TIMER0_OVF_vect)
{
PORTB=PORTB^0x10;
TCNT0=186; AVR_Normal_mode_Int
}
30 / 39
Clear Timer on Compare Match (2)

31 / 39
Clear Timer on Compare Match (CTC) (2)
◼ TCCR0(Timer/Counter Control Register)
Selects mode of Timer

◼ WGM01 and WGM00 selects the mode of the timer


◼ For CTC Mode
❑ WGM01=1 WGM00=0

◼ CS02,CS01 and CS00 selects the prescaler

32 / 39
Clear Timer on Compare Match
◼ In Clear Timer on Compare or CTC mode the OCR0
Register is used to manipulate the TIMER/COUNTER
resolution
◼ In CTC mode the counter is cleared to zero when the
counter value (TCNT0) matches the OCR0
◼ The OCR0 defines the top value for the counter, hence
also its resolution
◼ It also simplifies the operation of counting external
events

34 / 39
Clear Timer on Compare Match (CTC)
◼ An interrupt can be generated each time the counter
value reaches the TOP value by using the OCF0 flag
◼ If the interrupt is enabled, the interrupt handler routine
can be used for updating the TOP value
◼ If the new value written to OCR0 is lower than the
current value of TCNT0, the counter will miss the
compare match. The counter will then have to count to
its maximum value (0xFF) and wrap around starting at
0x00 before the compare match can occur

35 / 39
Example
◼ Write a C program to toggle only the PORTB.4 bit
continueously every 70 us. Use Timer0, CTC mode and
1:8 Prescaler to create the delay. Assume XTAL= 8 MHz.

36 / 39
Example CTC Mode (using interrupt)
#include<avr/io.h> #include<avr/io.h>
#include<avr/interrupt.h> #include<avr/interrupt.h>
int main() int main()
{ {
DDRB=0xFF; DDRB=0xFF;
TCCR0=0x0a; TCCR0=0x02;
OCR0=70; TCNT0=186;
TIMSK=0x02; TIMSK=0x01;
SREG|=0x80; SREG|=0x80;
while(1) while(1)
{} {}
}
}
ISR(TIMER0_COMP_vect) ISR(TIMER0_OVF_vect)
{ PORTB=PORTB^0x10; { PORTB=PORTB^0x10;
TCNT0=186;
} }

AVR_CTC_mode_Int 37 / 39
Counter Mode (3)

◼ The above two modes are used to drive the timer from
an external clock source.
◼ By using these options the timer can be used as a
counter to count external events.
38 / 39
Example
◼ Count the number of cars passing on the road. A sensor
is placed across the road, that sends a pulse when it
senses a car, attached to pin PB0. Configure the timer
so that it responds to the rising edge. Output the number
of cars on PORTA
◼ Solution
For this we configure the timer to count on the rising edge of an
external source. Hence, CS02=1,CS01=1,CS00=1. (Clock
Select)
The number of cars that pass through the road will be equal to
the value of TCNT0.

39 / 39
Solution - Code
#include<avr/io.h>

int main()
{
DDRA=0xFF;
DDRB=0x00;
TCCR0=0x07;
while(1)
{
PORTA=TCNT0;
}
}

AVR_Counter_Int.c
Timer_counter.isis
40 / 39

You might also like