STM32 ADC Multiple Channels Updated method
Today in this tutorial, I will show you how to read multiple channels in ADC in STM32 using new CubeMx. For this demonstration, I am using STM32F103C8 controller and True-Studio IDE. For the ADC purpose, I am using 3 channels as mentioned below:-
- CHANNEL 0 –> IR sensor
- CHANNEL 1 –> Potentiometer
- CHANNEL 16 –> Internal Temp sensor
So in order to read the multiple channels, we need to use DMA. The benefit of it is the conversion will take place in the background and we can perform some other operation with the controller and when we need the values, we can just read them easily.
Before proceeding further, Let’s see How we have to get the value from the internal temperature sensor of the STM32. According to the reference manual (Pg- 236), The temp is given as follows
V25, Avg_Slope are given in the datasheet (Pg-79)
|Also note that the ADC sampling time, while reading the temperature, needs to be 17.1 us.|
Above is the clock section from the CubeMx. Note that I have selected the ADC clock as 14 MHz. The reason behind this is that the Temp sensor sampling time needs to be 17.1 us. While in the ADC setting, we have maximum sampling time as 239.5 cycles. So (239.5/14) gives us 17.1 us sampling time.
Note that 3 channels are selected and the Continuous conversion mode is enabled. Also in the new CubeMx, you don’t need to worry about scan conversion mode. It will enable and disable by it’s own, based on if you are using multi channels or only one channel.
Above is the DMA setting for the ADC. Make sure that the DMA is circular and data width is selected as ‘Word‘. This is because the CubeMx uses ADC in 12 bits resolution by default and in order to store 12 bits we need the ‘Word‘ size.
Some Insight into the Code
Starts the ADC in DMA mode and the converted data is stored in ‘value‘ buffer.
‘3’ is the length of data to be transferred from ADC peripheral to memory.
The above code gets the temperature of the sensor in °C. You can change the values of V25 and Avg_Slope according to your controller’s datasheet.
. . . . . DOWNLOAD . . . . .
You can buy me acontrollerstech
coffeeSensor by clicking DONATE OR Just click DOWNLOAD to download the code
- WavePlayer using STM32 Discovery January 16, 2021
- How to use SPI in LPC2148 December 26, 2020
- STM32 ADC MULTI CHANNEL WITHOUT DMA December 24, 2020
- Send and Receive data to PC without UART (STM32 USB COM) December 10, 2020
- STM32 GPIO INPUT Configuration December 4, 2020
- STM32 GPIO OUTPUT Config using REGISTERS November 24, 2020