Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 260 of 9363. Microcontroller Fundamentals and Bare-Metal CFree lesson

Implementing Continuous Sampling with ADC Hardware Triggers

Configure the ADC to automatically perform repeated conversions triggered by internal timer events to maintain a steady data stream.

Implementing Continuous Sampling with ADC Hardware Triggers

In this lesson, you will learn how to automate your Analog-to-Digital Converter (ADC) so that it samples data at precise, recurring intervals without needing the CPU to start every single conversion.

The Problem with Software-Triggered ADC

Up until now, you have been triggering ADC conversions by manually setting a "Start Conversion" bit in a control register. If you want to sample a sensor at a steady rate of 1kHz, your code must sit in a loop, wait for a timer to expire, set the bit, wait for the conversion to complete, and repeat. This ties up your CPU and risks "jitter"—small variations in timing caused by other tasks running in the background.

Offloading to Hardware

To achieve a perfectly steady data stream, we shift the responsibility from the CPU to the hardware peripherals. Most microcontrollers allow you to connect a Timer to the ADC Trigger Input. Instead of the CPU setting the start bit, the Timer’s "Update Event" (or Overflow) acts as a hardware pulse that tells the ADC, "Begin a conversion now."

This creates a deterministic pipeline:

  1. Timer is configured to overflow at your desired sampling frequency.
  2. ADC is configured to wait for a "Hardware Trigger" (the Timer event) instead of a software bit.
  3. Once triggered, the ADC completes its work and resets, waiting patiently for the next timer pulse.

Configuring the Trigger

You generally configure this by selecting the trigger source in an ADC control register. Here is a conceptual example of how you might link them:

// 1. Configure Timer to overflow at desired frequency
TIM2->ARR = 1000;  // Auto-reload register for frequency timing

// 2. Configure ADC to use Timer 2 as the trigger source
ADC1->CR2 |= (0x04 << ADC_TRIGGER_SHIFT); // Select specific hardware trigger

// 3. Enable the hardware trigger mode
ADC1->CR2 |= ADC_EXTEN_RISING; 

By decoupling the timing from the software loop, your CPU is free to process previous samples or handle communication tasks while the hardware maintains a precise, consistent rhythm for data acquisition.

Key Takeaway: Hardware triggers offload the burden of timing from the CPU, ensuring your ADC performs conversions at a jitter-free, constant frequency determined by the hardware clock.