0:09
hello and welcome to controllers Tech
0:12
this is the fifth video in the ADC
0:16
32 in the previous videos we saw how to
0:19
read the multiple channels of the ADC
0:22
using the dma we used the dma in both
0:26
normal mode and circular mode to read
0:28
the multiple channels of the
0:30
ADC today we will see how to do the same
0:34
but without using the dma although there
0:37
is no reason for us to not use the dma
0:40
while reading multiple channels but
0:42
since there are ways to do it we will
0:44
cover them we will first use the method
0:47
of discontinuous conversion mode to read
0:50
one channel at a time later we will
0:53
modify the pregenerated code itself so
0:56
that we can read any random Channel at
0:58
any point in our code
1:01
here I have already created a project
1:04
tutorial the four channels are enabled
1:07
just like we did in the previous
1:10
videos the ADC clock is configured to
1:15
mahz let's configure the parameters now
1:19
I am using a prescaler of 10 so to
1:21
reduce the ADC clock to 1.5
1:25
mahz now the first thing we will do is
1:28
set the number of conversions to four
1:31
this will automatically enable the scan
1:33
conversion mode which is a must for the
1:37
channels as I mentioned we will use the
1:40
discontinuous conversion mode
1:42
first this mode is actually used to
1:45
split a longer conversion Q to smaller
1:48
cu's for example if we have 10 channels
1:52
to be converted we can convert four
1:54
channels at once then another four
1:56
channels and then the last two at the
1:58
end but we can also use this feature to
2:01
convert one channel at a time therefore
2:04
eliminating the need for the use of
2:06
dma so enable the discontinuous
2:09
conversion mode and set the number of
2:12
conversions to one this will split the
2:15
que of four conversions to one channel
2:17
at a time also make sure that the end of
2:20
conversion selection is set to the end
2:24
conversion we want to read the data from
2:26
one channel at a time therefore it is
2:30
set the conversion data to store in the
2:34
itself now let's configure the ranks for
2:37
each Channel although we are using
2:40
discontinuous mode to read a single
2:42
Channel we will still not have the
2:44
option to choose the channel to be
2:46
converted the channels will be selected
2:49
as per the rank we configure here all
2:52
right I have configured the ranks and
2:54
sampling time for all the
2:56
channels we are not using the dma and
2:59
the interrupt for this
3:01
tutorial we will simply use the polling
3:04
mode to read the multiple
3:06
channels that is all we need to
3:09
configure click save to generate the
3:12
project let's take a look at the
3:14
configuration required for other
3:17
MCU here I have the configuration for
3:22
C8 I have already enabled four channels
3:26
converted now the first thing we will do
3:29
is set the number of conversions to four
3:32
this will automatically enable the scan
3:35
conversion mode which is a must for the
3:38
channels enable the discontinuous
3:41
conversion mode and set the number of
3:43
conversions to one now you can configure
3:46
the ranks for the channels according to
3:48
the sequence in which you want to
3:50
convert them let's take a look at the
3:52
configuration required for the f44
3:56
6re here again we will first set the
3:59
number of conversions to four now enable
4:02
the discontinuous conversion mode and
4:05
set the number of conversions to one set
4:08
the end of conversion selection to
4:10
single Channel now you can configure the
4:13
ranks for the channels according to the
4:14
sequence in which you want to convert
4:16
them there is not much change in the
4:19
configuration required in different
4:22
mcus all right let's write the program
4:25
now let's create a 16bits array of four
4:28
elements to to store the converted
4:30
channels data we will repeatedly convert
4:34
the channels inside the while loop start
4:37
the ADC first then pull for the
4:40
conversion to complete here I am setting
4:43
a time out of 100 m seconds now we will
4:47
read the converted Channel data and
4:50
store it in the zeroth element of the
4:52
array now remember that we should not
4:54
stop the ADC rather start the ADC again
4:58
Poll for the conversion to complete and
5:00
read the converted Channel data we need
5:03
to repeat the steps for the number of
5:07
converting the sequence in which we will
5:09
get the results is based on the ranks we
5:12
configured in the cube
5:13
MX once all the channels have been
5:18
ADC let's also give a delay of 250
5:22
milliseconds before starting the
5:24
conversion again note here that we
5:27
cannot choose which channel is to be
5:29
converted we need to read the data in
5:31
the same sequence as we configured the
5:33
ranks in this is how the four
5:36
potentiometers are connected to the
5:38
MCU all the potentiometers are connected
5:41
to the 3.3 Vols and the ground of the
5:44
MCU the middle pins of the
5:47
potentiometers are connected to the ADC
5:50
pins all right let's build and debug the
5:58
now I have added the ADC value array to
6:03
expression let's run the debugger now
6:07
you can see that we are getting the data
6:11
potentiometers they are changing every
6:15
milliseconds I am going to fast forward
6:18
the video now I am rotating the first
6:21
potentiometer and you can see the
6:23
variation in the zeroth element of the
6:26
array I am leaving this value at around
6:30
,000 now I am rotating the second
6:33
potentiometer and you can see the
6:35
variations in the first element of the
6:37
array I am going to leave the value at
6:41
40,000 similarly rotating the third and
6:44
fourth potentiometers varies the second
6:47
and third elements of the array
6:50
respectively so we were able to read
6:52
multiple channels using the polling mode
6:54
and without the use of the dma although
6:57
if you see the code here there is no
7:00
point of doing it this way since we have
7:03
to read all the channels anyway so why
7:06
not simply use the dma we can stop the
7:09
ADC after reading just two channels so
7:12
the rest two channels can be avoided but
7:15
if you want to get the data from the
7:16
third Channel you still need to read the
7:20
channels as I mentioned earlier the
7:23
discontinuous conversion mode has an
7:25
entirely different purpose we can use
7:27
this mode to read a single Channel as we
7:29
we did just now but practically this is
7:33
purpose I will make another video
7:35
showing the actual purpose of
7:37
discontinous conversion
7:39
mode now we will see another method to
7:41
read the multiple channels in the
7:44
mode Let's Open the cube MX to modify
7:47
the project again let's disable the
7:50
discontinuous conversion mode as we are
7:55
anymore the rest of the configuration
8:00
the scan mode is enabled as it is needed
8:04
channels make sure the continuous
8:08
disabled the end of conversion selection
8:11
should be set to the end of single
8:13
conversion this will make sure that the
8:16
conversion complete flag is set after a
8:20
converted let's generate the project
8:23
again I am going to refresh the project
8:26
so that the new configuration is visible
8:29
let's comment out this part as we do not
8:33
anymore we need to copy the data from
8:35
the ADC initialization
8:38
function this is where the channel
8:40
configuration starts from let's copy the
8:44
configuration for the rank one now we
8:47
will create a new function ADC convert
8:50
rank one paste the channel
8:53
initialization part here inside it this
8:56
code will basically configure the ADC ch
8:59
channel 3 with rank one the sampling
9:02
time is configured as per the cube
9:05
MX after the channel has been configured
9:08
we will start reading the channel to do
9:14
first then Poll for the conversion to
9:17
complete read the ADC value and stop the
9:22
ADC since channel 3 is configured with
9:25
rank one we will get the data for
9:28
itself also remember that we have
9:30
configured the end of conversion
9:32
selection to the end of a single
9:34
Channel therefore the conversion
9:36
complete flag will be set after each
9:39
channel is converted and hence we can
9:41
get the data for each Channel we will
9:44
create similar functions for the other
9:46
channels as well let's rename this to
9:49
rank two the rank two is configured for
9:52
Channel 4 rank three is for channel 7
9:56
and rank four is for Channel 8 we we
9:59
also have different sampling time for
10:02
channels comment at the pre-generated
10:05
code for the channel initialization So
10:08
to avoid the conflict also copy this
10:11
channel configuration structure
10:13
definition and paste it inside all the
10:21
functions inside this function we will
10:24
just change the channel to channel 4 but
10:26
keep the rank to one also config
10:29
configure the sampling time as per the
10:32
configuration the rest of the code for
10:35
reading the ADC data Remains the Same
10:38
now for reading the rank three we will
10:41
change the channel to channel 7 keep the
10:43
rank to rank one and change the sampling
10:47
configuration finally for the rank four
10:50
function set the channel to channel
10:53
8 the rest of the code Remains the
10:56
Same basically we have created different
10:59
functions to read a single Channel data
11:03
channels now in the main function we can
11:06
simply call the particular function to
11:09
channel we will test it by reading only
11:12
rank three that is Channel 7 when we
11:16
were using the discontinuous mode
11:18
reading rank three was not possible
11:20
until we had read rank one and
11:23
two but with this method we should be
11:25
able to read rank three without
11:27
involving other channels
11:29
you can see that we are getting the data
11:31
for rank three let me vary the
11:35
potentiometer 3 you can see the
11:38
variations in the ADC
11:40
value this means that the rank three is
11:44
properly I am leaving the value at
11:46
around 40,000 just remember this as it
11:49
will come handy when we read all the
11:51
potentiometers together now we will do
11:55
another test where we will only read the
11:59
here we are getting the value for the
12:02
4 now I am varying the fourth
12:05
potentiometer and you can see the
12:07
variation in the ADC value I am going to
12:11
leave this value at around 60,000 just
12:18
too all right now we will read all four
12:21
ranks at once let's build and debug the
12:30
you can see that we are getting the data
12:31
for all four channels now the rank three
12:35
is at 40,000 and rank four is at
12:39
60,000 these are the same values which
12:42
we set while testing the channels
12:44
earlier I am rotating the first
12:47
potentiometer now and you can see the
12:49
variations in the first element of the
12:51
array similarly when I rotate the second
12:55
potentiometer you can see the variations
12:57
in the second element of the array
13:00
so the code works fine and we are able
13:02
to read the individual channels at our
13:05
wish basically you can call the
13:07
individual function to read a particular
13:10
Channel data at any point in the
13:12
code this method is more practical than
13:15
reading using discontinuous
13:17
mode if you want to read all the
13:20
channels at once better go with
13:23
dma this is it for the video you can
13:27
download the project from the link in in
13:30
description leave comments in case of
13:32
any doubt keep watching and have a nice