0:18
welcome to another video of controllers
0:22
this is another video in the
0:24
register-based programming series
0:26
and today we will see how to use the
0:31
i will focus on f4 series mainly but
0:34
wherever the changes are needed for f1
0:37
i will cover them too
0:44
let's start by creating the project in
0:55
let's give some name to this project and
1:03
i am using stm32f446re
1:10
here we will only select the cm syscor
1:20
now inside the project folder let's
1:27
in this main file we will create a main
1:31
and a while loop inside it
1:34
first thing that we are going to do is
1:36
copy the clock setup files inside our
1:44
now we will include those files in our
1:53
i hope you remember this file from the
2:00
let's copy the rcc config.h in our main
2:03
file create a function to configure the
2:08
pin here are the steps given to do so
2:14
first we need to enable the gpio clock
2:19
then set the pin as the input and
2:21
finally set up the internal pull
2:23
up or pull down resistance
2:26
let's start with enabling the gpio clock
2:31
in this tutorial i am using the pin pa1
2:35
so we need to enable the clock for the
2:40
this can be done in the rcca hb1 enable
2:45
as you can see here the zeroth bit
2:47
controls the clock for the g p
2:49
i o a so we need to write a 1
2:53
in the zeroth position
3:03
next we need to set the pin as input and
3:07
we modify the gpio mode register
3:15
to set the pa1 as the input pin we need
3:19
zero in the second and the third
3:35
and now finally we will configure the
3:38
and pull down register
3:44
but before doing that let's see how the
3:49
as you can see here the button is
3:52
pa1 and the ground so if we keep the pin
3:56
in the pull up mode and then press the
4:00
will discharge to the ground and it will
4:04
so keeping the pin in the pull-up mode
4:12
to set the pull up or pull down we will
4:15
modify the pull up or down register
4:18
here we will be using the pull-up mode
4:20
so we need to write a01
4:23
to the third and second positions
4:34
this completes the pin configuration in
4:37
now let's write another function to
4:39
configure the interrupt
4:41
to configure the pin as the external
4:44
we need to follow some steps and they
4:48
first of all we have to enable the
4:50
system configuration bit in the rcc
4:53
register this will basically enable the
4:56
configuration controller clock
4:59
then we will configure the external
5:01
interrupt in the system configuration
5:05
next we will disable the mask on the xd
5:08
using the interrupt mask register
5:11
then configure the rising or falling
5:13
edge trigger for the interrupt
5:16
and finally we will set the priority and
5:21
let's see them all first we will enable
5:25
the sys config bit in the rcc register
5:29
to do that we will look into the rcc apb
5:36
here you can see the 14th bit of this
5:39
register enables the system
5:41
configuration controller clock
5:49
but if you are using a f1 series mcu
5:52
like f103c8 you have to enable the
5:55
alternate function io clock for this
6:01
to do that you have to write a 1 in the
6:18
since i am writing this program for f4
6:21
i will continue using sys config bit
6:25
let's write a 1 in the 14th position to
6:33
next we will configure the external
6:35
interrupt in the sysconfig controller
6:49
as you can see here the external
6:51
interrupt config register 1 can control
7:04
similarly config register 2 can control
7:09
and in the end we have config register 4
7:12
which can control xd lines 12 to 15.
7:16
since we are using pin pa1 we would need
7:20
and therefore we will modify the config
7:28
if you want to configure this line for p
7:30
a port we need to write zero
7:32
zero zero zero in the fourth fifth sixth
7:35
and seventh positions
7:50
if you are using f1 series mcu the
7:53
external interrupt registers are exactly
7:56
but they are part of afio registers
8:03
so you need to do the same but with a
8:06
f i o registers instead of sys config
8:24
now we need to disable the mask on xd
8:27
and to do that we will modify the
8:29
interrupt mask register
8:36
as you can see here writing a 1 to the
8:39
respective position disables the
8:43
so we will write a 1 to the first
8:45
position in imr register
8:56
next is the configuration of rising or
9:02
this can be done in the rising trigger
9:06
and falling trigger selection register
9:11
basically writing a one in the
9:12
respective position will enable the
9:22
and the same goes for falling trigger
9:25
but should we select a rising edge
9:27
trigger or a falling edge trigger
9:30
well that depends on your requirement
9:35
let's see the button again here the pin
9:38
pa1 is pulled up by the internal pull-up
9:42
when we press the button this pin will
9:44
connect to the ground
9:46
and the pin will be low this is
9:49
considered as the falling edge
9:51
because the signal went low here as long
9:54
as the button is pressed
9:56
the signal will be low when we will
10:00
the pin will be pulled up again due to
10:02
the internal pull-up
10:04
and this will be considered as the
10:08
it's better for us to use the rising
10:11
since it will be triggered when we
10:13
release the button instead of when we
10:16
so we will enable the rising trigger for
10:19
by writing a 1 in the first position
10:23
and i am also disabling the falling edge
10:25
trigger for this xd line
10:28
now we will set the interrupt priority
10:31
we will use the inbuilt nvic functions
10:38
so go to the startup file
10:43
here you can see the list of all the
10:48
we are using xt1 so we need this handler
10:52
let's copy it and go back to the main
10:57
here we will call the function n-v-i-c
11:02
the first parameter is the interrupt so
11:04
copy the handler here
11:06
and replace the word handler with n
11:09
and now give the priority in the next
11:13
remember that lower is the number higher
11:18
that means zero have the highest
11:20
priority but since we are using only one
11:23
it doesn't matter what priority you give
11:27
now we will enable the interrupt and to
11:29
do that we will call nvic
11:31
enable irq this completes the interrupt
11:43
now let's write the interrupt handler
11:46
remember that we have to use the same
11:49
that is provided here
12:02
these are the steps for the interrupt
12:06
so basically we will check which pin
12:08
have triggered the interrupt
12:11
then we will process our data and clear
12:13
the interrupt pending bit
12:16
let's see the pending register for more
12:26
so what happens is whenever an interrupt
12:29
the respective interrupt pending bit is
12:32
set in the pending register
12:34
we can use this to check which pin has
12:36
caused the interrupt
12:39
then we process our data and clear this
12:48
so first we will check if the interrupt
12:50
is caused by the pin
12:58
if it is then we will clear the pending
13:14
i am going to use a flag here which will
13:17
whenever the interrupt gets triggered
13:19
and we can later use this flag in the
13:23
actually it would be better if we
13:25
process the data first
13:26
and then clear the pending bit so i will
13:29
fix this in the final code
13:32
let's create another variable which we
13:35
every time the interrupt is triggered
13:38
now let's write our main function
13:41
first of all we will initialize the
13:46
then configure the gpio
13:49
next configure the interrupt now inside
13:54
we will check if the flag is set if it
13:58
then we will increment the count
13:59
variable and reset the flag
14:03
let's create a function for delay also
14:05
which can take care of the de-bouncing
14:19
i think i need to use the 32-bit
14:23
add some small delay let's build it
14:34
we don't have any errors go to options
14:38
and change the frequency here according
14:47
select the st link debugger
14:55
okay let's debug this thing now
15:02
i am adding this count to the watch
15:26
seems like it's not incrementing
15:29
let's add a breakpoint inside the
15:42
looks like it's not able to execute this
15:48
pay attention guys as this is able to
15:52
that means the interrupt is definitely
15:54
being triggered by the pa1
15:57
and something is wrong with clearing the
16:06
so after i went through the data sheet
16:08
again i found out that the bit can be
16:10
cleared by writing a one in that
16:16
and i was writing a zero
16:21
let's write a one here now build
16:38
as you can see now the count is
16:42
whenever the button is released the
16:44
count gets incremented
16:54
note here that as long as the button is
16:57
nothing happens but when i release the
17:01
the count increments this is due to the
17:04
fact that we used the rising edge
17:07
so the interrupt is only triggered
17:09
whenever the pin goes to high again
17:12
this is it for this video i hope things
17:17
and you understood the external
17:20
you can download the code from the link
17:25
i have attached the files for both the
17:28
series and the f1 series
17:31
leave comments in case of any doubt
17:34
keep watching and have a nice day ahead