0% found this document useful (0 votes)
4 views

7 Timers

Description for timer working on AVR32

Uploaded by

lem62wak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

7 Timers

Description for timer working on AVR32

Uploaded by

lem62wak
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Outline

 Introduction.

 The CTC Timer Mode.

 CTC Mode using Interrupts.

 Pure Hardware CTC.

 Normal Mode.

 Counter.

 Fast PWM Mode.

 Phase Correct PWM Mode.

1
Introduction
 What is a counter?  A counter counts events.

 At each event the counter value is incremented by one.

 If the event (pulse) is taken place at constant rate, then the counter
becomes a timer.

 AVR timers measuring a elapsed time interval.

 These timer intervals are related to the input frequency by:


𝑇𝑖𝑚𝑒𝑟 𝑅𝑒𝑠𝑜𝑙𝑢𝑠𝑖𝑜𝑛 = 1/𝐼𝑛𝑝𝑢𝑡𝐹𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 (smallest time period)

 For example, if ATMEGA16 microcontroller is running at 1 MHz


1
𝑇𝑖𝑚𝑒𝑟 𝑅𝑒𝑠𝑜𝑙𝑢𝑠𝑖𝑜𝑛 = = 1𝜇𝑠.
1𝑀𝐻𝑧

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.

 Timers can be configured to produce outputs directly to


pre-determined pins, reducing the processing load on the
AVR core. 3
Introduction
 For timer0: The Timer/Counter (TCNT0) and Output Compare Register
(OCR0), and Timer/Counter Control Register (TCCR0).

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:

 Timer Interrupt Flag Register (TIFR).

 All interrupts are individually masked with the Timer


Interrupt Mask Register (TIMSK).

7
Timer modes in AVR

 CTC mode

 Normal mode

 PWM modes

 Counter

8
The CTC Timer Mode
 AVR timer modes:

 Normal ,

 CTC,

 PWM modes.

 In CTC mode, the hardware compares current timer value


against the wanted value, and when the wanted value is
reached a flag in a status register is set and the timer's value
reset.

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:

Set up LED hardware


Set up timer in CTC mode
Set timer compare value to one second
WHILE forever
IF CTC flag IS EQUAL TO 1 THEN
Toggle LED
Clear CTC flag
END IF
END WHILE

12
The CTC Timer Mode
For CTC mode WGM13: WGM10 = 4 or 12 as in table below

13
The CTC Timer Mode

# include <avr/io.h> // CTC mode (ex.: t4.c)


int main( void )
{
DDRB |= (1 << PB0); // Set LED as output
TCCR1B |= (1<< WGM12); // Configure timer1 for CTC mode 4
OCR1A = 15624; // Set CTC compare value to 1Hz

TCCR1B |= ( (1<< CS10)|(1<< CS11)); // Set up timer at Fcpu/64


while ( 1 )
{
if (TIFR & (1<< OCF1A))
{
PORTB ^= (1 << PB0 ); // Toggle the LED
TIFR = (1<< OCF1A); // clear the any AVR flag (by writing a
// logic one to its bit location !)
}
}
14
}
CTC Mode Using Interrupt
 If we want to shift the responsibility of choosing when to
execute the timer code to the AVR hardware, we need to use
the timer interrupts.

 Interrupts are events that when enabled, cause the AVR to


execute a ISR.

 The AVR timers can have several different Interrupts–


typically Overflow, Compare and Capture.

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

ISR Timer Compare


Toggle LED
END ISR
ISR (VectorName_vect)
{
// Code to execute on ISR fire here
}

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.

GPIO Pin Alternative Function


PB3 OC0
PD5 OC1A
PD4 OC1B
PD7 OC2

18
Pure Hardware CTC Mode

The pseudo code


Set up LED hardware
Set up timer in CTC mode
Enable Timer1 Compare Output Channel A in toggle Mode
Set timer compare value to one second

WHILE forever
END WHILE 19
Pure Hardware CTC Mode

// Pure Hardware CTC mode (ex.: t6.c)


# include <avr/io.h>

int main( void )


{
DDRD |= (1 << PD5); // Set LED as output
TCCR1B |= (1 << WGM12); // Configure timer1 for CTC mode
TCCR1A |= (1 << COM1A0); // Enable Timer1 compare channel A
// in toggle mode

OCR1A = 15624; // Set CTC compare value to 1Hz


TCCR1B |= ( (1<< CS10)|(1<< CS11)); // Set up timer at Fcpu/64

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

𝑡𝑖𝑐𝑘𝑠 = 𝑇𝑎𝑟𝑔𝑒𝑡 𝑡𝑖𝑚𝑒/ Timer 𝑡𝑖𝑚𝑒 − 1

 In Normal mode (𝑖𝑛𝑖𝑡𝑣𝑎𝑙𝑢𝑒 : 2𝑏𝑖𝑡𝑠 -1 = 𝑡𝑖𝑐𝑘𝑠)


𝑖𝑛𝑖𝑡𝑣𝑎𝑙𝑢𝑒 = 2𝑏𝑖𝑡𝑠 - 𝑇𝑎𝑟𝑔𝑒𝑡 𝑡𝑖𝑚𝑒/ Timer 𝑡𝑖𝑚𝑒

 If we want 1 second form the timer1.

Prescaler 𝑖𝑛𝑖𝑡𝑣𝑎𝑙𝑢𝑒 Notes


Value
1 -934464 negative value (not allowed)
8 -59464 negative value (not allowed)
64 49911 Less than max. value (allowed)
256 61629.75 Not integer value (not allowed)
1024 64559.4375 Not integer value (not allowed)
22
Normal Mode
 Use normal mode to toggle LED every one second

The pseudo code 1

Set up LED hardware


Set up timer in normal mode
Load timer count with initial value to get one second

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

TCCR1B |= ( (1<< CS10)|(1<< CS11)); // Set up timer at Fcpu/64


while ( 1 )
{
if (TIFR & (1<< TOV1))
{
PORTB ^= (1 << PB0 ); // Toggle the LED
TCNT1 = 49911; // initial value to get 1Hz in normal mode
TIFR = (1<< TOV1); // clear the TOF flag (by writing a logic
// one to its bit location !)
}
}
} // as exercise repeat normal mode example with interrupt.
24
Counter
 When we are using external source of clock to the timer it’s
called a counter.
 Timer0 & Timer1 only in AVR have this feature by appropriate
chose of CS bit.

25
Counter
GPIO Pin Alternative Function

PB0 T0

PB1 T1

 Assuming that a 1 Hz clock pulse is fed into pin T0.


26
Counter
 Final Report:

 Assuming that a 1 Hz clock pulse is fed into pin T0.

 Make a simple clock.

 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.

 The counter counts from BOTTOM to TOP then restarts from


BOTTOM.

 TOP in Fast PWM may be OCR1x , ICR1, 0xFF, 0x1FF, and 0x3FF.

 In non-inverting Compare Output mode, OC1x is cleared on the


compare match between TCNT1 and OCR1x, and set at
BOTTOM.
28
Fast PWM Mode
 With CTC mode we generate a square wave with 50% duty cycle only.

 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>

void timer1_init( void )


{
TCCR1B |= (1<<WGM13) |(1<<WGM12); // Configure timer1 for Phase
TCCR1A |= (1<<WGM11); // Fast PWM mode 14
TCCR1A |= (1<< COM1A1)|(1<<COM1B1); // Enable Timer1 compare
// channels A and B in non-inverting mode
ICR1 = 249; // Set ICR1 (Top) value to 125 Hz
OCR1A = 199; // Set the duty cycle to 80%
OCR1B = 74; // Set the duty cycle to 30%
TCCR1B |= ( (1<< CS12); // Set up timer at Fcpu / 256
}

int main( void )


{
DDRD |= (1 << PD5) |(1 << PD4); // Set LED as output
timer1_init( );
while ( 1 );
} 33
Phase Correct PWM
 There is a problem with Fast PWM mode with controlling stepper
motors.

 Note that the rising edge of the pulse on OC1x always occurs at
the same point.

 This causes the center of the pulse to shift. (phase shift)

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)
𝑇𝑜𝑝

 Example: Assuming XTAL 8Mz, generate two waves with


frequency of 125 Hz on OC1A and OC1B using mode 10, non-
inverted mode and prescaler = 256 with 80% and 40% duty
cycle, respectively.
8𝑀
 Solution: 125 = 2 𝑥 256 𝑥 𝑇𝑜𝑝
 Top = 125
𝑂𝐶𝑅1𝐴
 80 =
125
x 100  OCR1A = 100
𝑂𝐶𝑅1𝐵
 40 =
125
x 100  OCR1B = 50
38
Phase Correct PWM
//The code for Phase Correct PWM ICR1 as TOP mode 10 t10.c
# include <avr/io.h>
void Timer1_init( void)
{
TCCR1B |= (1<<WGM13; //Configure timer1 for Phase Correct PWM mode 10
TCCR1A |= (1<<WGM11);
TCCR1A |= (1<< COM1A1)|(1<<COM1B1); // Enable Timer1 compare
// channels A and B in non-inverting mode
ICR1 = 125; // Set ICR1 (Top) value to 125 Hz
OCR1A = 100; // Set the duty cycle to 80%
OCR1B = 50; // Set the duty cycle to 40%
TCCR1B |= (1<< CS12); // Set up timer at Fcpu / 256
}
int main( void )
{
DDRD |= (1 << PD5) |(1 << PD4); // Set LED as output
timer1_init();
while ( 1 ) { }
} 39
References
 Dean Camera, “Newbie's Guide to AVR Timers”, March
15, 2015.

 Muh. A. Mazidi, Sar. Namimi and Sep. Namimi, ”The


AVR Microcontroller and Embedded Systems using
Assembly and C ”, Prentice Hall , 2011.

 Atmega16 manual.

 Using AVR Counter.


40

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy