0:09
hello and welcome to controllers Tech
0:12
this is the seventh video in the SDM 32
0:15
ADC Series in the previous videos we
0:19
have covered how to read a single
0:20
Channel and multiple channels using
0:22
different ADC modes that includes
0:25
polling mode interrupt mode and dma mode
0:29
we also saw how to calculate the
0:31
conversion time for the ADC
0:34
channels in today's video we will cover
0:37
the external trigger source for the
0:39
ADC basically we will use one of the
0:42
timers to control the sampling rate for
0:45
channels I am going to use the same SDM
0:50
h750 custom development board which I
0:52
have used in previous videos as well
0:56
let's see the connections
0:58
once this is how the potentiometers are
1:01
connected to the dev board all the
1:04
potentiometers are powered with 3.3
1:08
itself the outputs from the
1:10
potentiometers are connected to the four
1:14
adc1 we will configure these channels in
1:18
MX I have created a new project for this
1:22
tutorial right now I have configured
1:24
only one channel of the adc1 here you
1:27
can see the pin pa6 is configured as the
1:30
ADC pin let's configure the parameters
1:34
now ADC clock is not an important
1:37
parameter for this tutorial so let's set
1:39
the prescaler to one so as to generate a
1:42
fast clock the ADC resolution is set to
1:47
bits it is very important to keep the
1:50
continuous conversion
1:52
disabled if this is enabled the ADC will
1:55
start the next conversion as soon as the
1:57
first one is finished
2:00
if that happens the conversion will go
2:02
on continuously and we won't be able to
2:05
control the sampling rate end of
2:08
conversion is set to single conversion
2:10
as we have enabled only one channel the
2:13
conversion data should be stored in the
2:17
only there is only one channel for now
2:20
and I have set the minimum sampling time
2:25
Cycles now let's check the available
2:27
external trigger sources for the ADC
2:31
if you browse through this list we have
2:33
various timers listed here choosing the
2:37
trigger out event of any timer is a
2:39
better option this trigger event is easy
2:43
timer I am choosing the timer one
2:46
trigger out event also the trigger Edge
2:49
is set to the rising Edge before we
2:53
configure the timer let's check the
2:57
h750 data sheet this diagram helps us
3:01
determine the clock structure of the
3:03
MCU here you can see the timer one is
3:06
connected to the apb2
3:08
bus here in the clock diagram you can
3:11
see that the apb2 timer clock is running
3:14
at 240 megaherz clock keep this in mind
3:18
while we configure the timer one set the
3:22
clock source as internal clock since the
3:25
timer is connected to apb2 bus the timer
3:31
mahz I am setting the prescaler to
3:34
24,000 this will reduce the timer clock
3:38
khz now if we use the auto reload value
3:41
of 1,000 the timer clock will reduce to
3:44
10 Herz we will use this frequency to
3:48
sample ADC channel here at the bottom
3:51
you can see the trigger event selection
3:54
there are two of them but I am using the
3:57
first one let's set the trigger to
3:59
update event doing this will generate an
4:03
event each time the timer's counter is
4:05
reset this event will trigger the
4:07
conversion start in the
4:09
ADC here you can see there are two
4:12
trigger outsources available from timer
4:14
one I have configured the first one
4:17
therefore the same is set here we will
4:20
read the ADC in interrupt mode so let's
4:23
enable the ADC interrupt in the nvic tab
4:27
let's set the PIN pa1 output so that we
4:31
can measure the sampling rate on the
4:33
scope all right that is all we need to
4:35
configure let's generate the project we
4:39
will write the main file now let's
4:42
Define a 16-bit variable where we will
4:44
store the value obtained from the
4:47
ADC inside the main function first start
4:50
the timer one base mode then start the
4:57
mode once the ADC converts is finished
5:00
an interrupt will trigger and the ADC
5:03
conversion call back will be called
5:06
inside this call back we will read the
5:08
ADC value and store it in the variable
5:11
we just defined now toggle the pin pa1
5:15
so that we can see the frequency of this
5:17
loop on the scope that is all let's
5:20
build the project now we do not have any
5:23
errors so let's launch the debugger I
5:27
have added the variable ADC value to to
5:29
the live expression you can see the ADC
5:32
value is updating every 100
5:35
milliseconds well we cannot be certain
5:38
about 100 milliseconds but we have set
5:40
the timer frequency to 10 Hertz
5:43
therefore it must be updating every 100
5:46
monds we will confirm this on the scope
5:49
in a while I am now rotating the
5:52
potentiometer in One Direction and you
5:54
can see the value is reducing
6:00
now I am rotating it in the other
6:02
direction and the value is
6:04
increasing the ADC resolution is 16 bits
6:08
therefore the maximum value obtained
6:17
65,535 let me leave it around
6:20
60,000 all right let's see the toggling
6:23
frequency of pin pa1 on the logic
6:26
analyzer here you can see the pin
6:31
milliseconds this means that the ADC
6:34
conversion complete call back is being
6:38
milliseconds so we were able to achieve
6:40
the ADC sampling rate of 10 htz with the
6:43
external trigger selection
6:45
mode the configuration Remains the Same
6:48
across other SDM 32 devices as well
6:51
therefore I am not showing it for others
6:55
now we will see the same for multiple
6:57
channels let's open the cube MX again
7:01
here we will enable three more channels
7:06
adc1 now go to the number of conversions
7:10
and set it to four the scan conversion
7:13
mode is automatically enabled make sure
7:16
that the continuous conversion mode is
7:18
disabled or else the ADC will start the
7:21
next sequence of conversion again as
7:23
soon as the first one is
7:25
finished this way we won't be able to
7:28
achieve the desired s sampling rate for
7:31
channels let's configure the channel
7:34
ranks I am setting the minimum sampling
7:39
channels all right since we are using
7:41
multiple channels we need to use the dma
7:44
to transfer the data let's go to the dma
7:48
tab and enable the dma request for
7:52
adc1 make sure the mode is circular and
7:55
the data width is configured according
7:57
to the ADC resolution
7:59
since we are using dma to transfer the
8:02
data the conversion data management
8:04
should be set to dma Circular mode also
8:08
we are converting four channels now so
8:10
the end of conversion should be set to
8:13
sequence this way the interrupt will
8:16
trigger only when all the channels have
8:19
converted this configuration is the same
8:21
as what we did for the multiple channels
8:23
using dma mode except that the
8:26
continuous conversion mode is disabled
8:30
now finally we need to configure the mpu
8:34
M7 enable the instruction and data cache
8:39
now enable the mpu in the default
8:42
mode I have already explained it in
8:44
multiple channels using dma
8:47
video basically we will relocate the
8:50
data buffer to another SRAM which is
8:52
located at the address 30 million
8:55
hex here we are simply configuring that
8:58
region as non-cashable able that is all
9:01
we need to configure let's generate the
9:05
again here is the article about the ADC
9:08
multiple channels using
9:15
dma here is the part explaining the
9:18
configuration for the cortex
9:20
M7 let's copy this buffer definition and
9:24
we will paste it in our
9:25
code also we need to create a section in
9:33
file now in the main function after
9:36
starting the timer we will start the ADC
9:39
in the dma mode the data will be stored
9:43
in the ADC value array and we want to
9:47
channels when all the channels have been
9:49
converted the conversion complete call
9:53
called we do not need to read the value
9:56
as it is already stored in the array
9:59
here we will simply toggle the pin PA
10:02
A1 that is all we need let's build and
10:07
project here you can see all the four
10:10
channels are updating
10:12
frequently to confirm the sampling
10:14
frequency we can check the pa1 on the
10:18
analyzer here you can see the pin
10:20
remains high and low for 100 millisecs
10:24
which means that the call back is being
10:26
called every 100 milliseconds
10:29
this means that the sampling frequency
10:31
is indeed 10 Hertz the first
10:34
potentiometer is set at
10:37
60,000 I am rotating the second
10:39
potentiometer to go around
10:41
50,000 then I am rotating the third one
10:46
30,000 and the fourth one is around
10:49
40,000 so the ADC channels are
10:52
responding fine and the sampling rate is
10:55
at 10 Hertz let me demonstrate a
10:58
different sample rate now here is the
11:01
timer one configuration and this is the
11:04
auto reload value we set at
11:07
1,000 I am changing this value to
11:10
10,000 doing so will reduce the timer
11:13
clock to 1 Hertz and this will be the
11:15
sampling frequency for the ADC so the
11:19
data will update every 1 second and the
11:21
pin pa1 will also toggle at this
11:24
interval let's see the result in the
11:27
debugger you can see the channel data is
11:30
updating every 1 second now let's check
11:33
the pin pa1 on the analyzer you can see
11:37
the pin remains high and low for 1
11:40
second so the conversion complete call
11:43
back is being called every 1 second and
11:45
therefore the sampling frequency is 1
11:48
Hertz so I hope you understood how the
11:50
sampling frequency of the ADC can be
11:53
controlled let's quickly see if there is
11:56
any change in configuration needed for
11:59
use here is the configuration for the
12:04
C8 under the external trigger Source we
12:07
have the events from different timers
12:10
you can see the trigger out event from
12:12
timer 3 so you can use it we can also
12:16
use the event from capture compare mode
12:19
but this requires a little more
12:21
understanding of the timer's operation
12:23
since the trigger out events are
12:25
available in every MCU we don't really
12:28
need to look into the capture compare
12:30
events but if you want I can make
12:33
another video explaining the usage of
12:35
capture compare mode to control the
12:38
frequency make sure the continuous
12:40
conversion mode is disabled while using
12:43
the external trigger Source otherwise
12:46
the external trigger is of no use that
12:49
is all for this video I hope you
12:52
understood how to control the ADC
12:54
sampling frequency using the timer you
12:57
can download the project from the link
12:59
in the description leave comments in
13:02
case of any doubt keep watching and have