0:20
welcome to another video of controllers
0:24
after i made some videos of register
0:26
programming with stm32f4 series a lot of
0:30
you guys requested the same for the f103
0:35
blue pill is a very popular device from
0:38
so i have decided to create a video for
0:40
the basic setup on stm32f103
0:45
this video will cover the basic clock
0:47
setup and timer setup
0:49
for the delay in micro and milliseconds
0:52
i can't continue making separate videos
0:55
for different microcontrollers for each
0:58
so i will just continue making videos
1:01
but i will also include the code for
1:03
f103 in the final download
1:07
mostly the things will be same but i
1:09
will comment the code properly
1:11
for better understanding so today we
1:14
will cover the clock setup and the timer
1:17
delays so let's start by creating a new
1:24
give some name to the project and click
1:29
select the controller here now we will
1:40
only select the cmsis core
1:42
and the device startup files from here
1:53
let's first add the main file to this
2:01
inside this main file i am creating the
2:05
and a while loop so this completes the
2:10
now first of all we will include the
2:20
if you see inside the file here you have
2:22
all types of registers
2:24
interrupts and pretty much everything is
2:40
before going forward let's include the
2:50
copy them inside the project folder
3:02
and inside the project add those files
3:09
you can see the files got added here
3:12
this delay header file have these
3:14
functions that we can use anywhere in
3:22
we will talk about them in a while let's
3:25
include the delay header file in our
3:34
now we will set up the clocks first
3:40
this process was quite difficult in f4
3:43
but here it's pretty simple we just need
3:46
to call this function
3:48
system init and we are done
3:53
let's see what's going on inside it
4:00
so system in it is predefined in the
4:03
that we included in our project it does
4:06
all the operations that we did in that
4:16
even though everything is predefined you
4:18
still have some control over the clocks
4:21
if you go to the top of this page here
4:24
you see the different clocks are defined
4:32
you can uncomment the clock that you
4:36
by default it is 72 megahertz and this
4:39
is the maximum clock that blue pill can
4:42
with i am okay with this setup
4:45
and i will leave it as it is note here
4:48
if you comment out every clock then the
4:51
system will run at internal clock
4:53
also known as hsi high speed internal
4:58
so choose the clock according to your
5:02
if you want any other setup other than
5:04
these predefined ones
5:05
you need to follow my other video in
5:08
which i covered the clock setup for f4
5:11
the steps will be same with minor
5:15
this is it for the clock setup now
5:18
before going into timer setup
5:20
let's see the clocks with cube mx once
5:46
this is how the current clocks are
5:49
we will come back to this
6:02
now let's see the delay.c file
6:06
as you can see here i am using timer 2
6:11
here are the steps given to configure
6:15
we need to enable the timer 2 clock
6:19
then according to the requirement set
6:22
and the auto reload register values
6:26
and finally enable the timer and wait
6:31
let's take a look at them now in order
6:34
to enable the timer clock
6:35
we need to see the rcc register
6:49
to be precise rcc apd1 enable register
6:53
here you can see the bit zero of this
6:56
register enables the timer two clock
7:07
so that's what i did i am writing a one
7:10
in the zeroth position
7:12
these commented lines here are the
7:14
default setup for timer
7:16
as they all are zeroes we don't need to
7:20
but anyway i have commented the code so
7:22
that you can understand what they mean
7:25
now we need to set the prescaler and
7:30
prescaler divides the timer clock and it
7:33
is used to reduce the frequency of high
7:35
peripheral clocks to whatever working
7:39
before setting the prescaler we need to
7:42
understand the clocks
7:44
basically the timers are connected to
7:48
blue pill have two peripheral clocks
7:52
and apb2 as you can see here
7:56
the apb1 timer clock is running at 72
8:00
and so does the apb b2 timer clock
8:04
since all the timers are connected to
8:07
all the timers will also be running at
8:11
but this is too convenient what if you
8:14
have some different setup here
8:17
to know the timer clock you need to know
8:19
where the timer is connected
8:22
to know that look at the data sheet for
8:25
and there you will see this clock
8:28
as you can see here the timer 1 is
8:31
connected to the apb 2 clock
8:33
and timer 2 is connected to the apb1
8:42
similarly you can see the other timers
8:46
as i am using timer 2 which is connected
8:50
and which is running at 72 megahertz
8:54
this means my timer clock is also 72
8:59
now a prescaler of 72 will further
9:03
and bring it down to 1 megahertz
9:06
we need to write -1 here and the reason
9:09
can be seen in the reference manual
9:12
let's understand the auto-reload
9:17
so when we enable the timer the counter
9:20
starts counting from zero
9:22
since i have set up the timer clock at
9:26
each count of this counter will take one
9:30
this counter will continue counting and
9:32
the maximum count that can reach is the
9:34
auto reload register value
9:37
so this is why we need to set the
9:40
maximum value for the arr
9:42
so that our counter can avoid reaching
9:46
now we will enable the timer and wait
9:48
for the update flag to set
9:51
in order to enable the timer we need to
9:53
look in the control register 1.
10:11
as you can see the zeroth bit of cr1
10:14
register enables the timer counter
10:16
so we need to write a 1 in the 0th
10:26
for the update flag we will check the
10:38
bit 0 is the uif bit
10:41
and you can see this bit is set when the
10:44
content of the register are
10:51
so we will wait for this bit to set this
10:54
completes the timer configuration
10:58
now this function here is the function
11:00
to generate the delay in microseconds
11:04
basically we will set the counter to
11:06
zero and wait for the counter to reach
11:10
as i have already mentioned that our
11:14
a way that each count takes 1
11:15
microsecond so this will result as delay
11:18
in desired microseconds
11:21
now let's talk about the gpio
11:26
i will blink the on-board led and to do
11:28
that we need to set the pin as output
11:32
we all know the led is connected to pin
11:41
the steps are very simple enable the
11:46
clock and configure the pin as output
11:51
let's enable the gp io clock first
11:55
we need to go back to our cc registers
12:11
as you can see in the apb2 enable
12:14
4th bit controls the port c clock
12:25
to enable the gpioc clock
12:28
we will write a one in the fourth
12:34
next step is to configure the pin as
12:38
and to do so we will go to the gpio
12:47
the first one here is the port
12:49
configuration register
12:52
this register is responsible for
12:54
configuring the mode of the pin
12:56
and its type as you can see here
12:59
the first two bits set the mode for the
13:01
pin and the next two bits configure that
13:04
mode similarly the next four bits
13:07
controls pin one mode
13:09
and its configuration this register is
13:12
divided into two parts
13:15
control register low is responsible for
13:18
controlling bits 0 to 7
13:21
and since we are using pc13 we will go
13:24
to the control register high
13:32
here we will set the output mode
13:35
you can choose the speed according to
13:39
i am choosing 10 megahertz speed
13:43
so in order to set the pin 13 as output
13:46
we need to write 0 and 1 to the 21st and
13:51
basically write a 1 at the 20th position
14:04
now for the configuration we are using
14:07
so these will be valid
14:14
i am choosing push-pull type output as
14:17
it's better for blinking a led
14:19
so i need to write zeros in the 22nd and
14:24
these positions are zero by default so i
14:27
am not writing anything there
14:47
then we have port bit set or reset
14:50
which we will come to in a while this
14:53
completes the configuration for the gpio
14:58
now let's write our main function first
15:01
of all call the gpio config
15:05
then call the timer config now inside
15:10
we will turn the led on and off
15:13
to do that we need to see the bsr
15:17
this register is divided into two halves
15:21
bits 0 to 15 can be used to set the pin
15:26
and 16 to 31 can be used to reset the
15:31
since we are interested in pin 13 in
15:33
order to set this pin
15:35
we need to write a 1 in the 13th
15:37
position and to reset the pin
15:39
we need to write a 1 in the 29th
15:44
let's set this pin pc13
15:51
now we will give some delay here
15:59
i will use this delay m s function to
16:02
set the delay in milliseconds
16:04
set the 1 second delay now reset pc
16:08
13 by writing a 1 in the 29th position
16:13
and give 1 second delay again
16:16
this is it let's build it now
16:23
we don't have any errors before
16:27
make sure you make changes in the
16:54
as you can see the led is blinking every
16:58
this means the clock setup the timer
17:03
setup everything went well this wraps up
17:07
the basic configuration needed for the
17:10
you can use the same configuration in
17:13
other videos as well
17:15
i will continue making these videos for
17:19
but i will upload the code for blue pill
17:22
if there are too many changes in the
17:24
process i will make a separate video for
17:27
blue pill for that particular peripheral
17:30
you can support this channel by joining
17:33
plan the download link for this
17:36
code is in the description that's all
17:42
keep watching and have a nice day ahead