Texas AN
Texas AN
Texas AN
Application note
Improving STM32F1x and STM32L1x
ADC resolution by oversampling
Introduction
The STMicroelectronics STM32F1x and STM32L1x Cortex™-M3 based microcontrollers
come with 12-bit enhanced ADC sampling with a rate up to Msamples/s. In most
applications, this resolution is sufficient, but in some cases where higher accuracy is
required, the concept of oversampling and decimating the input signal can be implemented
to save the use of an external ADC solution and to reduce the application consumption.
This application note gives two methods to improve ADC resolution. These techniques are
based on the same principle: oversampling the input signal with the maximum 1 MHz ADC
capability and decimating the input signal to enhance its resolution.
The method and the firmware (STSW-STM32014) given within this application note apply to
both Medium- and High-density STM32F1x products as well as all STM32L1x products.
Some specific hints are given at the end of the application note to take advantage of the
implementation of the DAC peripheral and the ADC dual mode into some STM32F1x and
STM32L1x devices.
This application note is split into two main parts: the first one describes how oversampling
increases the ADC-specified resolution while the second describes the guidelines to
implement the different methods available and gives the firmware flowchart of their
implementation on the STM32F1x and STM32L1x devices.
Contents
6 Hints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.1 What is the maximum number of bits that can be added to
the on-chip ADC resolution? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6.2 Taking advantage of STM32 DAC implementation . . . . . . . . . . . . . . . . . . 17
6.3 Taking advantage of the STM32F1x Dual ADC mode implementation . . 18
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
List of tables
Table 1. Oversampling using white noise vs. oversampling using triangular dither . . . . . . . . . . . . . 16
Table 2. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
List of figures
The ADC gives a representation of an analog signal among a finite number of digital words.
Since the digital domain is represented by a finite number of words which have to present a
continuous signal, the conversion step introduces the quantization error function of the ADC
input range and resolution.
For an ideal ADC, the quantization error is between ±0.5 LSB. In the case where the input
signal is varying through many levels between samples, and the sampling rate is not
synchronized with the input frequency, the quantization error can be considered as a white
noise whose energy is uniformly spread from the DC domain to half of the sampling
frequency. Please refer to Appendix A for more details regarding the calculation of its
density.
The SNR (signal-to-noise ratio) is the ratio of the ADC noise to the input signal power. For
an ideal ADC, it is assumed that the SNR is equal to the quantization noise (no other noise
source is considered) to the input signal. It is demonstrated that for a full-scale sinusoidal
signal, the ADC SNR is maximum and given by the following formula:
SNR dB = 6,02N + 1,76 , where N is the ADC resolution.
It is can be easily noticed that when the SNR increases, the ADC effective number of bits
increases.
For a real ADC, different error sources should be considered: offset, gain, INL (integral
nonlinear) and DNL (differential nonlinear). A brief description of these errors can be found
in the STM32F1x and STM32L1x datasheets. They degrade the ideal ADC resolution. In
this case, we speak of real effective number of bits.
Improving the SNR involves an enhancement of the ADC effective number of bits.
The following section demonstrates that sampling the input signal with higher rates than the
Nyquist frequency improves the SNR. The Nyquist frequency is introduced in the next
paragraph.
The Nyquist theorem states that in order to be able to reconstruct the analog input signal,
the signal should be sampled at a rate fS (sampling frequency) that is greater than twice the
maximum frequency component of the input signal.
Not respecting the Nyquist theorem causes aliasing effects and the analog signal cannot be
fully reconstructed from the input samples. Therefore, in most applications, a low-pass filter
is required at the ADC input to filter frequencies lower than half the sampling frequency. It is
difficult to handle the filter constraints with low sampling frequencies.
Oversampling consists in sampling the input analog signal at rates higher than the Nyquist
frequency limit, filtering the samples and reducing the sample rate by decimation. Using this
method relaxes the anti-aliasing low-pass filter constraints.
PSD PSD
Input signal
Same area
Quantization error
PSD = Power signal density
F F
-2.fm - fm fm fS = 2.fm -2.N.fm - fm fm fS= 2.N.fm
ai14937
According to Figure 1, when sampling the input signal at higher rates, the same noise
power, represented by the area of the green rectangle, is spread over a bandwidth equal to
the sampling frequency which is much greater than the signal bandwidth fm. Only a
relatively small fraction of the total noise power falls in the [–fm, fm] band, and the noise
power outside the signal band can be greatly attenuated with a digital low-pass filter.
Reducing the quantization noise enhances the signal-to-noise ratio and, consequently, the
ADC effective number of bits. Oversampling the input signal OSR times faster than the
Nyquist frequency gives the following SNR
SNR OVS = 6,02 ⋅ N + 1,76 + 10 log ( OSR )
It can be concluded that each doubling of the sampling frequency will lower the in-band
noise by 3 dB, and increase the measurement resolution by 1/2 bit. Therefore, 6dB SNR
gain is required to add 1 resolution bit to the ADC.
In general, if p additional bits are required by the application then, the ADC sampling
frequency should be at least:
p
F OVS = 4 F S , where FS is the current ADC sampling frequency used.
3.2 Decimation
The conventional meaning of averaging is adding m samples and dividing the result by m.
Averaging several data from an ADC measurement is equivalent to a low-pass filter which
attenuates the signal fluctuation and noise. The average method is often used to smooth
and remove speaks from the input signal.
Note that normal averaging does not increase the resolution of the conversion because the
sum of m N-bit samples divided per m is an N-bit representation of the sample.
ai14938
– In the case where external noise dither should be added to the input signal, then
the thermal noise generated by a diode or a resistor can be injected into the input
signal.
– The input noise should not correlate with the useful input signal and the input
signal should have equal probability to be between two adjacent ADC codes. This
means that for systems using feedback process, this method does not work.
ai14939
The ADC thermal noise can be computed from this histogram (though this can be shown, it
is not the objective of this application note and details are not offered here).
In order to carry on this ADC noise test, the user should do the following:
• uncomment the line #define Themal_Noise_Measure in the oversampling.h file
• configure the Total_Samples_Number which is the number of ADC conversion
operations. It should be smaller than 65535. The DMA channel is configured to store
the number of ADC samples in a RAM buffer. At the end of the transfer, an interrupt is
generated and the number of occurrences of each ADC code is computed
• In order to compute the occurrence of the ADC codes, a variable giving the relevant
ADC codes is defined
When the code is run, Relevant_ADC_Samples ADC samples and their corresponding
number of occurrences are displayed on the HyperTerminal. The HyperTerminal
configuration is 8-bit data, no parity, 115 200 baud rate. If the effective number of ADC
samples found is smaller than the defined Relevant_ADC_Samples variable, then 0 is
displayed for both ADC code and ADC code occurrence. The user can capture them and
build a histogram.
ADC period = 1 µs
<=1µs
Time t
ai14940
The oversampled data are computed in the DMA transfer complete interrupt. For
synchronization reasons, it is recommended to read it in the second TIM2 interrupt.
Note that with this implementation, the TIM2 period should be greater than the time required
by the ADC to convert OSR samples, and greater than the ADC interrupt execution time.
If the sampling frequency required by the application is exactly OSR µs, then the user is not
required to use Timer TIM2 to generate the input sampling frequency. However, the DMA
should be configured to be functional in continuous mode and the DMA transfer complete
interrupt should be updated accordingly. The oversampled data are usually computed in the
DMA transfer complete interrupt.
3000
2500
2000
1500
1000
500
0
30
60
90
120
150
180
210
240
270
300
330
360
390
ai14942
6000
5000
4000
3000
2000
1000
0
0
41
82
123
164
205
246
287
328
369
410
451
492
533
574
615
656
697
738
779 ai14942
The oversampling algorithm using white noise is run with the same ramp (50 Hz frequency
and 1 V amplitude). Both Figure 5 and Figure 6 give the ADC oversampled data as a
function of time in µs. Figure 5 is the result of adding one bit while Figure 6 is the result of
adding two additional bits to the ADC on-chip resolution.
When the ramp is sampled without using any extra software resolution, with a 3.3 V
reference supply, 1 V corresponds to the digital value 1250.
When one additional bit is added, 1 V is sampled as 2500 and when two additional bits are
added, 1 V is sampled as 5000.
This means that the environment contains enough noise for this method to work.
Assuming that the input signal is between two successive quantization steps q0 and q1
during the oversampling period, then the converter may convert it either to q0 or q1. Adding
extra p bits of resolution means determining the relative position of the input signal between
q0 and q1.
With the addition of an appropriate triangular signal, the quantizer generates a series of q1s
and q0s. Averaging the q1 occurrences over a given interval determines the relative position
of the input signal between the lower and the higher quantization steps.
The theory states that the best results are achieved when dithering the input signal using a
triangular waveform with a period of OSR times the ADC sampling period and an amplitude
of n+0.5LSB where n = 0,1,2,3.
The theory behind this methods is quite complicated, so that Figure 7 serves as an example
to illustrate how this method works. In this example, the ADC on-chip resolution is 3 and
three extra bits are added by firmware. The input signal is assumed to have an amplitude of
q0+ 0.6LSB (q0 = 6 in this example). In order to add three additional bits, the input signal is
sampled 2.23 times (16 times).
111 000 q1
Input signal @ q0+0.6LSB
110 111
110 110 Input signal + triangular
waveform samples q1
110 101
110 100 (q1+q0)/2
110 011
Input signal + triangular
110 010 (q1+q0)/4
waveform samples q0
110 001 (q1+q0)/8
110 000 q0 Average of q1 occurrences= 9/16 = 0.563
-> The nearest value is 110 101
Result =(7x110 000+ 9x111 000 + 1) >>1=110 101
ai14943
If the input signal is not correlated with the triangular waveform, then it is demonstrated that
the gain in the SNR is equal to
Therefore, each doubling of the sampling frequency improves the SNR by 6dB and adds 1
ADC bit resolution.
In general, in order to add p-bit extra resolution, the oversampling frequency should be
equal to
p
F OVS = 2.2 F S
1 kΩ
ai14944
The triangular dither is generated using Timer TIM3 configured in PWM mode by updating
the Capture Compare Register CCR1. Timer TIM3 period should be equal to the ADC
conversion rate and CCR1 should be updated OSR times where OSR is the oversampling
factor. In order to do this, the possible CCR1 values are first computed and stored into a
RAM buffer, then DMA transfer is used to update the CCR1 register, removing the need for
interrupts.
Note that the ADC conversion rate limits the oversampling factor. For example, in the case
where the ADC is running at 1 MHz, the STM32F1x is operating at 56 MHz. In order to have
a period of 1 µs, the auto-reload register of timer TIM3 should be equal to 55. The maximum
number of additional bits is then 4.
When a TIM2 update interrupt is triggered, the ADC and TIM3 DMA are re-enabled and the
converted ADC values can be treated to compute the new sample with the extra resolution
bits. Figure 9 summarizes the implemented functionality.
Input signal
Time t
ai14945
For this method to work, the input signal should not vary by more than ±0.5LSB during the
oversampling period. This means that for an STM32F1x or STM32L1x operating from a
3.3 V VREF+, the maximum allowed variations of the input signal during the oversampling
period is ~0.4 mV.
On the other side, a triangular waveform with an amplitude of 0.5LSB means a 0.4 mV
amplitude when operating the STM32F1x or STM32L1x from a 3.3 V VREF+. The application
environment must therefore not be very noisy. Any disturbance of the triangular waveform
will have an impact on the computed oversampled data.
According to the implementation, the triangular waveform is generated by means of the
STM32 timer and an RC filter that cuts the 1 MHz timer frequency. The timer PWM output
signal is integrated to provide a triangular signal with a 3.3 V amplitude. The division is done
with the ratio R3/R2.
The firmware related to this method is located in the TriangularDitherMethod
directory.
The first method based on oversampling and averaging using white noise provides a half-bit
additional resolution for each doubling of the oversampling rate. The maximum input
frequency is drastically decreased with the additional number of additional bits.
For applications where this gain is sufficient, then it is a good choice. It requires the
presence of white noise in the input signal to make the signal toggle between two adjacent
ADC codes. In general, the ADC thermal noise is sufficient and there is no need to add
external hardware to act as an external white noise source. This makes the solution more
cost-effective.
The second method based on dithering the input signal using a triangular waveform and
computing its relative position between two quantized steps provides one more bit for each
doubling of the oversampling rate. This is twice the improvement given by the first method.
To make this method work, the input signal should not correlate with the triangular signal
and should not have a variation greater than 0.5LSB during the oversampling period.
However, external hardware is needed to add the input signal and the triangular waveform.
Table 1 summarizes the main differences between the two methods. It is not possible to say
that one method is better than the other. Each method has its advantages and limitations.
The user should select the one that better meets their application requirements (sampling
frequency, number of effective bits etc.).
Table 1. Oversampling using white noise vs. oversampling using triangular dither
Oversampling using Oversampling using triangular
Implementation conditions
white noise dither
6 Hints
Let us assume that we have an N-bit analog-to-digital converter (ADC) and a voltage
reference VAREF.
Let quantum q be the minimum distance between two adjacent ADC codes. It is defined as
follows:
V AREF
q = ----------------
N
-
2
According to the above formula, the quantization noise power depends on the ADC
resolution and decreases drastically when the ADC resolution increases.
Given an ADC sampling frequency fS (which is specified according to the MCU), in the case
where the Shannon criteria is respected, then the quantization noise power density is equal
to
2
σ
PSD = ------
fs
Let fm be the maximum frequency component of the input signal. The quantization noise
power present in the band of interest is given by
fm 2
σ 2 2f m 2 2f m
∫–fm fs- df = σ ⋅ --------
2
η0 = ----- - = q ⋅ -------------
fs 12.f s
PSD PSD
Input signal
F F
-2.fm - fm fm fS = 2.fm -2.N.fm - fm fm fS= 2.N.fm
ai14946
Note that increasing the sampling frequency reduces the in-band quantization noise power
and consequently improves the signal-to-noise ratio.
Given the same input signal and sampling it with 2.fm and fS = OSR.2.fm, the gain in SNR is
⎛ 2 ⎞
⎜ σx ⎟
SNR OVS = 10 log ⎜ --------------------⎟ = SNR 2.fm + 10 log ( OSR )
⎜ 2 2f m⎟
⎝ σ ⋅ -------- fs ⎠
-
Revision history
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve the
right to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at any
time, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes no
liability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this
document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products
or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such
third party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIED
WARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIED
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWS
OF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
ST PRODUCTS ARE NOT DESIGNED OR AUTHORIZED FOR USE IN: (A) SAFETY CRITICAL APPLICATIONS SUCH AS LIFE
SUPPORTING, ACTIVE IMPLANTED DEVICES OR SYSTEMS WITH PRODUCT FUNCTIONAL SAFETY REQUIREMENTS; (B)
AERONAUTIC APPLICATIONS; (C) AUTOMOTIVE APPLICATIONS OR ENVIRONMENTS, AND/OR (D) AEROSPACE APPLICATIONS
OR ENVIRONMENTS. WHERE ST PRODUCTS ARE NOT DESIGNED FOR SUCH USE, THE PURCHASER SHALL USE PRODUCTS AT
PURCHASER’S SOLE RISK, EVEN IF ST HAS BEEN INFORMED IN WRITING OF SUCH USAGE, UNLESS A PRODUCT IS
EXPRESSLY DESIGNATED BY ST AS BEING INTENDED FOR “AUTOMOTIVE, AUTOMOTIVE SAFETY OR MEDICAL” INDUSTRY
DOMAINS ACCORDING TO ST PRODUCT DESIGN SPECIFICATIONS. PRODUCTS FORMALLY ESCC, QML OR JAN QUALIFIED ARE
DEEMED SUITABLE FOR USE IN AEROSPACE BY THE CORRESPONDING GOVERNMENTAL AGENCY.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately void
any warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, any
liability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.