0:09
hello and welcome to controllers Tech
0:12
this is the sixth video in the ADC
0:16
32 we have already covered how to read
0:19
single Channel and multiple channels of
0:21
the ADC using all modes that includes
0:24
polling mode interrupt mode and the dma
0:27
mode today in this video we will
0:30
understand the ADC sampling and
0:32
conversion time basically we will first
0:35
calculate the conversion time for the
0:37
ADC channels and then measure it on the
0:40
scope I will demonstrate the calculation
0:43
and measurement as well also we will
0:47
test the timing in both F 446 re and
0:53
MCU let's start the cube ID and create a
0:58
project first we will test the F 446
1:02
re give some name to the project and
1:07
finish let's start with the clock
1:10
setup here we will bypass the high-speed
1:14
Source the board has 8 MHz Crystal and
1:18
we will run the system at maximum 180
1:22
MHz now let's go to adc1 and enable
1:27
Z you can see the PIN pa0 is selected as
1:31
the adc1 Channel Zero pin let's
1:34
configure the ADC clock
1:37
first as you can see in the clock
1:39
configuration we do not have a separate
1:43
ADC to understand where the adc1 is
1:46
getting the clock from let's open the
1:48
data sheet of the f44
1:51
6re here you can find the clock
1:53
information in this block
1:56
diagram as you can see here the adc1 2
2:00
and three all are connected to the apb2
2:03
bus as per our clock tree the apb2
2:07
peripheral clock is at 90 MHz and this
2:10
is going to be the ADC clock
2:12
itself my scope will not be able to
2:15
measure such a high frequency so let me
2:17
reduce the clock here we can use the
2:20
apb2 prescaler to reduce the clock it is
2:25
still high let's reduce the system clock
2:28
itself or right now our apb2 clock is at
2:32
4 mahz and so is the adc1 clock we can
2:37
use the ADC prescaler to further reduce
2:41
clock I am going to use the prescaler of
2:45
six the ADC resolution is set to 12
2:49
bits this resolution also plays an
2:52
important role in determining the
2:54
conversion time we will cover that in a
2:56
while let's enable the continuous
2:59
convers conversion mode we will also use
3:02
the dma as we want the conversions to be
3:05
as fast as possible let's enable the dma
3:09
in circular mode and also make sure that
3:11
the data width is set to half word now
3:15
enable The dma Continuous request make
3:17
sure dma keeps on transferring the data
3:21
ADC end of conversion flag is irrelevant
3:25
as we are using a single Channel now
3:28
let's configure the channel itself self
3:31
here I am setting the maximum sampling
3:36
Cycles here we have the formula to
3:39
calculate the sampling and conversion
3:42
time the sampling time only depends on
3:45
the sampling Cycles we set in the
3:48
configuration but this time is
3:50
irrelevant as it only includes the time
3:53
ADC takes to sample the channel the
3:56
conversion time is more relevant to us
3:59
as this is the time when ADC delivers us
4:01
the value itself as you can see here the
4:05
conversion time depends on the sampling
4:07
Cycles we set the conversion time and
4:10
the ADC clock here the conversion
4:13
parameter depends on the resolution we
4:16
set to find the conversion time taken as
4:19
per the resolution you can check the
4:21
reference Manual of the
4:22
MCU here I have the manual of the f44
4:27
6re inside the ADC SE section look in
4:30
the fast conversion mode here you can
4:33
see data for the minimum conversion time
4:36
available for different
4:38
resolutions the value three is the
4:40
minimum sampling cycle we can set so for
4:44
12-bit resolution the conversion
4:46
parameter is 12 for 10bit resolution the
4:49
parameter is 10 and similarly for 6-bit
4:52
resolution the parameter is six the cube
4:56
MX shows 15 clock cycles for 12bit
4:59
resolu solution and that is because it
5:01
is showing the minimum conversion time
5:03
we can get as we have three clock Cycles
5:07
as the minimum sampling time we can set
5:09
here the conversion parameter may vary
5:12
with different mcus as well let's check
5:16
the manual of the f103
5:19
C8 the ADC resolution in f103 C8 is
5:23
fixed to 12 bits and it can't be
5:26
configured to something
5:27
else as you can see here the conversion
5:31
parameter for this 12-bit resolution is
5:35
Cycles whereas in f44 6 re the
5:39
conversion parameter for 12-bit
5:42
Cycles the parameter varies in different
5:45
mcus so make sure you check the
5:48
reference Manual of your
5:50
controller all right let's do the
5:52
calculation now I have set the sampling
5:57
480 the conversion parameter for 12bit
6:01
12 now the ADC clock the apb2 clock is
6:06
at 4 mahz and we are using the prescaler
6:10
six so the ADC clock is apb2 clock
6:15
6 this makes the conversion time equal
6:20
seconds we will test it in the scope
6:23
later let's set pin pa1 as output as we
6:27
will use this pin to measure the
6:29
conversion time go to the gpio
6:33
configuration and set the output speed
6:36
of this pin to very high all right that
6:39
is all we need to configure click save
6:43
project let's Define a 16bit variable to
6:46
save the ADC conversion data now in the
6:50
main function start the ADC in the dma
6:54
mode we will store the value in the
6:56
variable we just defined and there is
7:00
conversion when the dma finishes the
7:03
transfer an interrupt will trigger and
7:05
the conversion complete call back will
7:08
called inside this call back we will
7:13
pa1 basically this pin is being used to
7:16
measure how frequently the conversion is
7:18
complete and the call back is
7:20
called that is all let's build and debug
7:24
the project now I am using the logic
7:28
analyzer to measure the PIN
7:30
timing let's run the debugger and also
7:33
capture the data on the
7:35
analyzer here you can see the pin
7:37
remains high and low for 738 micros
7:41
seconds this is exactly the same time
7:44
that we calculated for the conversion to
7:46
complete so the conversion completes
7:49
every 738 micros seconds the interrupt
7:52
is triggered and the pin toggles our
7:55
calculation for the conversion time was
8:00
now onwards I am going to modify this
8:02
project in the cube MX just for the
8:05
demonstration I am going to change the
8:07
sampling Cycles to 28
8:11
Cycles let's modify the value in the
8:14
Formula 2 we got the conversion time of
8:24
now let's build the project and Flash it
8:32
you can see the pin is now toggling
8:35
every 60 micros seconds this means that
8:38
the calculation is correct and we are
8:41
able to calculate the ADC conversion
8:43
time let's try it for multiple channels
8:46
now here I am enabling three more
8:51
configuration let's set the number of
8:53
conversions to four the scan conversion
8:57
mode is now enabled end of conversion
9:00
flag is set to the end of all channels
9:03
let's configure the sampling time for
9:05
all the channels I am going to choose a
9:08
different sampling time for each Channel
9:12
this will result in different conversion
9:14
times for each Channel we will add the
9:16
conversion time for all the channels and
9:19
compare it with the result we get the
9:22
rest of the configuration is the same
9:24
let's generate the project again we will
9:27
now calculate the conversion times for
9:30
all the channels and add them together
9:33
the only change is in the sampling
9:35
Cycles the rest of the parameters in
9:37
this calculation remain the same the
9:40
total conversion time is
9:44
milliseconds let's see if we get the
9:47
same with our code since there are four
9:49
channels now let's define in an array of
9:53
elements also inside the main function
9:56
change the data length to four that
9:59
that's all we need to modify let's build
10:01
and Flash the project to the board let's
10:05
see the timing on the analyzer now you
10:08
can see the time is exactly the same as
10:10
we calculated that is
10:14
milliseconds so the calculation works
10:17
fine with multiple channels too the
10:19
similar calculation works for all the
10:22
stdm 32 MCU but I found some discrepancy
10:25
while testing the H7 series MCU we we
10:29
will see it so let's create a new
10:33
h750 I am using a750 VB based custom
10:38
board just like I used in the previous
10:41
videos give some name to the project and
10:44
click finish I am going to start with
10:47
the clock setup first I am selecting the
10:51
external Crystal to provide the clock
10:54
the board has 25 mahz Crystal and we
10:57
will run the system at maximum 480
11:01
MHz let's enable channel 3 of adc1 in
11:07
mode Let's go back to the clock
11:10
configuration we need to modify the ADC
11:13
clock source to bring the ADC clock in
11:19
range all right the ADC clock now is 2
11:23
mahz and this should be fine for our
11:26
measurement let's go back to the ADC
11:29
configuration I am setting the prescaler
11:32
to two so this will bring the ADC clock
11:36
mahz the resolution is set to 16 bits
11:40
let's enable The Continuous conversion
11:42
mode we also need to enable the dma so
11:46
let's add it in the dma section make
11:49
sure the dma is in the circular mode and
11:52
also the data width is set to half word
11:56
Now set the end of conversion to the end
11:58
of the sequence and also set the
12:00
conversion data management to the
12:02
circular dma mode let's set the maximum
12:05
sampling time for channel 3 that is
12:29
High let's do the calculation for the
12:32
conversion time now we need to find the
12:35
details about the conversion parameter
12:38
so let's check the reference Manual of
12:48
MCU here go to the ADC section and you
12:51
can find the details in the programmable
12:54
resolution section as mentioned here for
12:57
the 16bit resolution the conversion time
13:00
is 8.5 ADC clock Cycles let's put the
13:04
values in our formula we have selected
13:17
810.02 this will result in the total
13:21
conversion time of 89 micros seconds
13:25
let's go back to the E and see if we get
13:29
the code will remain the same we will
13:32
first Define a 16bit variable to store
13:34
the ADC data inside the main function
13:38
start the ADC in the dma mode once the
13:42
conversion is finished an interrupt will
13:44
trigger and the conversion complete call
13:46
back will be called inside the call back
13:50
we will toggle the pin
13:52
pd8 that is all we need to program let's
13:55
build and Flash the project to the board
14:00
you can see the toel time is 1. 638
14:05
milliseconds this is not what we got in
14:07
the result we should have got the time
14:09
around 819 micros but this is not the
14:13
same as being shown in the scope now if
14:16
we divide the ADC clock by two we will
14:21
milliseconds I tested this across
14:24
different sampling times and resolutions
14:26
but this division parameter remained
14:30
after doing a lot of digging in the
14:31
reference manual I found out the reason
14:34
for the same to understand this division
14:37
Factor go to the ADC common register
14:40
section here go to the common control
14:44
register the bits 16 and 17 of the
14:47
common control register represents the
14:49
clock mode Under the Clock mode there
14:53
are different sections for the devices
14:55
with revision Y and with revision v as
14:58
per for this statement the ADC clock
15:01
will have an additional divide factor of
15:03
two for the devices with revision V I
15:06
have the MCU with revision V and
15:09
therefore this divide factor is applied
15:11
to the ADC clock now what exactly is
15:14
this revision V or revision y here I
15:18
have a picture showing the h750 VB and
15:22
here you can see the letter V at the end
15:24
representing the revision V here is
15:27
another picture of the same MCU but with
15:30
revision y so whether the Divide Factor
15:33
will be applied or not depends on the
15:36
MCU revision you are using as much as I
15:39
have tested it the same is valid for
15:42
other H7 based MCU as well I have not
15:46
tested the devices with revision y but
15:49
as for the reference manual they should
15:51
not have this divide Factor if you guys
15:54
have the H7 based MCU with revision y
15:57
please let me know the result in the
15:59
comments below other than this divide
16:02
Factor the rest of the things are just
16:04
as we have seen in the F
16:06
446 we got the ADC conversion time just
16:10
as we calculated using the formula this
16:13
is it for the video I hope you
16:16
understood how to calculate the total
16:18
conversion time for the ADC you can
16:21
download the project from the link in
16:23
the description leave comments in case
16:26
of any doubt keep watching and have a