Module 3: Sequential Circuit Design · 3.5 Counters

Counters at Work

Digital counters perform timing, event measurement, and memory addressing across computer systems.

This article examines five practical applications of digital counters: frequency division, Pulse-Width Modulation (PWM), stopwatch timing, Direct Memory Access (DMA) addressing, and UART serial baud generation.

Frequency Division and Tick Generation

Consider a system where a peripheral requires a 1 MHz event rate from a 100 MHz master system clock.

A mod-100 counter counts from 0 to 99:

$$f_{\text{tick}} = \frac{f_{\text{clk}}}{N} \qquad \text{TICK} = (count == N - 1)$$

Do not route the TICK signal directly to the clock pins of peripheral flip-flops.

Instead, connect the TICK pulse to the clock enable ($EN$) inputs of peripheral registers while clocking all flip-flops with the master 100 MHz clock.

This architecture maintains a single synchronous clock domain, eliminating clock skew and domain-crossing hazards.

Pulse-Width Modulation (PWM)

Digital outputs generate only discrete voltage levels (logic 0 or logic 1). Pulse-Width Modulation delivers variable average power to motors and lighting elements by switching signals at high frequencies.

A PWM generator combines a free-running binary counter with a threshold comparator (Figure 1):

$$\text{PWM_OUT} = (count < \text{THRESHOLD})$$

PWM from a counter and a comparator
Figure 1: Pulse-Width Modulation waveform generated by comparing counter value against a threshold register

With an 8-bit counter running modulo-256, the duty cycle is:

$$\text{Duty Cycle} = \frac{\text{THRESHOLD}}{256} \times 100%$$

Software writes to the THRESHOLD register to adjust motor velocity or LED brightness.

Digital Stopwatch Architecture

A digital stopwatch displays minutes, seconds, and hundredths of a second using cascaded synchronous counters (Figure 2).

Stopwatch counter chain
Figure 2: Digital stopwatch architecture using cascaded BCD counter stages

  1. A 10 MHz reference oscillator divides by 100,000 to generate a 100 Hz enable tick.
  2. The 100 Hz tick enables a mod-100 hundredths-of-a-second counter.
  3. The terminal count of the hundredths stage enables the seconds units counter (mod-10).
  4. Cascaded terminal counts enable seconds tens (mod-6), minutes units (mod-10), and minutes tens (mod-6).

All flip-flops connect to the common 10 MHz master clock. A control flip-flop gates the 100 Hz enable tick to provide start and stop functionality. A parallel capture register provides a lap timing freeze function.

DMA Memory Address Generation

A Direct Memory Access (DMA) controller transfers data blocks between peripheral devices and memory without continuous processor intervention.

The controller contains a synchronous binary address counter:

  1. The processor writes the base memory address into the parallel-load inputs.
  2. On each completed word transfer, the address counter increments by 1 (or by a configured burst stride).
  3. A secondary transfer counter decrements until it reaches zero, asserting a transfer-complete interrupt.

Display graphics controllers use similar counters to stream pixel data sequentially from frame buffers to display panels.

UART Baud Rate Generation and Oversampling

UART serial communication lines transmit data without an explicit clock signal. The receiver must generate internal timing to sample incoming serial bits.

The receiver operates a baud rate counter at 16 times the nominal bit rate ($16\times$ oversampling):

$$N = \frac{f_{\text{clk}}}{16 \times \text{BaudRate}}$$

For 115,200 baud from a 100 MHz clock:

$$N = \frac{100 \times 10^6}{16 \times 115{,}200} \approx 54.25 \implies \text{Round to } 54$$

The actual baud rate is 115,741 baud (a timing error of $+0.47%$, well within the standard $\pm 2%$ UART tolerance).

The receiver tracks bit positions using a 4-bit tick counter (Figure 3):

  1. Detect the falling edge of the start bit.
  2. Count 8 ticks to sample the signal at the midpoint of the start bit.
  3. If the sample equals 0, validate the start bit and sample every subsequent 16 ticks to read data bits at their temporal centers.

UART receive sampling
Figure 3: UART receive sampling using 16x oversampling clock ticks

Practice Exercises

Problem 1

Design an 8-bit PWM generator operating at 50 kHz from a 100 MHz master clock. Calculate the required clock divider prescaler and find the threshold value for a 30 percent duty cycle.

Solution

An 8-bit counter requires $256$ clock ticks per PWM period:

$$f_{\text{counter}} = 50\text{ kHz} \times 256 = 12.8\text{ MHz}$$

Calculate the prescaler divisor:

$$\text{Divisor} = \frac{100\text{ MHz}}{12.8\text{ MHz}} = 7.8125 \approx 8$$

With prescaler divisor 8, the actual PWM frequency is:

$$f_{\text{actual}} = \frac{100\text{ MHz}}{8 \times 256} \approx 48.83\text{ kHz}$$

For a 30 percent duty cycle:

$$\text{THRESHOLD} = 0.30 \times 256 \approx 77$$

Problem 2

A digital stopwatch uses a 10 MHz reference clock to generate a 10 Hz tick for tenths-of-a-second timing. Calculate the required division ratio and determine register flip-flop count.

Solution

Calculate the division ratio:

$$N = \frac{10\text{ MHz}}{10\text{ Hz}} = 1{,}000{,}000$$

A single binary counter requires:

$$n = \lceil \log_2(1{,}000{,}000) \rceil = 20\text{ flip-flops} \quad (2^{20} = 1{,}048{,}576)$$

The counter clears synchronously on count 999,999.

Problem 3

Calculate the baud rate error when generating a 9600 baud rate with $16\times$ oversampling from a 50 MHz system clock.

Solution

Calculate ideal divisor:

$$N_{\text{ideal}} = \frac{50 \times 10^6}{16 \times 9600} \approx 325.52$$

Round to integer divisor $N = 326$:

$$\text{Baud}_{\text{actual}} = \frac{50 \times 10^6}{16 \times 326} \approx 9585.89\text{ baud}$$

$$\text{Error} = \frac{9585.89 - 9600}{9600} \times 100% \approx -0.15%$$

The error is far below the maximum allowable $\pm 2%$ UART margin.

Section 3.6 examines large-scale semiconductor memory arrays: register files, SRAM, DRAM, and ROM architectures.

← Up/Down, Gray, Johnson and Ring Counters Register Files →