0:09
hello and welcome to controllers Tech
0:12
today we will start a new series
0:14
covering the ADC peripheral of the SDM
0:18
microcontrollers we will cover different
0:20
ways of reading the data from The Analog
0:23
Devices throughout this series this is
0:26
the first video of the series and today
0:28
we will see how to configure the ADC and
0:31
how to use the polling mode to read the
0:33
single channel of the ADC
0:35
peripheral I will use the stm32 h750
0:39
development board throughout this series
0:42
but we will also see the configuration
0:44
available in the F4 series and stdm 32
0:52
h750 and h74 3 development boards are
0:56
available on the controller's tech store
0:58
so you guys can purchase these boards
1:01
from there let's start the cube ID and
1:06
project I am using stm32 h750 vbt
1:12
6 give some name to the project and
1:16
finish let's see the schematics of this
1:19
board before proceeding
1:21
forward here you can see that there is
1:24
an external 25 MHz Crystal available on
1:27
the board we will use this Crystal to
1:30
provide the clock to the
1:32
board let's open the RCC configuration
1:36
and enable the high-speed external
1:38
Crystal we will configure the clock in a
1:41
while let's enable the ADC first the
1:45
channels 0 to two are not available and
1:48
we have channel 3 as the first channel
1:50
here the ADC can be selected in either
1:53
differential mode or the single-ended
1:57
mode we will cover the differential mode
1:59
later in the series so for now let's
2:02
select the single-ended
2:04
mode single-ended mode is basically the
2:07
usual ADC we have available on other SDM
2:10
32 boards as well let's configure the
2:13
clock now this board has 25 MHz Crystal
2:18
so set the value 25 here I will use the
2:22
PLL to run the system at maximum
2:27
MHz let's see the configuration
2:30
available on the other SDM 32
2:33
MCU here I am creating a dummy project
2:37
MX we will see the configuration for the
2:42
6re let's enable Channel Zero of the
2:46
ADC here we do not have the option to
2:49
choose the differential or single-ended
2:51
mode the ADC by default is using the
2:55
single-ended mode and we can't use
2:57
different modes with this MCU
3:00
we will see the rest of the
3:02
configuration in a while let's go back
3:05
project here you can see the ADC clock
3:09
is at 75 MHz right now the ADC
3:13
peripheral is using the clock from PLL
3:15
2p so we can modify it here to vary the
3:21
ADC you can even change the ADC clock
3:24
Source but I will leave it to the
3:26
default one now let's take a look at the
3:31
diagram here we don't have a separate
3:35
ADC to know the ADC clock we need to
3:39
check the data sheet of the
3:41
MCU here look for the block diagram
3:44
which basically contains the clock
3:47
configuration as you can see the adc1 is
3:51
connected to the apb2 bus if you look in
3:54
the clock configuration the apb2 clock
3:57
is at 16 MHz right now now this makes
4:01
the ADC clock also at the same
4:04
frequency let's come back to our project
4:07
now we have enabled the adc1 channel 3
4:11
and hence the pin pa6 is selected as the
4:13
ADC pin we will connect the
4:16
potentiometer output to this pin let's
4:19
configure the ADC now the mode is set to
4:23
Independent mode because this ADC is
4:26
working independently right now we can
4:29
use the the combined mode by enabling
4:31
another ADC as well but that will be
4:33
covered in the future videos the ADC has
4:37
the configurable resolution which can be
4:39
set between 8 Bits to 16 bits the
4:43
resolution configuration is also
4:45
available on the F 446 and here we have
4:48
the options to choose between 6 bits to
4:50
12 bits but all the SDM 32 MCU do not
4:55
have the option to choose the ADC
4:58
resolution let's quick see the
5:00
configuration for the SDM 32 f103 C8
5:05
let's enable the adc1 channel 0o you can
5:09
see we do not have the option to choose
5:11
the ADC resolution in such cases you can
5:15
refer to the reference Manual of the MCU
5:18
you are using here if we check the ADC
5:21
section it is clearly mentioned that the
5:23
MCU supports 12bit resolution only
5:27
basically we can't choose the resolution
5:29
it is fixed in today's video I am
5:33
choosing the 16-bit resolution for the
5:35
ADC and hence the ADC data will vary
5:41
65535 next is the scan conversion mode
5:44
which is used while using the multiple
5:47
channels we are using a single Channel
5:49
only so let's leave it disabled The
5:53
Continuous conversion mode enables the
5:55
ADC to start another conversion as soon
5:57
as the first conversion is over
6:00
let's keep it disabled so that we can
6:02
see the difference the discontinuous
6:05
conversion mode is just the opposite of
6:07
the continuous conversion mode the end
6:10
of conversion sets the flag when either
6:12
a single conversion is completed or a
6:15
sequence of conversions is complete for
6:18
a single Channel we will leave it to
6:20
single conversion only I will leave the
6:23
rest of these to default we will see
6:25
their usage in the future videos the
6:28
channel configur ation can be found
6:30
under the rank section the channel is
6:33
set to channel 3 as we have enabled only
6:36
that the sampling time is basically how
6:39
long the ADC will take to sample and
6:42
convert the data we will cover the ADC
6:45
sampling and conversion time in a
6:47
separate video let's set it to maximum
6:50
now that is all the configuration we
6:53
need let's check the configuration
6:56
available in the f103 C8
7:00
here we have the data alignment which is
7:02
set to write align since the ADC has the
7:06
resolution of 12 bits the data can be
7:08
only stored in the 16-bit variable this
7:12
data alignment basically sets whether to
7:14
align the 12bit data to the right of the
7:17
array or to the left of the array then
7:20
we have the scan conversion continuous
7:23
conversion and discontinuous modes as
7:26
usual under the rank section we have the
7:29
same option option to choose the
7:30
sampling time now let's quickly see the
7:33
configuration for the f44
7:36
6re here we have the clock prescaler
7:39
which can be used to further divide the
7:41
APB clock so to reduce the clock fed to
7:45
ADC for example the apb2 clock is at 16
7:49
mahz right now and if we use the
7:52
prescaler of 8 the ADC clock will be
7:56
mahz then we have the option to choose
7:59
resolution the data alignment and the
8:03
modes we have a new Option here to
8:06
enable The dma Continuous
8:08
request this is basically used when you
8:11
want the dma to continuously receive the
8:15
peripheral this option is not available
8:18
in the f103 C8 MCU so there you can
8:22
simply enable the dma and it will
8:24
automatically fetch the data
8:27
continuously here under the rank section
8:29
we again have the same options to choose
8:33
Etc we also have the option to choose
8:40
MCU for example the ADC clock is at 15
8:44
MHz right now and if we use the
8:46
prescaler of 10 the ADC clock will
8:52
mahz all right that is all we need for
8:54
now click save to generate the
8:57
project here is the picture showing the
9:00
connection between the potentiometer and
9:02
the board the VCC from the potentiometer
9:06
is connected to the 3.3 Vols ground is
9:09
connected to the ground and the data pin
9:11
is connected to the pin
9:14
pa6 all right we will start with the
9:16
polling mode to get the ADC data let's
9:20
Define a 16-bit variable to store the
9:22
ADC data we want to get the results
9:26
continuously so we will write the code
9:32
ADC then pull for the conversion to
9:36
complete we will wait 100 milliseconds
9:38
for the ADC to sample and convert the
9:41
result then read the ADC value and store
9:44
it in the variable we
9:47
defined finally stop the
9:49
ADC let's give a delay of 500
9:52
milliseconds between each call we will
9:56
also increment the count variable so to
9:58
make sure if everything is working all
10:00
right inside the while loop all right
10:03
that is all we need to read the single
10:05
Channel ADC using the poll for
10:08
conversion method let's build the
10:11
project and debug it to see the variable
10:14
value let's set ADC value and the count
10:18
variables to live expression you can see
10:20
the variation in the ADC value the
10:23
fluctuations in the ADC values are
10:26
normal the count variable is
10:28
incrementing every 500 milliseconds so
10:31
our while loop is working normally now
10:34
let's Slide the potentiometer to the
10:36
other end you can see the ADC value is
10:40
incrementing as well now we are
10:43
somewhere near the middle and the ADC
10:47
39,000 let's keep sliding it towards the
10:50
other end now we have reached the other
10:53
end the resistance of the potentiometer
10:55
is maximum at this point so is the ADC
11:00
since the resolution is set to 16 bits
11:03
the maximum value is
11:06
65,535 and it is what we got here so the
11:10
ADC value is varying as we Slide the
11:13
potentiometer but we are not quite
11:14
getting the value zero at the lowest
11:17
point the ADC value is
11:21
1,700 this is fine for now as we can
11:24
still refer to this as our lowest value
11:26
some of the stm32 MC use support the ADC
11:30
calibration we will talk about it in the
11:33
future videos now let's assume that we
11:36
want to map this ADC value to some
11:38
specific range we can use this map
11:42
function which I took from the Arduino
11:44
source code call the map function after
11:47
the ADC value has been captured the
11:51
output will be stored in the value
11:53
variable we want to map the ADC value
11:56
the minimum input value is 1,000 the
12:02
65,535 and we want to map it between 0
12:05
to 100 actually the minimum input value
12:08
should have been zero but since we are
12:11
only able to achieve 1,700 with our ADC
12:16
1,000 anyway let's build and debug this
12:20
project again we will add the value
12:22
variable to the live
12:26
expression you can see that the value
12:28
variable is incrementing as I am moving
12:31
the slider it is towards the middle now
12:36
58 and when we move it towards the end
12:39
the value has reached 100 it is the
12:42
maximum output value we set in the map
12:45
function so we can use the map function
12:48
to get the output in terms of voltage or
12:50
in percentage resistance so the poll
12:54
method to read the ADC data is working
12:56
fine in this polling method the CPU is
13:00
blocked for the ADC operation and we
13:02
cannot perform any other task during
13:04
that time this is where the interrupt
13:07
comes in we will cover the interrupt and
13:10
dma in the next video I hope you
13:13
understood how to configure the ADC to
13:16
use the polling method the polling
13:18
method does block the CPU but this is
13:21
good enough if you do not have any time
13:23
sensitive requirement from the ADC we
13:26
can use this method in combination with
13:28
the RTO to read the ADC values every few
13:32
milliseconds this is it for the video
13:35
you can download the project from the
13:37
link in the description leave comments
13:40
in case of any doubt keep watching and
13:43
have a nice day ahead