In embedded system design, accurate timing and event counting are essential functions of any microcontroller. The AVR family provides dedicated hardware modules known as Timer/Counters to perform these operations efficiently without overloading the CPU. The same hardware unit can function either as a timer or a counter depending on the clock source configuration. Understanding the difference between these two modes is extremely important, because it helps the programmer choose the right configuration for time measurement, event counting, or waveform generation. Many students get confused between timer and counter operations — both use similar registers, but the input source differs. In this post, we will explore the working principle, configuration, registers, and internal block diagram that differentiate Timer and Counter operations in AVR microcontrollers.
1. Concept Overview
Both the Timer and Counter are based on a register called TCNTx, which increments automatically as clock pulses arrive.
The key difference lies in the source of clock pulses:
- In Timer Mode, the pulses are derived from the internal system clock of the AVR.
- In Counter Mode, the pulses come from an external pin (T0, T1, etc.).
2. Internal Clock Source – Timer Operation
When configured in Timer Mode, the counter increments automatically using the system clock. The internal clock frequency can be divided by a prescaler to adjust the counting speed. The value in the counter register represents elapsed time.
// Example: Timer0 configured in Timer Mode
TCCR0 = (1<<CS02) | (1<<CS00); // Internal clock with 1024 prescaler
Here, the TCCR0 register is used to select the prescaler bits (CS02, CS01, CS00).
The timer increments automatically with every 1024th CPU clock pulse.
3. External Clock Source – Counter Operation
In Counter Mode, the counting is controlled by an external signal applied to the timer’s external clock pin (like T0 or T1).
Each pulse received on this pin increases the counter value by one.
This mode is extremely useful for counting real-world events such as pulses from sensors, encoders, or switches.
// Example: Timer0 configured in Counter Mode (external clock source)
TCCR0 = (1<<CS02) | (1<<CS01); // External clock on T0 pin (falling edge)
In this configuration, each falling edge on the T0 pin increments the TCNT0 register by one.
Thus, instead of measuring time, it counts the number of input events.
4. Key Difference Between Timer and Counter
| Parameter | Timer Mode | Counter Mode |
|---|---|---|
| Clock Source | Internal CPU Clock | External pulses through T0/T1 pin |
| Purpose | Measure time intervals, generate delays | Count external events or pulses |
| Register Used | TCNTx | TCNTx |
| Configuration Register | TCCRx (select prescaler) | TCCRx (select external source) |
| Clock Frequency | Depends on system clock and prescaler | Depends on external signal frequency |
| Typical Use Case | Delay generation, PWM, periodic interrupts | Pulse counting, frequency measurement |
5. Registers Involved in Timer/Counter Configuration
Both modes share common control and data registers. The important ones are:
- TCNTx: Timer/Counter register that holds the current count value.
- TCCRx: Timer/Counter Control Register used to select clock source, mode, and prescaler.
- OCRx: Output Compare Register used for compare match operations.
- TIMSK/TIFR: Timer Interrupt Mask and Flag Registers for overflow and compare match interrupts.
The above diagram shows how both internal and external clock sources are multiplexed through the TCCR register and then used to increment the TCNTx counter. Depending on the mode selected, the timer either measures time (internal clock) or counts events (external signal).
6. Practical Applications
- Timer Mode: Creating precise delays, real-time clocks, PWM waveforms, or periodic interrupts.
- Counter Mode: Counting external events such as sensor pulses, revolutions of a motor shaft, or number of items detected on a conveyor.
7. Example: Event Counter Using Timer1
#include <avr/io.h>
int main(void)
{
DDRB = 0xFF; // PORTB as output
TCCR1B = (1<<CS12) | (1<<CS11); // External clock on T1 pin (rising edge)
TCNT1 = 0; // Reset counter
while(1)
{
if(TCNT1 >= 1000)
{
PORTB ^= (1<<PB0); // Toggle LED after 1000 pulses
TCNT1 = 0;
}
}
}
Conclusion
The AVR’s Timer/Counter module is a flexible hardware feature that can act as a time measurer or event counter depending on the configuration. The difference lies in the clock source — the internal clock for timing tasks and the external pin for event counting. Understanding this distinction allows embedded engineers to design systems that can measure real-time intervals or track physical events with high accuracy. In the next section, we’ll explore different timer modes (Normal, CTC, PWM) and learn how they expand the capabilities of the AVR Timer subsystem.
Comments
Post a Comment
Subscribe to Post Comments [Atom]