0:09
hello and welcome to controllers Tech
0:13
this is the continuation of the stm32
0:16
timer series and we will continue with
0:19
the timer synchronization
0:21
today in the eighth video of the series
0:23
we will discuss how to Cascade the
0:25
timers together and make a bigger
0:27
counter than the default 16-bit
0:30
the video title says making a 48-bit
0:33
counter but actually you can make the
0:35
counter however big you want depending
0:37
on the number of timers you have
0:40
you must watch the previous video to
0:42
understand how the cascading works for
0:44
these timers the video is in the top
0:48
we will first see what is the need for
0:52
let's assume our APV timer clock is at
0:55
90 megahertz and we use the prescaler of
0:58
9 to bring it down to 10 megahertz
1:01
this 10 megahertz is counter frequency
1:04
basically the counter will be counting
1:08
this means each count in the counter
1:10
will take 0.1 microseconds
1:13
now most of the time is in stm32 are
1:16
16-bit in size so the Maximum Auto
1:19
reload value we can set is 65535
1:24
this is the maximum number of counts the
1:29
this makes the period of the counter
1:31
equal to 6.5 milliseconds
1:34
and the lowest frequency we can measure
1:36
with this counter will be 150 Hertz
1:40
so ideally the counter can measure the
1:43
highest frequency of 10 megahertz and
1:48
obviously it cannot measure 10 megahertz
1:50
due to the limitations like interrupt
1:53
processing time but let's assume for now
1:57
now the problem is how do we measure
1:59
even lower frequencies like 100 Hertz or
2:03
50 hertz or even lower ones like one
2:07
of course you can reduce timer clock by
2:10
increasing prescaler but then it will
2:12
also reduce the highest frequency you
2:15
the issue can be resolved by using a
2:20
say for example if we had a 48-bit
2:22
counter we could get the auto reload
2:29
the counter is still running at 10
2:31
megahertz but the maximum number of
2:34
counts have been increased now and so it
2:36
can measure even the very low
2:39
we will achieve this by cascading the
2:41
timers together so depending on how many
2:44
timers you can Cascade you can make the
2:46
counter as big as you want
2:49
let's start the cube IDE and create a
2:53
I am using stm32f446re
2:58
give some name to the project and click
3:02
first of all I am selecting an external
3:05
Crystal to provide the clock
3:07
the board has 8 megahertz Crystal on it
3:10
and I want the system to run at 180
3:17
let's configure the timers now
3:20
I am configuring the timer 1 in the
3:22
input capture mode so that we can
3:25
measure the input signal
3:27
the pin pa8 will be used as the input
3:32
actually since I gave you the example
3:35
with a PB clock the 90 megahertz let's
3:38
change the system clock to 90 megahertz
3:41
the apb2 timer clock is at 90 megahertz
3:44
now and the timer 1 is connected to it
3:47
so the timer 1 is also running at 90
3:51
let's use the prescaler of nine this
3:54
will bring the clock down to 10
3:57
also I am using the auto reload value of
4:00
10 000. I will show the calculation in a
4:05
the timer 1 is the master timer so we
4:08
will enable the trigger event selection
4:11
here is the configuration for the input
4:13
capture and I am leaving it as it is
4:17
let's understand the calculation now
4:20
the counter one frequency is 10
4:24
this means that each count in the
4:26
counter 1 takes 0.1 microseconds
4:30
I have set the auto reload value to 10
4:33
000 so the counter one will have a
4:35
period of one millisecond
4:37
this means the counter one can measure
4:39
the signal whose period is between 0.1
4:42
microseconds to 1 millisecond
4:45
of course 0.1 microsecond is an ideal
4:48
case and it's not possible for this
4:51
microcontroller to measure it
4:53
now for the signals with a period more
4:55
than one millisecond we will Cascade
4:57
another timer with this one
5:00
I am configuring the timer 3 for this
5:03
timer 3 will be configured in the slave
5:06
mode and that 2 in the external clock
5:08
mode 1. the trigger Source will be from
5:12
the timer one so we will set it to ITR
5:14
0. watch the previous video if you don't
5:20
now the timer 1 output frequency will be
5:22
equal to APB time a clock divided by the
5:25
prescaler times the auto reload value
5:28
this will be equal to one kilohertz
5:32
timer 3 will be clocked by this
5:34
frequency and if we keep the prescaler
5:36
at 1 the counter 3 will be running at
5:41
this means each count in the counter 3
5:43
will take one millisecond
5:45
if we set the auto reload to maximum 65
5:50
535 the counter 3 will have a period of
5:55
basically the count of three can measure
5:57
frequencies between one kilohertz and
6:02
now think of these two counters as the
6:04
single counter and we have ourselves the
6:07
frequency bandwidth of 10 megahertz to
6:12
this is a huge bandwidth considering we
6:15
were only able to achieve up to 150
6:17
Hertz with the single counter
6:20
we will use the input capture mode with
6:22
the interrupt so enable the respective
6:24
interrupt for the timer 1.
6:27
that's it for the cube MX configuration
6:29
click save to generate the project
6:33
inside the main function we will start
6:35
the timer 1 channel 1 in the input
6:38
capture interrupt mode
6:40
also we will simply start the timer 3 so
6:44
that its counter can also count
6:46
when the rising Edge will be captured by
6:48
the timer 1 an interrupt will be
6:50
triggered and the input capture complete
6:53
callback will be called
6:55
we will write the rest of the code
6:57
inside this callback function
7:04
here first of all I am defining some
7:06
variables which we will need later
7:10
counter 1 and 3 will hold the values of
7:13
the respective counters
7:15
his first captured will be used to reset
7:23
now first we will check if the Callback
7:25
is called by the timer 1 channel 1.
7:29
then we will check if the first Edge is
7:33
if this is the first Edge then reset the
7:36
counters and set the first capture to 1
7:38
indicating the first Edge has been
7:42
now when the interrupt is triggered by
7:44
the second Edge we will first read the
7:48
and now calculate the frequency
7:52
to calculate the frequency we will
7:54
convert the counter 3's value in terms
7:57
of the counter one's value
7:59
basically every tick encounter 3 is
8:01
equivalent to having 10 000 ticks in
8:03
counter one so to make them equal we
8:06
will multiply counter 3 with 10 000.
8:09
since the overall counter value is now
8:12
in terms of the counter 1 to calculate
8:14
the frequency we will simply divide the
8:17
counter one's clock by the counter value
8:20
after the frequency has been calculated
8:22
we will reset the first captured so that
8:25
the whole process can start from the
8:28
let me revise this one more time
8:31
when the first Edge arrives we will
8:36
now when the second Edge arrives we will
8:39
read the counter values
8:41
then convert all the counter values in
8:43
terms of the counter one and finally use
8:46
the counter one clock to calculate the
8:50
if you are confused about this part you
8:52
can think of this as the simple formula
8:55
counter is equal to counter one plus
8:58
counter two times the counter one's
9:00
reload Value Plus counter three times
9:02
the counter two's reload value times the
9:05
counter one's reload value and so on
9:08
of course this is considering the three
9:10
timers are in Cascade mode with timer
9:13
one clocking the timer 2 which is
9:15
clocking the timer three
9:17
this is it for the coding part let's
9:19
build and debug the code now
9:22
I am using the any triple five module
9:25
which basically outputs square waves of
9:28
different frequencies
9:30
I have connected its output to the logic
9:32
analyzer and also to the input capture
9:39
let's add the counters and frequency to
9:49
for some reason it's not working
9:52
all right I have found the issue
9:55
here instead of timer Channel One it
9:58
should have been how Tim active Channel
10:01
let's build and debug the code again
10:07
now it's working all right
10:10
you can see the frequency is two hundred
10:14
if we check the logic analyzer it also
10:17
gives similar values
10:19
now I am going to reduce the frequency
10:31
you can see the frequency is decreasing
10:33
and now it's around 97 kilohertz
10:37
let's check the analyzer
10:40
here we have a similar frequency
10:46
now the reading is around 13.9 kilohertz
10:49
and you can see the similar reading in
11:01
let's reduce it further
11:03
notice that so far the counter 3 is
11:06
always zero this is as we discussed the
11:10
counter one is capable of reading up to
11:12
one kilohertz and if the signal period
11:14
is less than one millisecond the counter
11:19
so as long as our frequency is higher
11:21
than one kilohertz the counter three
11:25
but if we go lower than one kilohertz
11:28
now the counter three also starts
11:30
contributing to the frequency
11:37
right now the frequency is around 558
11:46
and this is exactly what the analyzer is
11:50
let's keep going lower
11:53
now the frequency is around 17.5 Hertz
11:57
as we discussed in the beginning this is
12:00
way lower than the limit we set for a
12:02
single 16-bit counter
12:13
this 0.57 Hertz is the minimum frequency
12:17
achievable using this module I have
12:20
and if we measure it in the analyzer it
12:25
I cannot show lower values than this but
12:28
with my current setup I can go as low as
12:33
so this is how cascading the timers can
12:36
be used to increase the counter size to
12:38
whatever resolution you want
12:41
this wasn't exactly a 48-bit counter but
12:44
if you do use that you can achieve an
12:46
extremely high frequency bandwidth
12:49
as I mentioned earlier to calculate the
12:52
frequency while using the 48-bit counter
12:54
you can use the counter one clock divide
12:57
by the counter one plus the counter 2
13:01
535 plus the counter three times the 65
13:08
basically we have to convert every other
13:10
counter's value in terms of the counter
13:13
one value and then divide the counter
13:15
one clock by the overall counter value
13:18
so I hope you understood the concept the
13:21
need and the process of implementing the
13:25
we will continue with the timer series
13:27
and more videos will be coming out soon
13:30
which will cover some other timer modes
13:33
this is it for today
13:36
the link to download the code is in the
13:40
leave comments in case of any doubt
13:43
keep watching and have a nice day ahead