0:20
everyone welcome to controllers tech
0:25
this is fifth video in the register
0:27
based programming series
0:29
and today we will cover the analog to
0:33
let's take a look at the stm32f4
0:36
reference manual here is some
0:39
description given about the adc
0:42
and you can see we have configurable
0:45
which is not present in f103
0:48
adc on and off control
0:51
we can turn the adc on by setting the ad
0:54
on bit of cr2 register
0:57
and to start the conversion we have to
1:00
start bit again the adc can be disabled
1:04
by clearing the ad on bit
1:07
now let's talk about the adc clock
1:16
as you can see here the clock is common
1:20
and it is derived from the apb2 clock
1:23
and divided using the prescalers
1:27
we'll check the data sheet to find out
1:39
but before that this is my clock setup
1:42
that i used in the first video
1:44
and i will be using the same setup here
1:47
please note here the ap v2 clock is 90
1:53
now let's open the data sheet look for
1:57
characteristics as you can see the
2:02
clock that i can get is 36 megahertz
2:06
the vdda is 3.3 volts by default
2:10
so the max adc clock can be 36 megahertz
2:15
and as i have shown the apb2 clock is
2:17
running at 90 megahertz
2:19
so i cannot use the prescaler of 2
2:22
otherwise the adc clock will come to 45
2:26
and that's not possible in this
2:29
the minimum prescaler that i can use
2:31
with this clock setup is 4
2:33
and that's what i am going to use then
2:37
channel then select the conversion mode
2:40
and a lot of other things
2:42
i will cover them all but let's create
2:44
the project in kyle first
2:56
choose the microcontroller here
3:04
select the code and the device startup
3:12
here is our project let's add the main
3:15
file to this project first
3:29
now i am going to include the clock
3:36
we need to include them in our project
3:47
this is the same setup that i have used
3:52
you can see the first video in this
3:54
series to understand it
3:57
let's include the rcc config in the main
4:01
let's build it once no errors here
4:10
adc in it will be used to initialize the
4:15
here are the steps listed that we need
4:19
we need to enable the clocks first then
4:23
then configure the scan mode and
4:26
then configure the continuous conversion
4:29
data alignment etc let's start with the
4:42
configuration of the clocks first
4:48
clocks can be configured in the rcc
4:52
i will be using two channels of adc one
4:55
and both of them are connected to gpioa
5:00
so i need to enable the clock for the
5:05
to do so i need to modify the ahb1
5:10
zeroth bit here corresponds to gpio
5:14
and i need to write a one in this zeroth
5:18
that's what i did here now the adc
5:22
clock i am using adc one and to enable
5:26
i need to modify the ap b2 clock enable
5:37
as you can see the eighth bit is the adc
5:40
one enable bit so i need to write a one
5:44
in the eighth position
5:56
now the clocks are enabled let's set the
5:58
prescaler in the common control register
6:07
as i have already mentioned that my apb
6:09
2 clock is running at 90 megahertz
6:33
as you can see the bits 16 to 18
6:36
controls the prescaler
6:37
and here i cannot set it as 2 or else
6:40
the clock will exceed the maximum limit
6:44
so i will set the prescaler as 4 and
6:46
write a 1 in the 16th position
6:50
now configure the scan mode and the
6:52
resolution in the control register 1.
7:07
bits 24 and 25 control the resolution
7:10
and i will keep them zero since i will
7:13
be using 12 bit resolution
7:20
i need to enable the scan mode since i
7:22
am using two channels
7:24
so set the bit 8 to 1.
7:52
now the configuration of control
8:08
i will keep the data alignment to the
8:10
right so the bit 11 will be zero
8:14
also i want the end of conversion flag
8:16
to set after each conversion
8:18
so the bit 10 will be one
8:26
and the continuous conversion is also
8:45
next is the selection for the sampling
8:56
we have two sampling registers here and
8:59
these denotes the channel number
9:02
since i am using the channel 1 and
9:05
i need to use the sample register one
9:09
i have made a video on sampling time and
9:12
you can check it on the top right corner
9:15
since i am not using any adc device that
9:18
needs a particular sampling rate
9:20
i will just use the sampling of three
9:22
cycles for simplicity
9:25
so i need to set zeros at the third and
9:27
the twelfth positions for channel one
9:50
now we need to configure the sequence
9:58
adc sequence register 1 have this l bit
10:03
this basically indicates how many
10:05
channels are we using
10:07
this bit must be zero for single channel
10:10
one for two channels
10:11
and it should be 15 if you are suing all
10:16
since i am using 2 channels i will write
10:20
in the 20th position the rest of the
10:32
sequence configuration will come later
10:40
at last we need to configure the pins as
10:45
this can be done in the gpio mode
10:55
to set the pins as analog we need to
10:58
write a 3 in the respective
11:06
pins pa1 and pa4 are set as analog
11:10
pins this completes the initialization
11:16
now this function will enable the adc
11:20
steps are mentioned here first we will
11:24
enable the ad on bit to enable the adc
11:26
and then wait for some time for the adc
11:36
in the control register 2 bit 0 is the
11:41
and we need to set it to 1.
11:47
and now we will wait for a small amount
11:55
now the function to start the adc and it
11:58
takes the channel number as the
12:04
here first we will set the channel
12:10
then clear the status register and
12:13
finally start the conversion by setting
12:29
let's see the sequence registers
12:33
here we have three sequence registers
12:35
and together they have 16
12:37
sequences so basically you can put the
12:40
16 channels in the sequence
12:43
that you want the conversion to happen
12:45
and these registers will take care of
12:48
but that can be handled effectively with
12:52
since we are not using any dma i will
12:55
only use one sequence
12:57
and put one channel in the sequence
13:04
first clear the sequence register and
13:06
then write the channel number in the
13:26
clear the status register and finally
13:30
we will start the conversion by setting
13:32
the 30th bit on the cr2 register
13:52
wait for conversion we'll wait for the
13:54
conversion to finish
14:03
eoc bit on the status register gets set
14:06
when the single conversion is complete
14:08
or the conversion of the sequence is
14:11
so we will look for this bit to set
14:20
and after the conversion is complete we
14:22
will copy the data from the data
14:29
we can also disable the adc by clearing
14:32
the ad on bit in the cr2 register
14:40
that's all regarding the adc functions
14:47
let's create an array to store the adc
14:55
in the main function let's call a system
14:57
clock configuration first
15:00
now initialize the adc
15:03
enable the adc in the while loop
15:07
first we will start the adc with channel
15:10
one now wait for the conversion to
15:17
and finally read the value
15:24
we will do the same for the channel 4
15:33
so that's all let's build it now let's
15:40
some settings type the clock frequency
15:44
mine is running at 180 megahertz
15:48
go to debug and select st link
16:09
i will add this adc value to the watch
16:30
here i have connected the channel 1 to
16:32
ground and the channel 4 to 3.3 volts
16:36
and that's why channel 1 reads 0 whereas
16:39
the channel 4 reads 4095
16:43
now i am changing the channel 1 to 3.3
16:47
and channel 4 to the ground and you can
16:49
see the readings also changes for them
17:10
and now when both are grounded both
17:15
this method we used today is like
17:17
polling for the conversion to finish
17:20
and also you saw we were able to use
17:23
multiple channels without even using the
17:26
which is kinda not possible with hal
17:30
using registers gives us more control
17:33
and that's why sometimes it's the best
17:37
obviously i will cover the dma and
17:41
but first i want to cover some basic
17:45
this is it for this video i hope you
17:50
you can download the code from the link
17:54
leave comments in case you have any
17:58
keep watching and have a nice day ahead