Professional Documents
Culture Documents
Objectives:
Demodulation
Recover the desired analog output from PWM
waveform: use a simple Low-Pass Filter
High cutoff frequency is
Requirement:
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
Pin3
Pin4
Pin5
Pin6
Pin11
Pin10
Pin9
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
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.
Timer/Counter 0
10
Counter/Timer 0 Registers
TCCR0B
TCCR
0A
TCCR
0B
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.
12
13
14
WGM mode 3
TECH 3157 Microcontroller Applications
TOP = OCR0A
WGM mode 7
15
16
WGM mode 5
Top = OCR0A
17
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
20
Timer/Counter 1 Registers
21
Timer/Counter2
Timer/Counter2: 8-bit, pre-scaler is
different, provides asynchronous operation.
Clock prescaler is different from Timer0&1
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;
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.
26