Cornell University
Electrical and Computer Engineering 4760
AVR mega644/1284
Hardware Timers
Introduction
All of the AVR MCUs have hardware support for measuring time. The Mega644 has three hardware timers, two 8-bit (timer0 and timer2) and one 16-bit timer (timer1). The Mega1284 has four hardware timers, two 8-bit (timer0 and timer2) and two 16-bit timers (timer1 and timer3). Each timer has a prescalar. At each prescaled clock tick the timer counter register is automatically incremented by one. Each timer can be started/stopped, written with a new value, and read. Each timer can be used in several different ways. A short list follows for each timer.
Timer0 and timer2 functions
- Clock Sources
- Timer0 can use a prescalar or increment based on input from an i/o pin (rising/falling edge).
Prescalar divides the cpu clock by: off, 1, 8, 64, 256, 1024.
- Timer2 can use a prescalar or increment based on a separate crystal.
Prescalar divides the cpu clock by: off, 1, 8, 32, 64, 128, 256, 1024.
- 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 A (OCR0A or OCR2A).
- 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.
- Count to a preset value specified in the Output Compare Register B (OCR0B or OCR2B).
- Reaching the preset value can trigger an interrupt if enabled.
- 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.
- Two PWM signals from each counter are output to four pins based on preset values (OCR0A,B OCR2A,B) which can be changed on every PWM cycle.
- An interrupt can be generated on every PWM cycle if enabled.
- In PWM mode, a timer cannot do anything else!
Timer1 and Timer3 functions (644 does not have timer3)
- Clock Sources
- Timer1/3 can use a prescalar or increment based on input from an i/o pin (rising/falling edge).
Prescalar divides the cpu clock by: off, 1, 8, 64, 256, 1024.
- 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 A or B (OCR1A, OCR1B, OCR3A, OCR3B).
- Reaching any preset value can trigger interrupts if enabled.
- Reaching the preset value in OCRxA 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 OCRxA or OCRxB 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.
- Two PWM signals for each timer are output to pins based on preset value (OCRxA or OCRxB) which can be changed on every PWM cycle.
- An interrupt can be generated on every PWM cycle if enabled.
- In PWM mode, a timer cannot do anything else!
- Copy the value in the timer counter (current time) into an input capture register (ICR1, ICR3) 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 (timer1 only!).
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.
Examples Using Timers
Copyright Cornell University Jan 2009