Knowledge / Electronics and Communication Engineering / Microcontroller peripherals: timers, ADC, PWM and interrupts

Microcontroller peripherals: timers, ADC, PWM and interrupts

Electronics and Communication Engineering (B.E./B.Tech) · Microprocessors and embedded · free study notes for exams, placements and interviews

In short

A microcontroller is its CPU plus on-chip peripherals; timers, analog-to-digital converters (ADC), pulse-width modulators (PWM) and the interrupt controller are the four that appear in almost every embedded design. Timers measure and generate time, the ADC turns sensor voltages into numbers, PWM controls motor speed, LED brightness and SMPS duty, and interrupts let the CPU respond to events without polling. These are standard units in AICTE/CBCS "Microprocessors and Microcontrollers" and "Embedded Systems" courses, carry marks in GATE EC/IN (timer count and ADC resolution numericals) and in ISRO/BARC/DRDO and PSU papers. In interviews at Bosch, Continental, L&T Technology Services, Tata Elxsi, KPIT, Wipro, HCL, Qualcomm and Texas Instruments India, duty-cycle arithmetic, ADC step size and "what must an ISR never do" are near-certain questions.

Explained

Timers and counters: the core block

Analog-to-digital converter (ADC)

PWM: generation and use

Interrupts

Putting them together — a typical measurement loop

  1. Configure a timer for an exact period (prescaler + auto-reload).
  2. Let the timer trigger the ADC so samples are equally spaced (software triggering adds jitter).
  3. ADC end-of-conversion raises an interrupt or triggers DMA into a circular buffer.
  4. The control law (PID, filter, averaging) runs in the main loop or an RTOS task.
  5. The output is written to a PWM compare register, updated at a preload/shadow register boundary so the duty never changes mid-pulse (glitch-free update).
  6. Typical Indian lab boards for this: 8051/AT89S52 kits, Arduino Uno (ATmega328P), STM32 "Blue Pill" (STM32F103), ESP32, TI LaunchPad (MSP430/C2000) and NXP/Infineon automotive parts in industry.

Key points

Formulas and examples

Timer tick period: t_tick = N_pre / f_clk (N_pre = prescaler, f_clk = timer clock in Hz, t_tick in s)
Overflow / period time: T_ov = (TOP + 1 − initial count) × t_tick (TOP = 2n − 1 for a free-running n-bit timer)
Required count for a delay: count = T_desired × f_clk / N_pre ; reload value (up-counter) = 2n − count
Auto-reload (ARR) for a period: ARR = f_clk /(N_pre × f_desired) − 1
ADC step size: ΔV = V_ref / 2n (volts per LSB); digital output D = round(V_in × 2n / V_ref)
Resolved voltage: V_in ≈ D × V_ref / 2n ; quantisation error = ±ΔV/2
Ideal SNR (dB) = 6.02 n + 1.76 ; Nyquist: f_s > 2 f_max
ADC conversion time: t_conv = (clocks per conversion) / f_ADC
Duty cycle: D = t_on/T (×100 for %) ; V_avg = D × V_supply ; f_PWM = 1/T
PWM compare value (edge-aligned, up-counting): CCR = D × (ARR + 1)
PWM steps available: N_steps = f_timer / f_PWM ; PWM resolution in bits = log2(N_steps)
Frequency measured by input capture: f_in = f_timer / (N_pre × Δcount), Δcount = capture2 − capture1

Example 1 — 8051 timer delay (16-bit, Mode 1). Crystal 11.0592 MHz; the 8051 machine cycle is 12 oscillator periods, so the timer clock is 11.0592/12 = 0.9216 MHz and one tick = 1/0.9216 MHz ≈ 1.085 µs. For a 50 ms delay: count = 50 ms / 1.085 µs ≈ 46,080 ticks. Reload value = 65,536 − 46,080 = 19,456 = 4C00H, so TH0 = 4CH, TL0 = 00H. Maximum single delay with this crystal = 65,536 × 1.085 µs ≈ 71.1 ms, so a 1-second delay needs 20 such interrupts counted in software.

Example 2 — STM32/AVR style auto-reload for an exact 1 kHz tick. Timer clock 72 MHz, prescaler N_pre = 72 (PSC register = 71) → counter clock = 1 MHz. ARR = 1,000,000/1,000 − 1 = 999. Period = (999 + 1)/1 MHz = 1.000 ms exactly, with no software reload and therefore no jitter.

Example 3 — ADC resolution and code. A 10-bit SAR ADC with V_ref = 5.00 V.

Example 4 — PWM for DC motor speed. ATmega328P style: f_timer = 16 MHz, 8-bit fast PWM with prescaler 64.

Example 5 — Input capture frequency measurement. Timer clock 72 MHz, prescaler 72 → 1 MHz capture clock (1 µs per tick). Two successive rising edges are captured at 1,200 and 21,200. Δcount = 20,000 ticks = 20,000 µs = 20 ms → f_in = 50 Hz (a mains zero-cross signal). If the third capture reads 41,150, the second period is 19,950 µs → 50.125 Hz, so the measurement resolution here is about 1 µs in 20 ms ≈ 0.005%.

Peripheral feature8051 (AT89S52)AVR (ATmega328P)ARM Cortex-M (STM32F103)
Timers2-3 × 16-bit, 4 modes2 × 8-bit, 1 × 16-bitSeveral 16-bit, advanced + SysTick
Clock divisionFixed ÷12 (÷6 in some variants)Prescaler ÷1 to ÷1024PSC 16-bit (÷1 to ÷65,536)
On-chip ADCNone in the classic 805110-bit, 8 ch, SAR12-bit, up to 3 units, SAR, DMA
PWMSoftware / timer toggling6 hardware PWM channelsMany channels + dead-time + break
Interrupts5-6 sources, 2 priority levels~26 vectors, fixed priorityNVIC, up to 240 lines, nested priorities
DMANoNoYes (ADC, timer, UART)
ADC typeSpeedResolutionTypical use
FlashVery high (one clock)6-8 bitRF, oscilloscopes
SARMedium-high8-16 bitOn-chip MCU ADC, sensors
Dual slopeLow12-20 bitMultimeters, panel meters
Sigma-deltaLow-medium16-24 bitWeigh scales, energy meters, audio

Where it is used

Interview questions

Common mistakes

Related topics

8051 microcontroller · ARM and embedded C · Embedded interfaces: UART, SPI and I2C · Sensors and actuators for embedded systems · Real-time operating systems · Internet of Things basics

Open in the Knowledge reader →Find jobs →