You are on page 1of 26

Timers and Counters

Objectives:

Understand pulse width modulation


Understand the timers of the ATmega328P and their associated registers
Understand the operation modes of the 3 timers in ATmega328P
Select the proper timer and determine the register values for given
applications requirements
Learn to program timers to generate time delays and waveforms.
References:
1. ATmega328P datasheet, available at http://tinyurl.com/pg35r6f
2. Introduction to embedded system using ANSI C and the Arduino
development Environment, David Russell, 2010

Pulse Width Modulation (PWM)


In PWM, the average output voltage is controlled by the
pulse width, i.e. that duty ratio for a given frequency.
Typically the PWM waveform frequency should be much
larger than the frequency of the represented analog output.
Advantage: control circuits in digital

TECH 3157 Microcontroller Applications

Demodulation
Recover the desired analog output from PWM
waveform: use a simple Low-Pass Filter
High cutoff frequency is
Requirement:

PWM square wave

TECH 3157 Microcontroller Applications

Analog output

Timers/Counters in ATmega328P
ATmega328P has three timers/counters:
8-bit Timer0/Counter0 with PWM
16-bit Timer1/Counter1 with PWM
8-bit Timer2/Counter2 with PWM and Asynchronous operation

The related registers are


TCNTn (Timer/Counter register 8-bit/16-bit)
TIFRn (Timer/Counter Interrupt Flag Register)
TCCRn (Timer Counter Control register)
OCRnx (Output Compare register )
ICR1 (Input Capture Register only for Timer/Counter1 )
Where a lower case n replaces the Timer/Counter number, a lower case x
replaces the Output Compare Unit, in this case Compare Unit A or Compare Unit
B.
TECH 3157 Microcontroller Applications

Pin outputs of Timers/Counters

Pin3
Pin4

Pin5
Pin6

Pin11
Pin10
Pin9

Pinx in red is the pin number in the Arduino board


TECH 3157 Microcontroller Applications

Timer/Counter 0
Timer/Counter0 is a general purpose 8-bit
Timer/Counter module, with two independent
Output Compare Units, and with PWM support.
It allows accurate program execution timing
(event management) and wave generation.
Often used delay() function relies on timer 0
interrupt.
A simplified block diagram of the 8-bit
Timer/Counter is shown in next slide.
TECH 3157 Microcontroller Applications

Figure 8-bit Timer/Counter diagram (Timer0 and Timer2)


TECH 3157 Microcontroller Applications

Timer/Counter 0 (cont.)
The Timer/Counter (TCNT0) and Output Compare Registers (OCR0A and
OCR0B) are 8-bit registers.
The double buffered Output Compare Registers (OCR0A and OCR0B) are
compared with the Timer/Counter value TCNT0 at all times.
The result of the compare can be used by the Waveform Generator to generate a
PWM or variable frequency output on the Output Compare pins (OC0A and
OC0B).
Whenever TCNT0 equals OCR0A or OCR0B, the comparator signals a match. A
match will set the Output Compare Flag (OCF0A or OCF0B) at the next timer clock
cycle.
The Waveform Generator uses the match signal to generate an output according
to operating mode set by the WGM02:0 bits and Compare Output mode
(COM0x1:0) bits
The Timer/Counter can be clocked internally, via the prescaler, or by an external
clock source on the T0 pin. The output from the Clock Select logic is referred to as
the timer clock (clkT0).
The Clock Select logic block controls which clock source and edge the
Timer/Counter uses to increment (or decrement) its value.
The Timer/Counter is inactive when no clock source is selected.
TECH 3157 Microcontroller Applications

Clock Select
CS02:0: Clock Select
The three Clock Select bits select the clock source to be
used by the Timer/Counter.

TECH 3157 Microcontroller Applications

Timer/Counter 0

TECH 3157 Microcontroller Applications

10

Counter/Timer 0 Registers
TCCR0B

TCCR
0A

TCCR
0B

TECH 3157 Microcontroller Applications

11

Modes of Operation
The mode of operation, i.e., the behavior of the Timer/Counter and the
Output Compare pins, is defined by the combination of the Waveform
Generation mode (WGM02:0) and Compare Output mode (COM0x1:0) bits.
Normal mode: In this mode the counting direction is always up
(incrementing), and no counter clear is performed. The counter simply
overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then
restarts from the bottom (0x00).
Clear Timer on Compare Match (CTC) Mode: the counter is cleared to
zero when the counter value (TCNT0) matches the OCR0A. The OCR0A
defines the top value for the counter, hence also its resolution. This mode
allows greater control of the compare match output frequency.
Fast PWM Mode: provides a high frequency PWM waveform generation
option. Single-slope operation. The counter counts from BOTTOM to TOP
then restarts from BOTTOM.
Phase Correct PWM Mode: based on a dual-slope operation. The counter
counts repeatedly from BOTTOM to TOP and then from TOP to BOTTOM.

TECH 3157 Microcontroller Applications

12

Waveform Generation mode WGM02:0

TECH 3157 Microcontroller Applications

13

Clear Timer on Compare Match (CTC) Mode


The counter value (TCNT0) increases until a compare match occurs between TCNT0 and
OCR0A(or OCR0B), and then counter (TCNT0) is cleared.
The OC0A value will not be visible on the port pin unless the data direction is set to output.
COM0x1COM0x0 Description
0

Normal port operation, OC0A disconnected.

Toggle OC0A on Compare Match

Clear OC0A on Compare Match

Set OC0A on Compare Match

TECH 3157 Microcontroller Applications

The N variable represents the prescale factor

14

Fast PWM Mode (1)


TOP = 255

WGM mode 3
TECH 3157 Microcontroller Applications

TOP = OCR0A

WGM mode 7
15

Fast PWM Mode (2)


TCCR0
A

TECH 3157 Microcontroller Applications

16

Phase Correct PWM Mode (1)

The phase correct PWM mode is based on a dual-slope operation.


The counter counts repeatedly from BOTTOM to TOP and then from TOP to BOTTOM.
WGM mode 1
Top = 255

TECH 3157 Microcontroller Applications

WGM mode 5
Top = OCR0A

17

Phase Correct PWM Mode (2)


TCCR0
A

TECH 3157 Microcontroller Applications

18

Example 1
User Timer0 to toggle pin 6 (PD6/OC0A) continuously every 1 ms. Notice
the Arduino has a system clock of 16MHz.
Solution: Since the output waveform toggles every 1 ms, i.e. 1 kHz, Timer0
is 8-bit and the t_clk = 1/16 MHz, we have to use the pre-scaler. Choose prescaler = 64, i.e CS02-0 = 011. Now we need to use either phase correct
PWM or Fast PWM. If Fast PWM is adopted and we choose OC0A toggle
mode (toggle on compare match), the configuration should be as follows:
void setup()
{
/* toggle mode, fast PWM. WGM02:0 = 111, COM0A1:0= 01 (use OC0A) */
TCCR0A = 0b01000011; //0x43
TCCR0B = 0b00001011; //0x0B
OCR0A = 127; // 1 kHz = 16MHz/(2*64*(OCR0A+1))
}
void loop()
{}
TECH 3157 Microcontroller Applications

19

Timer/Counter 1: a 16-bit timer/counter

TECH 3157 Microcontroller Applications

20

Timer/Counter 1 Registers

Other registers see the textbook/datasheet


TECH 3157 Microcontroller Applications

21

Timer/Counter2
Timer/Counter2: 8-bit, pre-scaler is
different, provides asynchronous operation.
Clock prescaler is different from Timer0&1

TECH 3157 Microcontroller Applications

22

Example 2
Output a PWM waveform with 50% duty ratio and 0.5
Hz frequency. The system clock frequency of Arduino
boards is 16 MHz.
We use Timer/Counter1 phase correct PWM to generate it.
Use waveform generation mode 11 and toggle mode (i.e.
Toggle OC1A on Compare Match).
void setup()
{
DDRB = 0x02;

//set pin 9 as output; pin 9 (OC1A) is connected to an LED

/*set operation Toggle mode; WGM13:0 = 1011 phase correct PWM*/


TCCR1A = 0x43;
TCCR1B = 0x15;
OCR1AH = 0x1E; // 0x1E84 = 7812 -> 1s
OCR1AL = 0x84;
}
TECH 3157 Microcontroller Applications

23

Example 3
Using waveform generation mode 3
(Phase Correct 10-bit PWM) to generate a
waveform with different duty ratio
void setup()
{
/* non-inverting mode, WGM13:0 = 0011 phase correct 10-bit PWM */
TCCR1A = 0x83; //0b10000011 = 0x83
TCCR1B = 0x05; // 0b00000101= 0x05
/* 0x01FF = half of the top value 0x3FF. The duty ratio can be adjusted
by applying different value to OCR1A. OCR1A should be less than TOP,
i.e. 0x3FF */
OCR1AH = 0x01;
OCR1AL = 0xFF;
}
TECH 3157 Microcontroller Applications

24

Example 4
Use Timer0 as an event counter. Assume T0 (PD4/pin4) is connected to an external clock
or debounced switch. Display the number of pulses counted on Serial monitor window.
unsigned int count; //the count of events

void setup()
{
PORTD |= 0x10; //activate pull-up of PD4
DDRD &= 0xEF; //set pin 4 (PD4) as input
TCCR0A = 0x00; //normal mode
TCCR0B = 0x06; //output clock source, falling edge
TCNT0 = 0;
Serial.begin(9600);
}
void loop()
{
char message[80];
count = TCNT0;
sprintf(message, "\nThe number of events: %d", count);
Serial.write(message);
}
TECH 3157 Microcontroller Applications

25

Assignment
Please read chapter 7 in the textbook.
Problems:
1. How many timers are in the ATmega328P? Briefly describe their functions.
2. What is the highest pre-scaler value for each of the timers?
3. Assuming that system frequency is 16MHz and we are generating a
square wave on pin 6.
4. find the lowest and highest square wave frequency that we can generate
using Normal mode
5. find the lowest and highest square wave frequency if we waveform
generation mode is 5, COM0A1:0 = 01 and OCR0A = 0x5F.
6. Assume pin 5 is connected to an external clock and we want to use Timer1
as an event counter to count the number of pulses coming to the pin.
Determine the register values of Timer1.
Please complete the homework assignment. Assignment solution will be
posted on e-Learning next week.

TECH 3157 Microcontroller Applications

26

You might also like