0:00
welcome to controllers tech few months
0:03
ago I did a video on how to create delay
0:06
in microseconds using DWT although it
0:10
worked perfectly there were some
0:12
microcontrollers which does not support
0:15
DWT especially the f0 series and the F 3
0:20
series microcontrollers so today in this
0:23
video I will show you how to generate
0:26
delay in microseconds and nanoseconds
0:29
across all the stm32 controllers and to
0:34
do so I am going to use the timers let's
0:38
start with the cube MX first I am using
1:23
okay now the clock is set to the maximum
1:26
note that the APB to clock is at 180
1:30
megahertz and the APB 1 clock is at 90
1:34
megahertz now let's take a look at the
1:38
timers in the datasheet note that time
1:41
of 1 is connected to the APB - so I am
1:45
going to use that for the delay because
1:48
in order to create delay in nanoseconds
1:50
I need at least hundred megahertz clock
1:53
frequency for the timer set the timer at
2:09
now like I said I need hundred megahertz
2:13
for the delay in nanoseconds and it will
2:15
give me a delay of 10 nanoseconds
2:18
unfortunately I cannot go lower than
2:21
so the prescaler value is going to be
2:24
zero because we cannot divide the APB
2:27
clock any further I am writing one one
2:30
minus one just to indicate that whatever
2:33
value is input here the microcontroller
2:36
is going to add one to it so our actual
2:40
value is 1 and we are subtracting one
2:42
for the microcontroller to add it later
2:45
the auto reload register for timer 1 is
2:49
16-bit so I will input the maximum value
2:53
here and that is 0 cross F F F F which
2:57
turns out to be 65,536 in decimals also
3:03
I am setting pin p1 as output so that we
3:06
can measure the frequency in an
3:21
let's write the code now okay
3:24
first of all I will create a function
3:27
for the delay whose parameter is going
3:29
to be the delay we want remember that if
3:33
you input value one here that's going to
3:36
be a ten nanoseconds delay because our
3:39
clock is at 100 megahertz inside this
3:52
function we will set the counter to zero
3:54
and then let the counter increment until
3:58
it reaches the input value that we have
4:00
provided each count takes ten
4:02
nanoseconds in this case once it does
4:06
the control will come out from the loop
4:08
also remember that we cannot use the
4:11
delay higher than 65,536
4:15
because that's the limit for our counter
4:18
as it is only 16 bits if you want higher
4:21
values you have to use a 32 bit timer
4:25
but in my case the maximum clock to the
4:28
32 bit timers is 90 megahertz so I
4:34
inside the main function let's toggle
4:37
the pin after some delay
5:00
so if the delay is one that's going to
5:04
be ten nanoseconds now 100 nanoseconds
5:09
1000 900 second 10 microseconds 100
5:15
microseconds I am using this higher
5:18
delay because I don't have means to
5:20
measure the high frequencies so this
5:23
hundred microseconds is going to give me
5:26
a frequency of 10 kilohertz which I can
5:29
measure but as the pin will toggle after
5:32
every 100 microseconds the oscilloscope
5:35
is going to read it as 5 kilohertz
5:38
because this one reads the frequency off
5:41
pin toggling make sure you initialize
5:44
the timer in its normal mode let's build
5:47
this code and test it if you want one
5:53
nanoseconds delay your controller
5:56
frequency should be at least 1 gigahertz
5:59
similarly if you want the delay in
6:02
microseconds all you have to do is use
6:05
the pre scalar to divide the main time a
6:07
clock to get a 1 megahertz frequency so
6:11
the pre scalar value in that case would
6:22
as you can see here the frequency is 5
6:28
that means the nanosecond delay is
6:46
let me reduce the delay a little so that
6:49
we can increase the frequency now I am
6:52
using 50 microseconds delay technically
6:56
this should give me the frequency of 20
6:58
kilohertz but like I said the
7:00
oscilloscope is going to read the
7:02
frequency of pin toggling it is going to
7:05
read it as 10 kilohertz
7:19
as you can see it's reading 10 kilohertz
7:24
I hope you understood this guy's this
7:27
should work across all stm32 devices if
7:30
you choose the timer's properly by
7:32
taking a look at the datasheet this is
7:36
keep watching have a nice day