7 Timers
7 Timers
Introduction.
Normal Mode.
Counter.
1
Introduction
What is a counter? A counter counts events.
If the event (pulse) is taken place at constant rate, then the counter
becomes a timer.
2
Introduction
ATMega16 has 3 timers:
timer0 - 8 bit,
Timer1 - 16 bit,
timer2 - 8 bit.
In the AVR, the timers are separate circuits which can run
independent of the main program, interacting via the
control and count registers, and the timer interrupts.
4
Introduction
For timer1: The Timer/Counter (TCNT1-16-bit) and Output Compare Registers
(OCR1A and OCR1B), and Timer/Counter Control Register (TCCR1A and
TCCR1B).
5
Introduction
For timer2: The Timer/Counter (TCNT2) and Output Compare Register
(OCR2), and Timer Control Register (TCCR2).
6
Introduction
General Timer/Counter Registers:
7
Timer modes in AVR
CTC mode
Normal mode
PWM modes
Counter
8
The CTC Timer Mode
AVR timer modes:
Normal ,
CTC,
PWM modes.
9
The CTC Timer Mode
Some basic definition of timer / counter:
BOTTOM: This is the minimum value that the counter can
reach (i.e. zero).
MAX: This is the maximum value that the counter can have
(i.e. 2bits – 1. i.e. 28 – 1=255 or 216 – 1=65535).
TOP: This is the highest value in the count sequence which can
be assigned to MAX or value stored in OCR or ICR registers.
10
The CTC Timer Mode
𝑃𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟 𝑣𝑎𝑙𝑢𝑒
𝑡𝑖𝑐𝑘𝑠 + 1 = 𝑇𝑎𝑟𝑔𝑒𝑡 𝑡𝑖𝑚𝑒/𝑇𝑖𝑚𝑒𝑟 𝑡𝑖𝑚𝑒 where 𝑇𝑖𝑚𝑒𝑟 𝑡𝑖𝑚𝑒 =
𝐶𝑃𝑈 𝑓𝑟𝑞𝑢𝑒𝑛𝑐𝑦
Let 𝐶𝑃𝑈 𝑓𝑟𝑞𝑢𝑒𝑛𝑐𝑦 = 1 MHz for target time = 1 sec what the exact
prescaler (i.e. integer) value?
1000000
𝑡𝑖𝑐𝑘𝑠 = −1
𝑝𝑟𝑒𝑠𝑐𝑎𝑙𝑒𝑟
Prescaler = 64
and OCRA1 = 15624.
11
The CTC Timer Mode
Let's pseudo code this so we can get a better understanding of what we
want to do:
12
The CTC Timer Mode
For CTC mode WGM13: WGM10 = 4 or 12 as in table below
13
The CTC Timer Mode
15
CTC Mode Using Interrupt
Let's pseudo code can get a better understanding of what we want to do:
Set up LED hardware
Set up timer in CTC mode
Enable CTC interrupt
Enable global interrupts
Set timer compare value to one
second
WHILE forever
END WHILE
Note: Vector Name get form the table from the Source
column by putting underscore for each space. 16
CTC Mode Using Interrupt
# include <avr/io.h> // CTC mode (ex.: t5.c)
# include <avr/interrupt.h>
int main( void )
{
DDRB |= (1 << PB0); // Set LED as output
TCCR1B |= (1 << WGM12); // Configure timer1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
sei ( ) ; // Enable global interrupt
OCR1A = 15624; // Set CTC compare value to 1Hz
TCCR1B |= ( (1<< CS10)|(1<< CS11)); // Set up timer at Fcpu/64
while ( 1 ) { }
}
//Notice that our main loop is now empty; if this is the case you may put
//sleep commands inside the main loop to save power between compares.
ISR ( TIMIR1_COMPA_vect )
{
PORTB ^= (1 << PB0 ); // Toggle the LED
} 17
Pure Hardware CTC Mode
We'll be looking at the Compare Output settings of the AVR timer.
In AVR timer, these are bits called COM allow us to control the
hardware behavior when a compare occurs.
Instead of firing an interrupt. The hardware can be configured to
set, clear or toggle the hardware pins (OC0, OC1A ,OC1B, and
OC2 ) when a compare occurs.
This mode used to generate square waveforms by toggle the
hardware pins as in figure.
18
Pure Hardware CTC Mode
WHILE forever
END WHILE 19
Pure Hardware CTC Mode
while ( 1 ) { }
}
20
Normal Mode
When the timer register exceeding its max value, it will
automatically roll around back to zero and the overflow flag bit is
set.
By setting overflow flag bit (TOV) indicates the main application that
the event has occurred.
21
Normal Mode
In CTC mode
WHILE forever
IF TOV flag IS EQUAL TO 1 THEN
Toggle LED
Load timer with initial value to get one second
Clear TOV flag
END IF
END WHILE
23
Normal Mode
// Normal mode 1 (ex.: t7.c)
# include <avr/io.h>
int main( void )
{
DDRB |= (1 << PB0); // Set LED as output
TCNT1 = 49911; // initial value to get 1Hz in normal mode
25
Counter
GPIO Pin Alternative Function
PB0 T0
PB1 T1
Use keypad as
in table below.
Key operation
+ Adjust seconds
- Adjust minutes
ON Reset clock
= Resume clock
27
Fast PWM Mode
While CTC mode allows us to generate simple waveforms. For
greater control of waveforms, we need to use the timer/counter
PWM (Pulse Width Modulation) modes.
Fast PWM Mode works very much like CTC mode, but adds
another register for controlling the pulses on the OC1x pins.
TOP in Fast PWM may be OCR1x , ICR1, 0xFF, 0x1FF, and 0x3FF.
But in fast PWM mode we generate a square wave with variable duty
cycle.
The fast PWM mode well suited for power regulation, rectification,
and dc motors applications.
29
Fast PWM Mode
The Compare Output Mode bits for Channel A and B are used to
control the Output Compare pins (OC1A and OC1B).
30
Fast PWM Mode
For Fast PWM mode 4,5,6,7,14 and 15
31
Fast PWM Mode
In general by PWM we can control of frequency, duty cycle
and average of the wave generated.
Frequency of Fast PWM
𝐹 𝐹
𝐹𝐹𝑊𝑀 = 𝑡𝑖𝑚𝑒𝑟 = 𝑜𝑠𝑐𝑖𝑙𝑎𝑡𝑜𝑟
𝑇𝑜𝑝+1 𝑁 (𝑇𝑜𝑝+1)
𝑂𝐶𝑅1𝑥+1
𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 = x 100 (non-inverted)
𝑇𝑜𝑝+1
𝑇𝑜𝑝−𝑂𝐶𝑅1𝑥
𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 = x 100 (inverted)
𝑇𝑜𝑝+1
Example: Assuming XTAL 8Mz, generate two waves with
frequency of 125 Hz on OC1A and OC1B using mode 14, non-
inverted mode and prescaler = 256 with 80% and 30% duty
cycle, respectively.
8𝑀
Solution: 125 = 256 𝑥 (𝑇𝑜𝑝+1)
Top = 249
𝑂𝐶𝑅1𝐴+1
80 =
250
x 100 OCR1A = 199
𝑂𝐶𝑅1𝐵+1
30 =
250
x 100 OCR1B = 74
32
Fast PWM Mode
//The code for Phase Correct PWM ICR1 as TOP (mode 14) t9.c
# include <avr/io.h>
Note that the rising edge of the pulse on OC1x always occurs at
the same point.
34
Phase Correct PWM
For motor control, we want the center of the pulse to be constant (no
phase shifts). So we use the Phase Correct PWM mode to solving this.
OC1x goes low when OCR1x = COUNT on the way up (incrementing)
and goes high when OCR1x = COUNT on the way down (decrementing).
No switching occurs at TOP, which may be OCR1x , ICR1, 0xFF, 0x1FF,
and 0x3FF.
35
Phase Correct PWM
The Compare Output Mode bits for Channel A and B are used to
control the Output Compare pins (OC1A and OC1B).
36
Phase Correct PWM
For Phase Correct PWM mode 1,2,3,10 and 11
37
Phase Correct PWM
Frequency of Phase Correct PWM
𝐹𝑡𝑖𝑚𝑒𝑟 𝐹
𝐹𝑃𝐶𝑃𝑊𝑀 = = 𝑜𝑠𝑐𝑖𝑙𝑎𝑡𝑜𝑟
2 𝑥 𝑇𝑜𝑝 2 𝑥 𝑁 𝑥 𝑇𝑜𝑝
𝑂𝐶𝑅1𝑥
𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 = x 100 (non-inverted)
𝑇𝑜𝑝
𝑇𝑜𝑝−𝑂𝐶𝑅1𝑥
𝐷𝑢𝑡𝑦 𝐶𝑦𝑐𝑙𝑒 = x 100 (inverted)
𝑇𝑜𝑝
Atmega16 manual.