3.6) AVR Timer Programming: Counter Programming

posted by Hamid Sayyed • November 12, 2025 0 Comments

In embedded systems, counters play a vital role in measuring external events, timing intervals, and monitoring system operations. A counter is a special hardware register that increments whenever a specific event occurs, such as an external pulse or an internal clock tick. In AVR microcontrollers, counters are closely related to timers, sharing the same hardware blocks but differing in their clock sources. Timers use the internal system clock to count time intervals, whereas counters use external events as input signals. This makes counters particularly useful in real-world applications like frequency measurement, event counting, motor speed detection, and encoder-based systems. Learning counter programming helps students understand how AVR microcontrollers interact with the environment through external triggers, and how such triggers can be processed efficiently using C programming. Let’s study in detail the working, control registers, and program examples for counters in AVR.

Understanding Counter Operation in AVR

In AVR microcontrollers like the ATmega16, each timer can also function as a counter. When configured in counter mode, the Timer/Counter register increments on the detection of an external edge at the corresponding input pin (T0, T1, or T2). For example, Timer0 uses the T0 pin (PB0) to receive pulses. Depending on the control register settings, the counter may increment on either the rising or falling edge of these signals. This mode is ideal for counting the number of events such as object detection pulses or encoder signals.

External Signal
(T0/T1 Pin)
Control Logic
(TCCR0 / TCCR1)
Counter Register
(TCNTx)
Compare / Overflow
Interrupt

The block diagram above illustrates how an external signal enters through the T0/T1 pin, passes through the control logic determined by the TCCR register, and updates the counter register (TCNTx). When the counter reaches its maximum value (for 8-bit counters, 255), it triggers an overflow that can be used to generate interrupts or reset the count automatically.

Counter Control Registers

Counter operation is controlled using specific registers. The primary registers are TCCR0, TCCR1A, TCCR1B, and TCCR2. These registers define how the counter works, the edge sensitivity (rising or falling), and the clock source selection. The TCNTx register holds the current count value, which automatically increments upon each event pulse. By carefully setting the bits in the TCCR register, you can switch between timer and counter modes easily. The Clock Select bits (CS02, CS01, CS00 for Timer0) determine whether the counter uses the internal or external clock source.

Example:
If CS02:CS00 = 101, counting occurs on the falling edge of the external T0 pin.
If CS02:CS00 = 111, counting occurs on the rising edge of the external T0 pin.

Programming Counter in C

The following example demonstrates how to use Timer0 as an external counter using pin T0 of the AVR microcontroller. The program counts pulses received on the pin and displays the count value through LEDs connected to PORTC.


#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>

int main(void)
{
    DDRB &= ~(1<<PB0);     // Set PB0 (T0 pin) as input
    DDRC = 0xFF;           // PORTC as output to display count

    TCCR0 = (1<<CS02) | (1<<CS00);  // External clock source on T0 pin (falling edge)
    TCNT0 = 0;                        // Initialize counter to zero

    while (1)
    {
        PORTC = TCNT0;   // Output current counter value to LEDs
    }
}
  

Applications of Counter Programming

Counter programming is widely used in automation, measurement, and control applications. It enables microcontrollers to count pulses from sensors, measure rotational speeds, or detect event frequencies. In robotics, counters are used for wheel rotation measurement using encoders. In industrial systems, counters help monitor machine cycles and production counts. Thus, understanding how to configure and program counters helps in implementing precise, event-driven control systems.

Conclusion

Counters in AVR are versatile modules that make microcontrollers capable of real-world event measurement and control. Through careful register configuration and external signal handling, students can design systems that accurately count, time, and react to hardware events. The knowledge of counter programming is a key foundation for developing advanced embedded applications such as frequency meters, digital event counters, and automatic control systems.

Comments

Post a Comment

Subscribe to Post Comments [Atom]