You are on page 1of 13

Microcontroller’s Timer/Counter

Sistemas Embebidos
Oscar Acevedo, PhD
μC Clock System
Timer/Counter

• The timer is a very common peripheral found in microcontrollers


• ATMega328 has 3 timers/counters

• Timer/Counter0 (TC0) is a general purpose 8-bit Timer/Counter module typically used for:
• Count
• Compare
• PWM
Timer/Counter0 Block Diagram
Timer/Counter

• TC0 can be clocked internally, via the prescaler, or by an external clock source on the T0
pin (TCCR0B register)
Timer/Counter

• The 8-bit comparator continuously compares TCNT0 with the Output Compare Registers
(OCR0A and OCR0B)
• 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
• If the corresponding interrupt is enabled, the Output Compare Flag generates an Output
Compare interrupt.
Timer/Counter Modes of Operation

• Normal Mode: the counting direction is always up, and no counter clear is performed
• The counter simply overruns when it passes its maximum 8-bit value and then restarts from
0x00
• The Timer/Counter Overflow Flag (TOV1) will be set in the same clock cycle in which the
TCNT becomes zero
• A new counter value can be written anytime
• The Output Compare unit can be used to generate interrupts at some given time
Timer/Counter Modes of Operation

• Clear timer on compare match Mode (CTC):


• The OCR0A defines the top value for the counter
• The counter is cleared to ZERO when the counter value TCNT matches the OCR0A
• An interrupt can be generated each time the counter value reaches the OCR0A value by
setting the OCF0A Flag
• For generating a waveform output in CTC mode, the OC0A output can be set to toggle its
logical level on each compare match by writing the Timer/Counter Control Register A to toggle
mode
Timer/Counter Modes of Operation

• Clear timer on compare match Mode (CTC)


Timer/Counter Modes of Operation

• Additional Modes:
• Fast PWM
• Phase correct PWM
Timer/Counter Register List

• TC0 Control Register A (TCCR0A): configure operation mode for channel A and B

• TC0 Control Register B (TCCR0B): additional configuration options


• Clock select

• TC0 Interrupt Mask Register


• TC0 Counter Value Register (TCNT0)
• TC0 Output Compare Register A (OCR0A)
• TC0 Output Compare Register B (OCR0B)
• TC0 Interrupt Flag Register (TIFR0)
TCCR0B Clock Select
Fin de la presentación

You might also like