In embedded systems, time plays a crucial role. Whether it’s generating delays, measuring pulse width, counting external events, or creating PWM signals for motor control — the microcontroller must handle timing operations with accuracy and reliability. The AVR family of microcontrollers comes equipped with powerful timer/counter modules that can perform all these timing-related tasks automatically in the background while the CPU executes other instructions. Understanding how timers work is one of the most important skills in embedded programming because many advanced operations like ADC sampling, sensor interfacing, or motor speed control depend on precise timing. The AVR timers are flexible, programmable, and can be configured in various modes depending on the application. In this article, we will explore the concept of timers, their internal working, the different timer modules available in AVR microcontrollers, and how they can be programmed in C. This is the first step towards mastering advanced topics like PWM generation, event counting, and interrupt-driven timing control.
1. What is a Timer in Microcontroller?
A Timer is a hardware peripheral inside the microcontroller that counts clock pulses either from the internal system clock or from an external source. When configured properly, timers can be used to measure time intervals, generate precise delays, count external events, or trigger actions after a fixed period. Each timer in AVR operates independently and can be set to count up or down depending on the mode.
2. Basic Working Principle of Timer
Internally, a timer consists of a register (called a counter) that increments automatically with every incoming clock pulse. When the counter reaches its maximum value, it either resets to zero or triggers an interrupt, depending on the configuration. This automatic counting process allows programmers to generate time delays without writing long software loops.
The basic components of a timer system are:
- Counter Register (TCNTx): Holds the current timer count value.
- Control Register (TCCRx): Used to set prescaler, mode, and clock source.
- Output Compare Register (OCRx): Stores the compare value for generating PWM or timed interrupts.
- Timer Overflow Flag (TOVx): Indicates when the counter overflows.
- Prescaler: Divides the system clock to slow down the counting rate for longer intervals.
3. Timer vs Counter Mode
An AVR timer can operate in two modes:
| Mode | Clock Source | Purpose |
|---|---|---|
| Timer Mode | Internal CPU Clock | Measures time intervals or generates delays. |
| Counter Mode | External Clock Input | Counts external pulses or events. |
The difference lies in the source of clock pulses. In Timer Mode, the internal clock of the CPU drives the timer, while in Counter Mode, an external signal (from sensors, encoders, etc.) is counted.
4. Types of Timers in AVR (Example: ATmega16)
The ATmega16 microcontroller includes three main timer/counter units:
| Timer | Bit Size | Registers | Main Uses |
|---|---|---|---|
| Timer0 | 8-bit | TCNT0, TCCR0, OCR0 | Simple delays, PWM generation, counters |
| Timer1 | 16-bit | TCNT1H/L, TCCR1A/B, OCR1A/B, ICR1 | High-precision timing, servo control, event measurement |
| Timer2 | 8-bit | TCNT2, TCCR2, OCR2 | Asynchronous operation with external clock (like 32.768 kHz crystal) |
5. Prescaler – The Heart of Timing Control
A prescaler is a programmable divider that reduces the input clock frequency before it reaches the timer.
For example, if the CPU clock is 8 MHz and the prescaler is set to 64, the timer will count at 8,000,000 / 64 = 125,000 Hz.
This allows flexible time control without changing the main CPU clock.
The available prescaler options typically include:
| Prescaler Value | Effect |
|---|---|
| 1 | No division (fastest counting) |
| 8 | Divides by 8 |
| 64 | Divides by 64 |
| 256 | Divides by 256 |
| 1024 | Divides by 1024 (slowest counting) |
6. Timer Overflow Concept
When the timer register reaches its maximum value (255 for 8-bit or 65535 for 16-bit), it overflows and starts again from zero. This event can generate an interrupt that allows the CPU to perform specific actions such as toggling LEDs or updating counters.
// Example: Simple timer overflow initialization
#include <avr/io.h>
int main(void)
{
TCCR0 = (1<<CS02) | (1<<CS00); // Prescaler 1024
TCNT0 = 0; // Start count from 0
DDRB = 0xFF;
while(1)
{
if(TCNT0 == 255)
{
PORTB ^= (1<<PB0); // Toggle LED
TCNT0 = 0; // Reset counter
}
}
}
In this example, the LED toggles every time the timer overflows, giving a visible time-based output.
7. Applications of Timers in Embedded Systems
- Creating precise software delays without wasting CPU cycles
- PWM generation for motor speed or LED brightness control
- Frequency and pulse width measurement of external signals
- Event counting such as RPM measurement
- Real-time clock implementation using external crystal with Timer2
- Interrupt-based scheduling and multitasking in embedded systems
Conclusion
Timers are one of the most powerful and versatile peripherals in AVR microcontrollers. They allow precise control over time-related operations and help in building efficient, event-driven embedded systems. Understanding how timers work internally, how to configure prescalers, and how to use overflow or compare modes is essential before moving to advanced features like PWM and interrupt-based control. In the upcoming posts, we will discuss Timer Modes, Timer Interrupts, and PWM generation using C programming on AVR in greater depth.
Comments
Post a Comment
Subscribe to Post Comments [Atom]