ADC conversion Time/Frequency Calculation
I have already covered How to use ADC in STM32. None of those tutorials actually covered the precise way to measure the conversion time or frequency for the ADC. Today in this tutorial, we will cover How to measure ADC conversion Time or conversion Frequency.
This tutorial will be divided into two halves. I will demonstrate how to utilize the datasheet to do these calculations. The first half of the tutorial will cover the STM32F103C8 controller, and the second half will cover STM32F446RE.
But this is not specific to these two microcontrollers. If you read everything carefully, you can implement the same logic for whatever series of microcontroller you have.
STM32F103C8 controller
Some of the important ADC features of STM32 F1 series, from the calculation point of view, are listed below
- 12 BIT Resolution. This is fixed, and can’t be changed.
- ADC clock is derived from the APB2 Clock. And using the prescalar, we can further control the ADC clock
- MAX ADC Clock can be 14 MHz
Below is the picture from the reference manual of F103 controller.
It shows that the ADC Conversion Time = Sampling Time + 12.5 Cycles. Here Sampling Time is something that you can choose during the setup in the CubeMX. Below is the modified formula to do this calculation
The formula mentioned above is the same, but little better representation of what’s there in the reference manual. See the example below
I want the conversion time to be of 17 microseconds. It’s not a random time, this is the requirement for the internal Temp sensor to work.
We need to select the values of Sampling Time and ADC Clock in such a way, so as to get this 17 microseconds of Conversion Time.
There can be many possibilities for doing this, and one of them is if we select the Sampling Time of 71.5 CYCLES along with the ADC CLOCK of 5 MHz
(71.5 + 12.5)/5MHz = 17 us
In the similar way, the Conversion frequency can be calculated by reversing the formula mentioned above
With the same setup of Sampling time (71.5) and same ADC Clock (5 MHz), the Conversion Frequency would be around 59 KHz.
STM32F446RE Controller
Some of the ADC Key points from the Reference manual of F446RE are as follows:
- 12-bit, 10-bit, 8-bit or 6-bit configurable resolution
- ADC Sampling Time changes based on the resolution Selected
- ADC Clock is derived from the APB2 clock, and can be configured using the prescalar
F446RE reference manual also have a formula to calculate the Conversion time and that is as shown below
I have modified this a bit in order for you to understand it, and the modified formula to calculate the conversion time is given below
Here, Cycles depends on the resolution you select for the ADC
- For 12 bit Resolution CYCLES = 12
- For 10 bit Resolution CYCLES = 10
- For 6 bit Resolution CYCLES = 6
The rest of the variables, like Sampling Time, and ADC CLOCK are configurable during the setup.
For eg:- Let’s say I want the ADC conversion time of 10 microseconds, for the Temperature Sensor. Here is the setup I an do for the same
- Keep the APB2 CLOCK at 50 MHz
- I am using 12 bit Resolution for this purpose, so the Cycles = 12
- Use the prescalar as 4. This is bring the ADC CLOCK to 12.5 MHz
- Use the Sampling Time of 112 CYCLES.
- Now conversion Time = (112 + 12) / 12.5 MHz = 9.9 us
The above example is just one of the combinations of many, that you can do for the same conversion time.
The same setup would also result in Conversion Frequency of 1/10 us = 100 KHz
You can apply the same Logic to any other STM32 controller you have. Just remember to look for the formula in the datasheet or the reference manual.
9 Comments. Leave new
Hi .
i followed your tutorial and uploaded the code on a BluePill board and get the Temp and ADC_VAL readings as follows :
Temp = 7.18795443
ADC_VAL = 0x0000074E
i tried enabling and disabling the continues mode the result remained the same
repeated the steps multiple times and issue still remains .
but when i changed the Vref from 3.3 to 1.4 and 1.6 , as per following sources readings are still incorrect .
Temp = 208.518799
ADC_VAL = 0x0000074F
in your code :
#define Vsense 3.3/4096
Vsense * adcValue
per rm0008 manual – page 222 :
Note: ADC1 analog inputs Channel16 and Channel17 are internally connected to the temperature sensor and to VREFINT, respectively.
the only time i can get a very good reading if Vref =3.2
#define VSENSE 3.2/4096
Temp = 17.6235466
ADC_VAL = 0x00000750
my confusion is regarding the Vref . what is the correct value for stm32f103c8-bluepill ?
because as far as i know STM32f103 MCUs’ Vref = 3.3v but the sources i mentioned above say otherwise . i have a custom made DevBoard around stm32f103ret6 which i know for has 3V3 Vref.
Hi, i am looking to sample a signal at around 300kHz(3.33us). Which stm32 series can satisfy? Please, I appreciate your reply
f4 series should be fine
I am confused with sampling rate and conversion time.What is the difference between them in data acquisition system? How to setup sampling frequency 300ksps?
HI , I have one doubt how to u given “Sampling Time of 112 CYCLES” for this any calculation is there can u please explain.
you can rewrite that formula making the sampling time as the subject.
Hi, I have a little question about this issue. What if oversampling (e.g. with 15 osr ratio) is used, what will the Tconv? According the last example, will Tconv be 112*osr_ratio + 12, or (112+12) * osr_ratio.
hi thank admin
I have a bit of a problem, hope you to answer. I use the STM32Cube IDE but when programming, the temperature is not displayed, and when using Keil C it can display the temperature. The code is the same between the two IDEs.
Keep Using the Keil