STM32 ADC Conversion Time/Frequency Calculation || Internal Temp Sensor

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everyone welcome to controllers tech I have already covered the ADC in stm32 in my previous videos where we did single and multiple channels there still are some doubts though one of them is calculating the conversion frequency or conversion time today in this video we will talk about that also we will see how to use the internal temperature sensor which requires the particular conversion time in order to work to show the difference is in the ADC I will be using two different microcontrollers in this video the first half will cover the f103 cortex m3 controller and the second half will cover the f4 series controllers other series would follow the same rules let's take a look at the reference manual of f103 first it have 12 bit ADC which is fixed and we can't change the resolution now to calculate the conversion time there are these formulas given in the manual based on these I have written another formula to simplify this calculation basically conversion time is equal to the sampling time plus 12.5 cycles divided by the ADC clock you will understand this as we progress further let's start by creating the project in cube IDE [Music] give some name here and click finish [Music] you first of all I am enabling the external crystal for the clock also select serial wire here now in the ADC select the internal temperature sensor we will come back here let's first set the clock the ADC clock is 12 megahertz right now let's go back to ADC setup we have no option to choose the resolution as mentioned in the datasheet we can only use 12 bit resolution for ADC make sure you enable the continuous conversion here we have the sampling time now as accordance to the formula 12.5 cycles will continue and if I select the sampling time of 71 point 5 cycles along with the ADC clock of 5 megahertz I can get the conversion time of 17 microseconds so why 17 microseconds let's see the temperature sensor details as the datasheet says we need the conversion time around 17 point one microseconds for the temperature sensor to work [Music] now we need to set these two values to their respective positions select 71.5 cycles and change the ADC clock to 5 megahertz [Music] let's also include the DMA change it to word and change the mode to circular now all set click Save to generate the project [Music] [Music] let's create a variable to store the ADC value in the main function start the ADC with DMA the parameters our ADC handler variable to store the data in and the number of channels we need to copy this conversion complete function in our main file when the ADC finishes the conversion we will convert the ADC value to the temperature the formula to do that is given in the respective reference manuals let's just copy this entire formula [Music] we need to replace these curly braces with the normal brackets now we need to define these values they can be found in the device datasheet just search for V sense and you can find all the values [Music] here we have average slope as 4.3 millivolts per Celsius let's define average slope [Music] I am defining it in votes per Celsius [Music] [Music] v25 value is 1.4 3 volts so we will use as it is now these senses basically the voltage per ADC value 4096 is for 12 bit resolution as to power 12 is 4096 and based on the ADC value we will get the voltage here everything is set now let's build and debug this code I have included both the variables in the live expression as you can see the temperature of the core is around 40 degrees Celsius this is fine because the ambient temperature here is around 36 degrees this temperature sensor could only work if the conversion time is selected properly this is it for the f103 rest of the video will cover the f4 series if you want to know the difference you should keep watching it this way you will better understand this entire process [Music] you let's take a look at the referee you love F 4 4 6 r e [Music] as you can see we have options to select the different resolutions for the ADC let's scroll down [Music] here you can see the ADC clock is derived from the APB - clock and we have prescaler to further control this clock [Music] now here we have the formula to do the calculation for the conversion time we will come back to this but first let's create a new project in cube IDE like I said I will be using f4 for 6r II give some name to your project and click finish [Music] first of all I will set up the clock I have eight megahertz external crystal now if you remember the ADC is connected to a PB two o'clock I will keep the a PB two o'clock at fifty megahertz the rest of the clocks will set up automatically you will soon realize the reason behind keeping this at fifty megahertz let's go back to our setup and select the temperature sensor channel in ADC we will deal with prescaler in a while these are the resolutions that we can select notice that there are different ADC clock cycles for these resolutions now this can be confusing like what exactly these clock cycles are to know this let's go back to data sheet by default the formula here says twelve cycles [Music] for 12-bit resolutions the minimum conversion time can be calculated with three ADC cycles and this totally gives up 15 ADC clock cycles so this thing here you see is actually the minimum conversion time minimum conversion time for the ten bit resolution can be 15 ADC clock cycles and same for the rest of these resolutions so our actual ADC cycles for the 12 bit resolution is 12 cycles and for 10 bit it is 10 cycles now before going any further let's take a look at the formula this is the modified formula to do calculate the conversion time or frequency basically conversion time is equal to the sampling time plus the ADC cycles divided by the ADC clock ADC cycles will be 12 as I am using the 12 bit resolution if you remember I use the APB 2 clock as 50 megahertz and if we use the prescaler as 4 this will give us the ADC clock of 12.5 megahertz and now a sampling time of 112 cycles would give us the conversion time of nine point nine microseconds you let's set the things in our setup now so why do I need this nine point nine microseconds in the first place to know this go back to reference manual and see the temperature sensor section looks like we don't have all the details here we need to open the data sheet for this controller search for V cents we will directly come to the properties of temperature sensor as you can see here the conversion time need to be around 10 microseconds for the temperature sensor to work and that's why the nine point nine is the closest I got so everything is set up now just click Save to generate the project I forgot to add the DMA let's add it now select the circular mode and the data width is 32 bit in the parameter setup enable the continuous conversion mode and the DMA continuous request [Music] you create variables to store the ADC value and the temperature value [Music] next we need to write a conversion complete callback function here we will convert the ADC value to the temperature using the formula provided in the manual [Music] [Music] all the constants used in the formula are defined in the datasheet so we will define them in our main file also the value for average slope is given in millivolts per Celsius it's two point 5 millivolts per Celsius I am writing this in volts per Celsius and that's why 0.0025 V 25 is 0.76 volts and V cents depends on resolution this here will convert the ADC value to the respective voltage let's just quickly build this once so we don't have any errors till now now start the ADC with DMA in the main function we only have one channel to convert so one as the parameter here let's build and debug this now [Music] [Music] as you can see the temperature is being displayed and it is very stable also don't compare it with the temperature from the f103 as I made these two parts at different times the temperature here is accurate as the ambient pressure right now is around 27 degrees this accuracy can only be achieved if you have the conversion time of 10 microseconds this verifies the fact that the conversion time calculation was accurate I hope you understood the concept and now you should be able to do the same for your respective microcontrollers it's all there in the reference manual and the datasheet this is all for this video you can download the code and the formula from the link in the description keep watching and have a nice day you
Info
Channel: Controllers Tech
Views: 9,585
Rating: undefined out of 5
Keywords: stm32, stm32f4, f103, discovery, nucleo, adc, conversion, time, frequency, calculation, example, tutorial, internal, temperature, sensor
Id: Jqq8rJNUwJg
Channel Id: undefined
Length: 19min 27sec (1167 seconds)
Published: Mon Jul 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.