STM32 ADC settings?

An area to discuss ARM specific problems and examples

Moderator: Benj

Post Reply
User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

STM32 ADC settings?

Post by fotios »

Hi everyone
I try to get the peak voltage of a pulse with fixed F=1KHz. The peak voltage and the duty cycle can be changed but during the ADC conversion are stable.
The more precise way I found so far, is to get the voltage as an averaged integer ("GetAverageInt" in FC).
Then I use the formula Vpeak = Vavg / F * T (where T = the time duration of the positive edge of pulse).
The resulted Vpeak looks good as I get a few millivolts deviation, say 10mV.
E.g. on the actual hardware LCD, the indicated value is continuously changed from 3.10 to 3.19 Volt Peak.
I tried a lot of ADC configurations to get this result but I'm not sure which is more suitable for the STM32-ARM.
It looks precise only with ADC clock = PCLK / 4 = 24MHz/4 = 6MHz (ADC clock cycle = 0.166usec).
The acquisition time looks meaningless, the result is the same, e.g. from any setting within 1 to 100usec.
What I'm not sure that understand, are the parameters in the ADC component properties window.
For the "GetAverageInt" selection the parameters are two: "NumSamples" and "DelayUs". Both should be inserted in byte format, i.e. 0-255d.
I have noticed that if both have the same value and about 229d to 233dd the ADC offers the more precise and stable readings.

What exactly is expressed through the two bytes? The actual number of Samples and the actual time between samples?

The ADC of STM32 is 12-bit and its total conversion time is Tconv = Sampling Time + 12.5 ADC clock cycles.
In my case is 0.166us X 12.5 = 2.08us + sampling time.

Could I get a more precise measurement of the Voltage? This +/-10 millivolts deviation looks some big in my eyes. My DSO, only occasionally flips by just 2mV in averaging mode, while in the peak value mode looks very stable.

Thanks
Last edited by fotios on Thu Nov 21, 2019 8:28 pm, edited 1 time in total.
Best Regards FOTIS ANAGNOSTOU

User avatar
fotios
Posts: 458
Joined: Mon Feb 08, 2010 10:17 am
Location: Greece
Has thanked: 109 times
Been thanked: 117 times
Contact:

Re: STM32 ADC settings?

Post by fotios »

Hi again
To be more precise,
If I select NumSamples = 200 and DelayUs = 200 then I will have 6 samples of the square pulse, per each full period (1msec=1,000usec) as is presented in the following picture?
And that the 200 samples will be completed in: 5 samples / 1000us => 200 samples in 40 X 1000 = 40,000usec?
And that the sampling rate is:
5 samples / 1ms
200 samples / 40ms
5,000 samples / 1sec
I.e. the sampling rate is 5 KSPS?
sampling.jpg
sampling.jpg (32.73 KiB) Viewed 4676 times
It is also noticeable that the more stable reading of 1.56 to 1.58 Vavg (3.12 - 3.15 Vpeak) is obtained with:
NumSamples = 231
DelayUs = 231

Does that mean?
1,000us / 231us = 4.329 samples each 1,000us
231 samples / 4.329 = 53.361ms
4.329 * 1,000 = 4329 samples / sec

Thanks
Best Regards FOTIS ANAGNOSTOU

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: STM32 ADC settings?

Post by LeighM »

The “Number of samples” is the number samples that are taken in a loop to accumulate and return an average value.

The DelayUs parameter of the GetAverage functions is actually the sample time in ADC clock cycles.
It is the equivalent of the “Acquisition Time” for the single sample read functions,
which is also the sample time in ADC clock cycles. (The duration for which the ADC samples the input).

So I would not rely on this to give a specific delay time between samples, it does not have uS resolution as the name implies.
You would be better taking and averaging a value from a timer interrupt if you need the sample rate to be accurate.

For measuring peak, you could loop read a number of samples and record the peak?

Post Reply