0:21
welcome to controllers tech this is the
0:24
second video in the series of
0:26
register-based programming for stm32
0:29
and today we will see how can we use
0:31
timer to generate the precise delay in
0:34
and milliseconds let's start by creating
0:51
select your microcontroller here
0:57
the only files we need to add are the
1:00
and the device startup files
1:06
in my previous video i covered how to
1:10
and today i will simply include that
1:24
we need to add the file here also
1:33
you can see the rcc config file have all
1:37
that we covered in the first video with
1:41
the system clock would be running at 180
1:45
and this video will basically prove that
1:47
all that setup was completely fine
1:50
we will see more about this in a while
1:54
let's add the main.c file in our project
2:02
i am going to include the rcc config
2:06
let's also create the main function and
2:09
a while loop inside it
2:16
now let's go to timers as i mentioned
2:20
i am going to use timer to create a
2:25
here in the reference manual you can see
2:27
that we have advanced timers
2:29
some general purpose timers and some
2:33
since we only need the delay i will use
2:36
timer for this purpose i have two basic
2:44
i am going to use timer 6 for this
2:55
this is the data sheet of f446
2:58
and this is the clock diagram as you can
3:02
the timer 6 is connected to a pb1 clock
3:05
whose core frequency is 45 megahertz
3:13
now this is how my clock is configured
3:17
i have covered this in the first video
3:19
so if you don't understand
3:21
see that one first here i am using the
3:25
external 8 megahertz crystal to generate
3:27
a system clock of 180 megahertz
3:32
now if you notice here the apb1
3:35
timer clock is running at 90 megahertz
3:39
so i need to divide this 90 megahertz in
3:42
order to get the required delay
3:45
let's create a function for timer
3:49
these are the steps that we are going to
3:53
first enable the timer peripheral clock
3:57
then set the prescaler and the arr
4:02
and finally enable the timer
4:05
let's start with the first step in order
4:08
to enable the peripheral clocks
4:10
we need to modify the rcc registers
4:23
rcc peripheral clock enable register
4:26
to control the timer 6 clock
4:39
if you are using any other timer it
4:41
might be present in apb2 register also
4:49
here we need to write a one to the
4:51
fourth bit of apb1enr
4:57
that's exactly what we are going to do
5:05
now it's time to set up the prescaler
5:10
if you remember the apb1 clock is at 90
5:18
if i divided by 90 the clock will come
5:24
a one megahertz clock means each count
5:27
by the counter will take one microsecond
5:30
and this way we can get the delay in
5:40
to set the prescaler we will look in the
5:47
here we have the prescaler register
5:49
where we can directly input the value
5:56
this is the auto reload register which
5:58
is basically the maximum counts
6:01
the counter can have also note that
6:04
a 1 will be added to the prescaler value
6:09
so we will subtract 1 from the actual
6:17
let's enter the prescaler now i want to
6:20
write 90 but because of that one being
6:24
i am writing 90 minus 1.
6:31
provide the maximum a r r value
6:34
so the counter can have a higher limit
6:39
now the final step is to enable the
6:43
to do so we will look into the timer
6:57
setting the bit zero of this register
7:08
so let's set the bit 0 of timer 6 cr1
7:15
we also need to wait for the update flag
7:19
let's take a look at the timer status
7:23
here we only have one bit uif
7:26
and this bit is set whenever the
7:28
registers are updated
7:34
so we will wait for this bit to set
7:41
this completes the timer configuration
7:44
now let's create a function to create
7:53
here we will first set the counter to
7:56
and then wait for the counter to reach
8:00
as each count by the counter takes 1
8:03
we will get the desired time delay
8:06
timer 6 have a count register where the
8:18
we will first set the counter to zero
8:20
and then wait for it to reach the given
8:23
this waiting is in the blocking mode and
8:26
hence we will get the delay
8:28
let's write another function for the
8:44
here we will just call the delay for
8:48
to achieve one millisecond delay time
8:52
now let's configure the gpio clock
8:56
i am using pin pa5 as the output for led
9:01
here are the steps required to configure
9:06
let's start with enabling the clock
9:09
again we need to look into the rcc
9:17
this time the ahb1 peripheral clock
9:28
here you can see the gpioa
9:32
clock can be enabled by setting the bit
9:34
zero of this registers
9:38
so we will set the bit zero of the rcca
9:46
next we need to set the pin pa5 as
9:50
output to do so we will take a look at
10:00
gpio mode register is used to set the
10:03
different functions for the pin
10:08
here we want to set it as output
10:13
bit 0 and 1 configures the pin 0
10:16
bits 2 and 3 for pin 1 bits 4 and 5 for
10:21
bits 30 and 31 for pin 15.
10:25
i want to set the pin pa 5 so i have to
10:35
so basically we need to write a 0 in the
10:39
and a 1 in the 10th position
10:47
i will just write a 1 in the 10th bit as
10:50
the 11th bit is 0 by default
10:53
next configure the output mode
10:57
let's go back to the registers
11:01
we have port output type register
11:04
here we need to set what type of output
11:08
for toggling the pin we want the
11:12
and that's why we need to write zero in
11:24
in the gpioa o type register
11:27
write a zero in the fifth position
11:33
next we need to select the speed of the
11:37
we can work with even low speed here but
11:40
i just want to explain
11:41
how can we use higher speed for the pin
11:44
here also two bits are configured for a
11:53
i want to select the fast speed so i
11:56
need to write a 1 in the 11th position
11:59
and a 0 in the 10th position
12:06
so i will just write a 1 in the 11th
12:10
as the others are anyway zero
12:16
next is the pull-up or pull-down
12:20
we don't want any of it since we are
12:22
going to toggle the pin
12:25
so we need to write 0s in 10th and 11th
12:30
i don't need to write these zeros since
12:32
that's the reset state of the register
12:34
but i want to show you how can we write
12:37
zeros in more than one position
12:39
at the same time so either you can write
12:42
a zero in the tenth position
12:44
and eleventh position separately
12:48
or you can first write a one in both the
12:51
and then negate the entire statement
12:54
so this completes our gp io
12:59
now let's write the main function first
13:03
call the system clock configuration
13:06
then the timer configuration and gpio
13:18
now in the while loop first of all i
13:30
to do so we need to look into the bsr
13:34
register this register consists of two
13:39
the upper bits are used to reset the pin
13:42
and the lower bits are used
13:44
to set the pin in order to set the fifth
13:47
pin we need to write a one in the fifth
13:50
and in order to reset it we need to
13:52
write a one in the 21st position
13:59
let's set the pin pa first
14:07
give a delay of one second
14:17
i prefer using this method to reset
14:20
instead of writing a 1
14:22
in the 21st position
14:26
give the delay again and that's all
14:31
let's build the code we have no errors
14:35
before flashing it go to the options
14:38
type in your clock frequency here
14:41
mine is running at 180 megahertz
14:48
go to debug select the sd link
14:52
go to setting select connect under reset
14:55
and select reset and run
15:02
let's load the program into the board
15:06
as you can see here the led is blinking
15:09
every second you can time the blinking
15:22
we have a perfect one second delay
15:31
let's change the delay time to 500
15:40
build and flash again
15:52
now the led is blinking every 500
16:01
let's change it to 250 milliseconds
16:11
build and flash again
16:22
you can see the results basically
16:26
this proves that whatever clock setup we
16:28
did in the previous video
16:30
was correct since i programmed the timer
16:33
based on those frequencies
16:35
so if there was some error in the clock
16:37
setup we wouldn't have got the exact
16:40
like we did get this is it for this
16:45
i hope things were very clear we can use
16:48
this delay for our upcoming tutorials
16:51
where again i will simply include this
16:55
you can download the code from the link
16:59
in the next video i will cover the basic
17:03
and receiving goodbye for now
17:07
keep watching and have a nice day ahead