July 2013 DocID022945 Rev 5 1/32
AN4073
Application note
How to improve ADC accuracy when using STM32F2xx and
STM32F4xx microcontrollers
Introduction
The purpose of this application note is to show how to improve the accuracy of A/D
conversions for applications using the STM32F2xx and STM32F4xx microcontrollers.
It also explains the firmware methodology, which can be applied to reduce the ADC error
and gives some general tips on writing firmware for better ADC accuracy.
Please note that the data provided with this application note is for reference only, measured
in a lab under typical conditions (unless specified otherwise) and not tested in production.
Table 1 lists the microcontrollers concerned by this application note.
Table 1. Applicable products
Type Part numbers
Microcontrollers
STM32F2xx (STM32F20x, STM32F21x)
STM32F4xx (STM32F405, STM32F407, STM32F415, STM32F417,
STM32F42x, STM32F43x)
www.st.com
Contents AN4073
2/32 DocID022945 Rev 5
Contents
1 Overview of parameters impacting the ADC accuracy . . . . . . . . . . . . . 6
2 Firmware techniques for improving the conversion accuracy . . . . . . . 7
2.1 Averaging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.1 Averaging of N ADC samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1.2 Averaging of N-X ADC samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.2 Additional recommendation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 STM32F2 and STM32F4 practical measurements . . . . . . . . . . . . . . . . 11
3.1 Measurement conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
3.1.1 Hardware setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.1.2 Firmware setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
3.2.1 ADC measurements when ART is ON . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2.2 ADC measurements when ART is OFF . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2.3 ADC measurements when (Data+Instruction) cache ON + prefetch OFF15
3.3 Timing considerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
3.4 Measurement conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4 STM32F4 ADC accuracy options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1 Configuration options for ADC accuracy . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.1 Option 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.1.2 Option 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2 Practical measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.2.1 Hardware Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.2.2 Common firmware setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.2.3 Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.3 Measurement conclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Appendix A Averaging of N ADC samples: source code . . . . . . . . . . . . . . . . . . 26
Appendix B Averaging of N-X ADC samples: source code . . . . . . . . . . . . . . . . 27
Appendix C Firmware sequence to activate Option 1 and Option 2 . . . . . . . . . 29