Cornell University
Electrical Engineering 476
AVR microcontrollers
Hardware Timers
Introduction
All of the AVR MCUs have hardware support for measuring time. The Mega32 has three hardware timers, two 8-bit (timer0 and timer2) and one 16-bit timer (timer1). Each timer has a prescalar that divides the cpu clock by 1, 8, 64, 256, 1024. At each prescaled clock tick the timer counter register is automatically incremented by one. In addition, each timer can be stopped. Each timer can be used is several different ways. A short list follows for each timer.
Timer0 and timer2
- Clock Sources
- Timer0 can use a prescalar or increment based on input from an i/o pin.
- Timer2 can use a prescalar or increment based on a separate crystal.
- Count to overflow.
- Count to 255, then roll over to zero and keep counting.
- Overflow may trigger an interrupt if enabled.
- Count to a preset value specified in the Output Compare Register (OCR0 or OCR2).
- Reaching the preset value can trigger an interrupt if enabled.
- Reaching the preset value can cause the timer to be reset. Counting to a preset value, folllowed by reset (and automatic continued counting) is the best way to generate a precision time base.
- Reaching the preset value can cause an output pin to be set, cleared, or toggled. This feature can be used to generate a square wave of aribtrary frequency with no software overhead.
- Pulse-width modulator (PWM) mode.
- A PWM signal is output to a pin based on a preset value which can be changed on every PWM cycle.
- An interrupt can be generated on every PWM cycle if enabled.
Timer1
- Clock Sources
- Timer1 can use a prescalar or increment based on input from an i/o pin.
- Count to overflow.
- Count to
216-1, then roll over to zero and keep counting.
- Overflow may trigger an interrupt if enabled.
- Count to two preset values specified in Output Compare Register 1A or 1B (OCR1A, OCR1B).
- Reaching either preset value can trigger interrupts if enabled.
- Reaching the preset value in OCR1A can cause the timer to be reset. Counting to a preset value, folllowed by reset (and automatic continued counting) is the best way to generate a precision time base.
- Reaching the preset value in OCR1A or OCR1B can cause an output pin to be set, cleared, or toggled. This feature can be used to generate square waves of aribtrary frequencies with no software overhead.
- Pulse-width modulator (PWM) mode.
- A PWM signal is output to a pin based on a preset value which can be changed on every PWM cycle.
- An interrupt can be generated on every PWM cycle if enabled.
- Copy the value in the timer counter (current time) into an input capture register (ICR1) when an external event occurs.
- The event can be a logic transition on one i/o pin.
- The event can be a change in state of a built-in analog comparator. This allows the cpu to determine when an analog voltage equals an arbitrary reference voltage.
- This feature allows the cpu to determine the exact time of an external event.
Using Timers
Copyright Cornell University Jan 2008