How to use an ADC peripheral to convert several channels. ADC conversions are performed successively in a scan sequence. This example is based on the STM32C0xx ADC LL API. The peripheral initialization is done using LL unitary service functions for optimization purposes (performance and size).
Example configuration:
ADC is configured in single conversion mode, from SW trigger. Sequencer of ADC group regular (default group available on ADC of all STM32 devices) is configured to convert 3 channels: 1 channel from GPIO, 2 internal channels: internal voltage reference VrefInt and temperature sensor. DMA is configured to transfer conversion data in an array of size three elements (one array address for conversion data of each channel) in RAM memory, in circular mode.
Example execution:
Every second, ADC performs conversion of a channel among the 3 channels of the scan sequence, successively at each conversion start (discontinuous mode enabled).
Note: In case of discontinuous mode is disabled, then the entire scan sequence is converted in burst from one conversion start.
ADC conversion data of each sequence rank is transferred by DMA into the array “uhADCxConvertedData”:
When sequence is completed, it restarts from the beginning: first channel in the sequence, data transfer in first array address (rollback).
ADC conversions raw data are computed to physical values using LL ADC driver helper macro:
Note: For this example purpose, analog reference voltage (Vref+) is computed from ADC conversion of internal voltage reference VrefInt and used to compute other conversion data. This voltage should correspond to value of literal “VDDA_APPLI”. This procedure can be performed when value of voltage Vref+ is unknown in the application. (This is not the case in this example due to target board supplied by a LDO regulator providing a known constant voltage of value “VDDA_APPLI”). In typical case of Vref+ connected to Vdd, it allows to deduce Vdd value.
LED1 is used to monitor program execution status:
Normal operation: Activity of ADC scan sequence can be observed with LED toggle:
Error: LED remaining turned on
Debug: variables to monitor with debugger:
Connection needed:
Use an external power supply to generate a voltage in range [0V; 3.3V] and connect it to analog input pin (cf pin below). Note: If no power supply available, this pin can be let floating (in this case ADC conversion data will be undetermined).
Other peripherals used:
1 GPIO for LED1
1 GPIO for analog input: ADC channel 4 on pin PA.04 (Arduino connector CN8 pin 3 A2, Morpho connector CN7 pin 32)
1 DMA channel
ADC, analog digital converter, analog, conversion, voltage, channel, analog input, DMA transfer, sequence, temperature sensor, internal voltage reference, VrefInt, discontinuous
- ADC/ADC_MultiChannelSingleConversion_Init/Inc/stm32c0xx_it.h Interrupt handlers header file
- ADC/ADC_MultiChannelSingleConversion_Init/Inc/main.h Header for main.c module
- ADC/ADC_MultiChannelSingleConversion_Init/Inc/stm32_assert.h Template file to include assert_failed function
- ADC/ADC_MultiChannelSingleConversion_Init/Src/stm32c0xx_it.c Interrupt handlers
- ADC/ADC_MultiChannelSingleConversion_Init/Src/main.c Main program
- ADC/ADC_MultiChannelSingleConversion_Init/Src/system_stm32c0xx.c STM32C0xx system source file
This example runs on STM32C071RBTx devices.
This example has been tested with NUCLEO-C071RB board and can be easily tailored to any other supported device and development board.
In order to make the program work, you must do the following :