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

Timers of ATmega328P

Embed Size (px)

DESCRIPTION

This document explains the timers and counters of ATmega328P with certain examples

Citation preview

Page 1: Timers of ATmega328P

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/pg35r6f2. Introduction to embedded system – using ANSI C and the Arduino

development Environment, David Russell, 2010

Page 2: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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

2

Page 3: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Demodulation

• Recover the desired analog output from PWM waveform: use a simple Low-Pass Filter

• High cutoff frequency is • Requirement:

3

Analog outputPWM square wave

Page 4: Timers of ATmega328P

TECH 3157 Microcontroller Applications 4

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.

Page 5: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Pin outputs of Timers/Counters

5

Pin5

Pin6

 

Pin3

Pin4

Pin11

Pin10

Pin9 

Pinx in red is the pin number in the Arduino board

Page 6: Timers of ATmega328P

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.

6

Page 7: Timers of ATmega328P

TECH 3157 Microcontroller Applications 7

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

Page 8: Timers of ATmega328P

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

Page 9: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Clock Select

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

9

Page 10: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Timer/Counter 0

10

Page 11: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Counter/Timer 0 Registers

11

TCCR0A

TCCR0B

TCCR0B

Page 12: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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

Page 13: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Waveform Generation mode WGM02:0

13

Page 14: Timers of ATmega328P

TECH 3157 Microcontroller Applications 14

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 0 Normal port operation, OC0A disconnected.

0 1 Toggle OC0A on Compare Match

1 0 Clear OC0A on Compare Match

1 1 Set OC0A on Compare Match

The N variable represents the prescale factor

Page 15: Timers of ATmega328P

TECH 3157 Microcontroller Applications 15

Fast PWM Mode (1)TOP = 255

TOP = OCR0A

WGM mode 3 WGM mode 7

Page 16: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Fast PWM Mode (2)

16

TCCR0A

Page 17: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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.

17

WGM mode 1Top = 255

WGM mode 5Top = OCR0A

Page 18: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Phase Correct PWM Mode (2)

18

TCCR0A

Page 19: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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 pre-scaler = 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:

19

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(){}

Page 20: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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

20

Page 21: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Timer/Counter 1 Registers

21Other registers see the textbook/datasheet

Page 22: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Timer/Counter2

• Timer/Counter2: 8-bit, pre-scaler is different, provides asynchronous operation.

• Clock prescaler is different from Timer0&1

22

Page 23: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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

23

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;}

Page 24: Timers of ATmega328P

TECH 3157 Microcontroller Applications

Example 3

• Using waveform generation mode 3 (Phase Correct 10-bit PWM) to generate a waveform with different duty ratio

24

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; }

Page 25: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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.

25

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);}

Page 26: Timers of ATmega328P

TECH 3157 Microcontroller Applications

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