STM32 ADC Time-Distributed Sampling Design Method

Introduction

One of the simplest ways to use ADC on STM32 is to read multiple samples at a fixed moment and calculate their average.

However, in real embedded systems, multiple state machines often operate simultaneously. If ADC sampling is concentrated at a specific point in time, CPU load increases and may affect the real-time performance of the entire system.

In this article, I would like to introduce a Time-Distributed Sampling structure that spreads ADC sampling over time to reduce system load while obtaining stable measurement values.

Main Content

Problems with the Conventional Method

A commonly used approach is as follows:

for(int i = 0; i < 10; i++)
{
sum += ADC_Read();
}
avg = sum / 10;

This method is simple to implement, but it has several limitations.

  • ADC sampling is concentrated at one specific moment.
  • It creates a blocking structure that occupies CPU time continuously.
  • It may affect the execution of other state machines.
  • System responsiveness can be reduced.

In other words, although the structure is simple, it can become a burden in real equipment.

Time-Distributed Sampling

To solve these problems, ADC sampling can be distributed over time instead of being concentrated.

The core concept is simple:

  • Do not read ADC multiple times at once.
  • Sample periodically (e.g., every 10ms).
  • Calculate the average after a certain period (e.g., every 100ms).

This structure distributes CPU load while maintaining stable average values.

Structure Explanation

This structure consists of three major layers.

Sampling Layer(10ms)

  • Execute one ADC sampling every 10ms.
  • Operates in a non-blocking manner.
  • Does not place a heavy load on the system.

sum += ADC_Read();
count++;

Accumulation Layer

Samples are continuously accumulated during a specific time period.

An important point here is:

count is not a fixed value, but the actual number of accumulated samples.

Depending on scheduler timing, the number of samples may vary. Therefore, averaging should be based on the actual accumulated sample count.

 

Processing Layer(100ms)

Every 100ms, calculate the average and convert it into voltage.

avg = sum / count;
voltage = f(avg);

This is not just a simple average.

It is important to understand that this is a time-based average.

Core Design Concept

The Time-Distributed Sampling structure goes beyond simply “reading ADC values.”

It represents a design approach that harmonizes with the overall operation of the system.

The key idea is to clearly separate sampling and processing.

ADC reading is performed continuously in short cycles, while result interpretation and processing are executed in longer cycles.

This separation improves both system responsiveness and stability.

Sampling is repeated at short intervals such as 10ms, and only minimal work is performed at each sampling point.

This greatly reduces CPU load compared to reading multiple samples at once.

On the other hand, average calculation and data conversion are performed every 100ms based on the accumulated data.

In this structure, the average is based on the actual sample count rather than a fixed sample number.

As a result, even if system timing changes slightly, stable operation can still be maintained.

This approach effectively distributes CPU workload over time instead of concentrating it at one moment.

At the same time, averaging helps reduce noise and provides more stable measurement values.

This structure is especially useful when expanding to DMA-based ADC structures in the future.

It provides a natural transition path.

This design method is particularly advantageous in systems where multiple state machines operate simultaneously.

For example, in environments where motor control, communication processing, and sensor reading occur at the same time, CPU blocking by one task can affect the entire system.

In such cases, distributing sampling into short periodic tasks helps maintain system flow.

It also works very well for slowly changing signals such as temperature.

By averaging over time, stable measurements can be obtained even in noisy environments.

In summary, this structure can be described as:

“Measure quickly, process slowly.”

Sampling runs continuously without interrupting system flow, and processing is executed only when necessary.

This separation is a very important design pattern in embedded systems and can significantly improve overall system quality.

Conclusion

Instead of concentrating ADC sampling at one point in time, distributing it over time helps maintain system real-time performance while obtaining stable measurement values.

Separating Sampling and Processing is an important embedded system design pattern and can naturally extend to DMA-based structures in the future.

댓글 남기기