AVR® MCU Timer Overview

Last modified by Microchip on 2023/11/09 09:02

Timers are a very useful feature of a microcontroller for counting pulses on an input pin. When driven by the instruction clock, they can become an accurate time base. AVR® devices have 8-bit-wide and 16-bit-wide timers and they offer different features based on the device. A very typical set of timers can be found in the AVR ATmega328PB microcontroller. This device has five Timer/Counters as described here:

Timer 0TC08-bit Timer/counter with Pulse Width Modulation (PWM)
Timer 1TC116-bit Timer/counter with PWM and Asynchronous Operation
Timer 2TC28-bit Timer/counter with PWM and Asynchronous Operation
Timer 3TC316-bit Timer/counter with PWM and Asynchronous Operation
Timer 4TC416-bit Timer/counter with PWM and Asynchronous Operation

Definitions

Register Nomenclature
BOTTOMThe counter reaches the BOTTOM when it becomes zero (0x00 for 8-bit counters and 0x0000 for 16-bit counters)
MAXThe counter reaches its maximum value when it becomes 0x0F (15 decimal) for 8-bit counters and 0x00FF (255 decimal) for 16-bit counters
TOPThe counter reaches the TOP when its value becomes equal to the highest value possible. The TOP value can be assigned a fixed value MAX or the value stored in the OCRxA register. This assignment is dependent upon the mode of operation

Back to top

TC0 - 8-bit Timer/Counter with PWM

Timer/Counter0 (TC0) is a general-purpose 8-bit Timer/Counter module, with two independent Output Compare Units and PWM support.

Back to top

TC0 Registers

The Timer/Counter 0 register (TCNT0) and Output Compare TC0x registers (OCR0x) are 8-bit registers. Interrupt request signals are all visible in the Timer Interrupt Flag Register 0 (TIFR0). All interrupts are individually masked with the Timer Interrupt Mask Register 0 (TIMSK0).

TCCR0B

TC0 Timer/Counter Clock Sources

TC0 can be clocked by an internal or an external clock source. The clock source is selected by writing to the Clock Select (CS02:0) bits in the Timer/Counter Control Register (TCCR0B).

Bits 2:0 – CS0n: Clock Select [n = 0..2]

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

CS02CS01CS00Description
000No Clock source (Timer stopped)
001clkio/1 (No prescaling)
010clkio/8 (From prescaler)
011clkio/64 (From prescaler)
100clkio/256 (From prescaler)
101clkio/1012 (From prescaler)
110External clock source on T0 pin (clock on falling edge)
111External clock source on T0 pin (clock on rising edge)

TC0 Counter Unit

Depending on the mode of operation used, T0 is cleared, incremented, or decremented at each timer clock (clkT0). clkT0 can be generated from an external or internal clock source, selected by the Clock Select bits (CS0[2:0]).

The counting sequence is determined by the setting of the WGM01 and WGM00 bits located in the T0 Control Register A (TCCR0A) and the WGM02 bit located in the Timer/Counter Control Register B (TCCR0B).

Bits 1:0 – WGM0n: Waveform Generation Mode [n = 1:0]

Combined with the WGM02 bit found in the TCCR0B Register, these bits control the counting sequence of the counter, the source for maximum (TOP) counter value and what type of waveform generation to be used. Modes of operation supported by the Timer/Counter unit are: Normal mode (counter), Clear Timer on Compare Match (CTC) Mode and two types of Pulse Width Modulation (PWM) modes.

Table 1-2 Waveform Generation Mode Bit Description

ModeWGM2:0Mode of OperationTOPOCR0x UpdateTOV flag set on
00 0 0Normal0xFFImmediateMAX
10 0 1PWM Phase Correct0xFFTOPBOTTOM
20 1 0CTCOCRAImmediateMAX
30 1 1Fast PWM0xFFBOTTOMMAX
41 0 0Reserved~~~
51 0 1PWM Phase CorrectOCRATOPBOTTOM
61 1 0Reserved~~~
71 1 1Fast PWMOCRABOTTOMMAX

1. MAX = 0xFF
2. BOTTOM = 0x00

Modes of Operation for TC0

The mode of operation determines the behavior of TC0 and the Output Compare pins. It is defined by the combination of the Waveform Generation mode bits and Compare Output mode bits in the Timer/Counter control Registers A and B (TCCR0B.WGMn2, TCCR0A.WGM01, TCCR0A.WGM00 and TCCR0A.COM0x[1:0]).

Available modes of operation for TC0 are:

  • Normal Mode
  • Clear Timer on Compare Match (CTC) Mode
  • Fast PWM Mode
  • Phase Correct PWM Mode

Clear Timer on Compare Match Mode

In Clear Timer on Compare or CTC mode (WGM0[2:0]=0x2), the OCR0A Register is used to manipulate the counter resolution: 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.

The counter value (TCNT0) increases until a compare match occurs between TCNT0 and OCR0A and then counter (TCNT0) is cleared. An interrupt can be generated each time the counter value reaches the TOP value by setting the OCF0A Flag. If the interrupt is enabled, the interrupt handler routine can be used for updating the TOP value.

The waveform frequency is defined by the following equation:

Waveform Frequency Equation

N represents the prescaler factor (1, 8, 64, 256, or 1024).

Back to top

TC1, TC3, & TC4 - 16-bit Timer/Counters with PWM

The 16-bit Timer/Counter units allow accurate program execution timing (event management), wave generation and signal timing measurement.

Registers (TC1, TC3, TC4)

  • The Timer/Counter (TCNTn), Output Compare Registers (OCRA/B) and Input Capture Register (ICRn) are all 16-bit registers.
  • The Timer/Counter Control Registers (TCCRnA/B) are 8-bit registers and have no CPU access restrictions.
  • Interrupt requests (abbreviated to Int.Req. in the block diagram) signals are all visible in the Timer Interrupt Flag Register (TIFRn). All interrupts are individually masked with the Timer Interrupt Mask Register (TIMSKn).

Timer/Counter Clock Sources (TC1, TC3, TC4)

The Timer/Counter can be clocked by an internal or an external clock source. The clock source is selected by the Clock Select logic which is controlled by the Clock Select bits in the timer/Counter control Register B (TCCRnB.CS[2:0]).

Timer/Counter Clock Sources (TC1, TC3, TC4)

Bits 2:0 – CS[2:0]: Clock Select [n = 0..2]
The three Clock Select bits select the clock source to be used by the Timer/Counter.

CS02CS01CS00Description
000No Clock source (Timer stopped)
001clkio/1 (No prescaling)
010clkio/8 (From prescaler)
011clkio/64 (From prescaler)
100clkio/256 (From prescaler)
101clkio/1012 (From prescaler)
110External clock source on T0 pin (clock on falling edge)
111External clock source on T0 pin (clock on rising edge)

Counter Unit (TC1, TC3, TC4)

TC1, TC3 and TC4 are programmable 16-bit bi-directional counters.

Each 16-bit counter is mapped into two 8-bit I/O memory locations, Counter High (TCNTnH) containing the upper eight bits of the counter, and Counter Low (TCNTnL) containing the lower eight bits.

Depending on the selected mode of operation, the counter is cleared, incremented, or decremented at each timer clock (clkTn). The clock clkTn can be generated from an external or internal clock source, as selected by the Clock Select bits in the Timer/Counter Control Register B (TCCRnB.CS[2:0]).

The counting sequence is determined by the setting of the Waveform Generation mode bits in the Timer/Counter Control Registers A and B (TCCRnB.WGM[3:2] and TCCRnA.WGM[1:0]).

Modes of Operation (TC1, TC3, TC4)

The mode of operation is determined by the combination of the Waveform Generation mode (WGM[3:0]) and Compare Output mode (TCCRnA.COMx[1:0]) bits.

Available modes of operation are:

  • Normal Mode
  • Clear Timer on Compare Match (CTC) Mode
  • Fast PWM Mode
  • Phase Correct PWM Mode
  • Phase and Frequency Correct PWM Mode

Fast PWM Mode

The Fast Pulse Width Modulation or Fast PWM modes (modes 5, 6, 7, 14, and 15, WGM[3:0]= 0x5, 0x6, 0x7, 0xE, 0xF) provide a high-frequency PWM waveform generation option. The Fast PWM differs from the other PWM options by its single-slope operation. The counter counts from BOTTOM to TOP then restarts from BOTTOM.

In Fast PWM mode the counter is incremented until the counter value matches either one of the fixed values 0x00FF, 0x01FF, or 0x03FF (WGM[3:0] = 0x5, 0x6, or 0x7), the value in ICRn (WGM[3:0]=0xE), or the value in OCRnA (WGM[3:0]=0xF). The counter is then cleared at the following timer clock cycle.

The PWM frequency for the output can be calculated by the following equation:

PWM Frequency Equation

Note:
• The “n” in the register and bit names indicates the device number (n = 0 for Timer/Counter 0) and the “x” indicates Output Compare unit (A/B).
• N represents the prescale divider (1, 8, 64, 256, or 1024).

Back to top

TC2 - 8-bit Timer/Counter2 with PWM and Asynchronous Operation

Timer/Counter2 (TC2) is a general-purpose, dual-channel, 8-bit Timer/Counter module.

TC2 Registers

The Timer/Counter (TCNT2) and Output Compare Register (OCR2A and OCR2B) are 8-bit registers. Interrupt request signals are all visible in the Timer Interrupt Flag Register (TIFR2). All interrupts are individually masked with the Timer Interrupt Mask Register (TIMSK2).

TC2 Registers

TC2 Clock Sources

TC2 can be clocked by an internal synchronous or an external asynchronous clock source.

The three Clock Select bits (CS2:CS0) select the clock source to be used by the Timer/Counter.

CS02CS01CS00Description
000No Clock source (Timer stopped)
001clkio/1 (No prescaling)
010clkio/8 (From prescaler)
011clkio/64 (From prescaler)
100clkio/256 (From prescaler)
101clkio/1012 (From prescaler)
110External clock source on T0 pin (clock on falling edge)
111External clock source on T0 pin (clock on rising edge)

TC2 Counter Unit

Depending on the mode of operation used, the counter is cleared, incremented, or decremented at each timer clock (clkT2). clkT2 can be generated from an external or internal clock source, selected by the Clock Select bits (CS2[2:0]).

The counting sequence is determined by the setting of the WGM21 and WGM20 bits located in the Timer/Counter Control Register (TCCR2A) and the WGM22 bit located in the Timer/Counter Control Register B (TCCR2B).

TC2 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 (WGM2[2:0]) and Compare Output mode (COM2x[1:0]) bits.

Available modes of operation are:

  • Normal Mode
  • Clear Timer on Compare Match (CTC) Mode
  • Fast PWM Mode
  • Phase Correct PWM Mode

Normal Mode

In the Normal mode (WGM22:0 = 0) the counting direction is always up (incrementing) without having the counter cleared. The counter will roll over to 0c00 when it passes its maximum 8-bit value (TOP = 0xFF).

In normal operation, the Timer/Counter Overflow Flag (TOV2) will be set in the same timer clock cycle as the TCNT2 becomes zero. The TOV2 Flag, in this case, behaves like a ninth bit, except that it is only set, not cleared. However, combined with the timer overflow interrupt that automatically clears the TOV2 flag, the timer resolution can be increased by software. There are no special cases to consider in the Normal mode, a new counter value can be written at any time.

Regardless of which mode is used the programmer needs to remember two things:

  1. The timer has to be started by selecting the clock source.
  2. If interrupts are used, they must be enabled.

Back to top