0:09
hello and welcome to controllers Tech
0:12
this is the third video in the ADC
0:16
32 in the previous videos we have
0:19
already covered how to configure the ADC
0:21
with the single Channel and how to use
0:23
the polling mode interrupt mode and dma
0:27
to read the data from the potentiometer
0:30
we also saw how to deal with the cache
0:32
coherency while using the dma in a
0:37
MCU today we will start covering the
0:39
multiple channels of an
0:41
ADC we can use polling mode and
0:44
interrupt mode to read multiple channels
0:47
also but the ADC configuration varies
0:51
devices while I was testing it across
0:53
different MCU series I needed to make a
0:56
lot of adjustments to make it
0:58
work therefore I decided to cover
1:01
multiple channels using only the dma
1:04
mode here the dma mode does not mean
1:07
that the conversion will continue
1:08
forever and the interrupt will trigger
1:10
at a continuous rate we can also use the
1:14
dma mode to only fetch the data when we
1:16
need it just like the polling
1:19
mode so this video is split into two
1:23
halves first we will see the use of dma
1:26
in the normal mode to read the multiple
1:31
and then we will see the dma in the
1:33
continuous mode where it will fetch the
1:36
continuously let's start the ID to
1:40
project here I have already created a
1:43
new project ADC part 3 the clock is
1:47
configured to run at maximum 480
1:51
MHz let's go to the adc1 and select
1:55
channel 3 in the single-ended mode you
1:59
can see the P pa6 got selected as the
2:02
adc1 channel 3 pin I want to configure
2:06
the nearby pins for the other
2:08
channels pin pa7 is adc1 Channel 7 here
2:14
P input p7 stands for positive input pin
2:17
and that is what we need for the
2:19
single-ended mode pc4 is adc1 Channel
2:24
4 this one is in yellow as we still need
2:28
to enable the single Ed mode in the
2:31
configuration PC 5 is adc1 Channel 8
2:37
Pin let's resolve the clock before we go
2:41
ahead here the ADC peripheral is clocked
2:44
by the PLL 2p which is clocked around 50
2:48
mahz here I am configuring it to run at
2:58
clock the ADC peripheral is also at 15
3:01
MHz so I am using a prescaler of 10 to
3:07
MHz this is not a requirement I just
3:10
want to select low speed for the ADC as
3:13
there is no clock requirement for the
3:15
potentiometers connected to the
3:17
MCU the ADC resolution is set to 16
3:21
bits we need to enable the scan mode for
3:24
multiple channels but the cube does not
3:27
allow us to do so let's scroll down to
3:31
regular conversion mode here the number
3:34
of conversions is still set to one since
3:37
we have four channels let's set it to
3:40
four the scan conversion mode is now
3:43
enabled and the cube will not allow us
3:45
to disable it the continuous conversion
3:48
mode is used when we want the ADC to
3:50
automatically start the conversion again
3:52
for all four channels but as I mentioned
3:56
we want to convert the data when we want
3:59
so let's keep it disabled for now the
4:01
end of conversion flag should be set
4:03
once all the four conversions are over
4:06
so set it to the end of the sequence the
4:09
conversion data management mode should
4:11
be now configured to use the dma but we
4:13
haven't enabled the dma yet so we will
4:16
come back here later now we need to
4:19
configure the ranks for the
4:21
channels I am setting rank one for
4:24
channel 3 so it will be converted first
4:27
rank two is for Channel 4 so it will be
4:31
second rank three is for channel 7 so it
4:36
third and finally rank four is for
4:39
Channel 8 so it will be converted at
4:42
last you can set the ranks according to
4:45
the sequence in which you want the
4:49
converted I am configuring the random
4:51
sampling time as there is no particular
4:56
potentiometers now let's go to the dma
4:58
section and add the dma for the
5:01
adc1 keep the dma in normal mode as we
5:06
once also set the data width according
5:11
resolution now if we come back to the
5:14
conversion data management we have the
5:16
options for the dma mode as well we want
5:19
the dma to run only once so we will
5:22
select oneshot mode here all right that
5:25
is all the configuration we need so
5:28
click save to generate the
5:30
project in the meantime let's see the
5:34
connections I have connected four
5:36
potentiometers to the four pins of the
5:39
adc1 the ADC pins are connected to the
5:45
potentiometers all the potentiometers
5:47
are powered with 3.3 volts from the MCU
5:50
and the ground pins are connected to the
5:54
MCU now before we start programming it
5:57
let's see the configurations for Other M
5:59
cus as well here I have the
6:02
configuration for the f44
6:06
6re I have selected four channels of the
6:10
adc1 F 446 does allow us to enable the
6:13
scan conversion mode so we will enable
6:16
it the continuous conversion mode should
6:19
be disabled and the end of conversion
6:21
should be set at the end of all
6:24
conversions now we will set the number
6:26
of conversions to four and then
6:29
configure the ranks for all the channels
6:32
just like we did in the
6:35
h750 now go to the dma section and add
6:40
adc1 make sure that the mode is set to
6:43
normal mode and configure the data width
6:45
as pure resolution also make sure that
6:49
the dma continuous request is disabled
6:52
otherwise the dma will keep transmitting
6:55
ADC that is all we need to configure in
6:58
f44 4 6 now let's see the configuration
7:02
for the f103 C8 let's go to adc1 and
7:07
enable the four channels it also does
7:11
not let us enable the scan conversion
7:13
mode scroll down to the number of
7:16
conversions and set it to four now the
7:19
scan conversion is enabled you can
7:22
configure the ranks for the channels
7:26
MCU make sure that the continuous
7:28
conversion mode is disabled now open the
7:32
dma section and add the dma for the
7:35
adc1 the dma should be in the normal
7:38
mode and make sure that the data width
7:41
is half word the ADC in f103 C8 has a
7:45
resolution of 12 bits by default so data
7:48
width needs to be half word that is 16
7:51
bits all right let's write the code now
7:55
let's define a 16bits array of four
7:57
elements we will store the data for the
8:00
four channels here now inside the main
8:03
function start the ADC in the dma mode
8:07
pass the array that we just created and
8:10
we want to receive the data for four
8:12
channels remember that we have
8:14
configured the end of conversion flag to
8:16
set when all the channels are converted
8:19
so the interrupt will trigger when all
8:21
four channels are converted and the
8:23
conversion complete call back will be
8:25
called we can process the ADC data
8:28
inside this call back or we can also set
8:31
another variable to indicate that the
8:33
data has been received then inside the
8:36
main function we will check if this
8:38
variable is set and then process the
8:40
data here let's also increment the count
8:43
variable every 500 MCS to ensure that
8:47
the rest of the while loop is working
8:49
fine here I am calling the ADC dma just
8:53
once let's test this part
8:57
first the warning is for this array as
9:00
the function expects a 32-bit pointer
9:03
here but we will leave it as it is let's
9:06
build and debug the project now let's
9:10
set a break point inside the call back
9:13
function here you can see the breake
9:15
point got the hit that means the dma has
9:18
finished the data transfer of all the
9:21
channels you can even see the data for
9:23
all the channels in the live
9:26
expression now if we run the code again
9:29
this breakpoint will not hit that is
9:32
because the dma is in the normal mode
9:34
and hence it only runs once the count
9:37
variable is incrementing every 500
9:40
milliseconds so the while loop is
9:42
working fine now we can start the ADC in
9:45
dma mode whenever we need to read the
9:49
potentiometers the dma will fetch the
9:52
data from all the channels and once done
9:56
automatically we will test it by calling
9:59
the ma inside this Loop which runs every
10:03
milliseconds all right let's debug the
10:06
project again the break point is still
10:08
set inside the Callback function we hit
10:12
the breakpoint once now if we run the
10:15
debugger again we get another hit the
10:19
break point is getting the hit every 500
10:21
milliseconds that means the dma is
10:23
transferring data every 500
10:26
milliseconds this is because we are
10:28
repeatedly starting the ADC in dma mode
10:32
let's run the debugger without the
10:34
breako and see if the data from all the
10:36
potentiometers is being read I am
10:39
rotating the third potentiometer now and
10:42
you can see the third element of the
10:45
increasing I am fast forwarding the
10:48
video the value has reached the maximum
10:53
65,535 as the potentiometer is rotated
10:56
to the extreme end now I am rotating it
10:59
in the opposite direction and the ADC
11:02
value is now decreasing I will leave it
11:06
30,000 now I am rotating the second
11:09
potentiometer and you can see the ADC
11:12
increasing similarly if I rotate it in
11:15
the other direction the value starts
11:18
decreasing I will leave this one at
11:23
20,000 let's rotate the fourth one now
11:29
the a value is increasing and it has
11:32
reached the maximum now when we rotate
11:35
it in the opposite direction the value
11:37
starts decreasing I will leave this one
11:42
40,000 finally we will rotate the first
11:46
potentiometer the ADC value is changing
11:49
fine just as the potentiometer is
11:52
rotating let's leave this to around
11:55
10,000 all right all the channels of the
11:58
ADC are working fine so we can use the
12:01
dma mode to read the data when we want
12:04
it does not need to fire the interrupt
12:06
at a regular interval you can write a
12:09
condition and call the dma function
12:11
inside it so the conversion will only
12:13
take place when the condition is met or
12:16
you can even use it with the
12:18
RTO where a separate task can be
12:21
assigned to start the ADC in the dma
12:23
mode this is it for the video in the
12:27
next video we will continue the D CDMA
12:30
but we will use the circular mode to
12:34
continuously we will continue the next
12:36
video from this point itself where I am
12:39
leaving it today that is it for now you
12:43
can download the project from the link
12:45
in the description leave comments in
12:48
case of any doubt keep watching and have