0:10
everyone welcome to controllers tech
0:14
this is second video in the stm 32 timer
0:18
and today we will see how to use the pwm
0:22
input mode i covered the pwm
0:25
output in my previous video about the
0:27
timers and that's why i decided to go
0:29
with the pwm input today
0:32
we will see how to measure the input
0:34
frequency and the duty cycle
0:36
using the pwm input mode
0:40
so let's start by creating the project
0:44
i am using stm32f446re
0:49
give some name to the project and click
0:53
so first of all i am selecting the
0:55
external crystal for the clock
0:58
let's see the clock configuration
1:01
type in the crystal frequency of your
1:05
choose this as per your board don't just
1:09
my board have eight megahertz crystal
1:12
choose the hse for external crystal
1:16
choose pclk for pll clock
1:20
type the frequency you want to run the
1:23
and hit enter that's all about the clock
1:28
now let's configure the timer i am
1:31
choosing timer 1 to provide the pwm
1:34
signal pwm output has already been
1:38
and you can watch the video on the top
1:42
timer 1 is connected to a pb 2 clock
1:45
which is running at 180 megahertz
1:49
i am choosing the auto reload period of
1:52
so the output frequency will be 100
1:59
this is it for the timer 1.
2:03
now timer 2 is going to be used for the
2:11
choose the clock source as internal
2:13
clock and choose the combined channels
2:17
input timer 2 channel 1 will be our main
2:21
where we will provide the input clock
2:24
you can see here the pin p-a-0 got
2:28
now comes the parameters
2:33
here i am keeping the prescaler zero so
2:36
the timer clock will be same as a-p-b-1
2:39
and that is 90 megahertz
2:43
the auto reload is set to maximum value
2:47
this is a 32-bit register and that's why
2:50
this value is very high
2:52
if you have 16-bit register this will be
2:58
so leave it to default next is the
3:01
internal clock division to understand
3:05
we need to check the reference manual
3:19
here in the control register 1 we have
3:24
this clock division basically sets up
3:26
the dead time and sampling clock
3:29
the dts clock decides how fast we want
3:32
to sample the input signal
3:34
here are the settings for the dts clock
3:38
i am keeping it to no division and that
3:40
means the dts clock will be same as the
3:52
we can skip the rest and come to the
3:55
channel configuration
3:58
here the input trigger is t i 1 f p 1
4:01
which means that the input from channel
4:03
1 after the filter and polarity
4:06
will be connected to the capture 1. next
4:09
we have is the parameters for channel 1.
4:13
the polarity is set to rising edge which
4:15
means that this channel is going to
4:17
measure the rising edges of the signal
4:20
i-c selection is direct and we will
4:23
connect the input signal directly to
4:27
this channel is our main channel and it
4:29
will measure the frequency
4:32
now for the prescaler division ratio
4:35
let's see the reference manual again
4:40
here we are interested in the capture
4:46
we have the input capture prescaler and
4:50
it basically controls the capture
4:55
these bits controls how often we want to
4:59
let me explain this in detail
5:02
let's assume that this is the input
5:04
clock and we want to capture the rising
5:09
a rising edge would be counted as an
5:13
now suppose we use the prescaler
5:15
division 8 that would mean that
5:16
interrupt will trigger after the 8
5:19
and that means here this will keep
5:22
every eight events remember that
5:26
if we do the captures at very high
5:27
frequency then the interrupts will
5:31
and this will leave the rest of the code
5:35
so this is an important parameters
5:38
i am going with the highest possible
5:42
the capture will be done once every
5:46
but the issue is that the cube m-x is
5:48
not letting me choose this one
5:51
don't worry about this we can set it in
5:56
next comes the filter i am not going to
6:00
but let me explain it anyway here we
6:03
have the input capture filter
6:06
the filter configures the frequency at
6:08
which the input signal will be sampled
6:12
it is also used as low pass filter but i
6:14
couldn't find more information on the
6:18
for now we will keep the filter zero and
6:20
that means the sampling frequency will
6:23
s and that is the internal clock
6:26
i will update you if i find more
6:28
information on how to use these filters
6:32
now comes the parameters for channel 2.
6:35
the polarity should be opposite to the
6:37
first channel and that's why it's
6:40
edge the ic selection is indirect
6:43
and this means we don't need to give the
6:45
input to this channel
6:48
it's internally connected to the channel
6:50
1 and it is used to calculate the duty
6:54
all right let's enable the interrupt for
7:02
this is it for the setup click save to
7:05
generate the code now
7:12
let's create few variables where we can
7:30
now in the main function start the timer
7:33
in input capture mode for channel 1.
7:40
channel 2 can be used in the normal mode
7:42
since we only need the interrupt from
7:52
after the input capture has been started
7:54
we will start the pwm
8:02
and finally set the value of capture
8:04
compare register for the duty cycle
8:07
everything related to pwm output has
8:10
been covered in the previous video
8:13
so if you don't understand pwm output
8:16
please watch the video on the top right
8:21
once the input capture actually does the
8:24
an interrupt will be triggered and this
8:26
input capture callback will be called
8:33
now we will write the rest of the code
8:35
inside this callback function
8:40
here we will check if the interrupt was
8:42
triggered by the channel 1
8:44
that is due to the rising edge of the
8:51
if it is then we will read the input
8:53
capture value for the channel 1.
9:07
then we will read the capture value for
9:10
and use it to calculate the duty cycle
9:17
i will explain this in a minute
9:22
next calculate the frequency
9:25
this 90 megahertz is my timer 2 clock
9:29
now let's understand this calculation
9:32
let's see the pwm input mode in the
9:37
well we are mainly interested in this
9:40
here you can see when the first rising
9:44
the counter is reset and so do the
9:48
when the second rising edge gets
9:50
captures the capture value read
9:53
this value is actually this time
9:55
difference between the first
9:57
and second edges so the period of the
10:02
in the callback we also read the capture
10:04
value for the falling edge
10:07
using that value we can determine the
10:11
as a percentage of the total time
10:15
this will be the duty cycle for the
10:21
similarly using the clock frequency we
10:24
can determine the frequency of the input
10:33
i am just giving this delay in the while
10:35
loop to test if the control enters the
10:41
all right let's test this
10:50
i have added all three variables to the
10:59
ok you can see the frequency is around
11:03
and the duty is around 50 percent also
11:06
note that the input capture value is
11:10
let's put a break point in the while
11:12
loop to check if the control
11:14
enters the loop so the loop is also
11:22
since it's working fine for now let's
11:24
test the higher frequencies
11:34
now the auto reload is 450 making the
11:38
output frequency 400 kilohertz
11:41
and let's change the capture compare to
11:45
making the duty cycle 50.
11:56
and there we have it around 400
12:01
and 50 duty cycle but
12:04
the while loop isn't running anymore
12:07
this is because the interrupts are
12:09
getting triggered at very high rate
12:11
making the rest of the code impossible
12:20
this is where the input capture
12:24
right now it's set to division 1 but we
12:27
will change it to highest
12:29
and this will make the capture to be
12:31
performed every 8 events
12:53
and now you can see the while loop is
12:58
let's see how high air can we measure
13:12
this time the auto reload is 180
13:15
making the frequency equal to 1
13:19
let's change the capture compare to 60
13:21
so the duty cycle will be 33 percent
13:30
i would say the result is approximately
13:33
the frequency is around 1 megahertz and
13:39
the while loop is still running so we
13:41
can test even higher range
13:47
now the frequency will be 2 megahertz
13:49
and the capture compare is still
13:51
60 making the duty cycle equal to 66
14:02
this is still working all right
14:06
the while loop is still running honestly
14:09
i wasn't expecting it
14:10
but let's go even higher
14:17
now the auto reload is 60 making the
14:20
frequency equal to 3 megahertz
14:22
and i am keeping the auto reload at 30
14:25
so the duty would be 50 percent
14:32
here we do have approximate to what we
14:34
need but the while loop is not running
14:38
i guess this is the limit for the while
14:40
loop with the current setup
14:44
even though it was able to measure the 2
14:47
i would suggest that you keep the
14:48
measurement below 1 megahertz
14:52
for those frequencies the rest of the
14:55
pretty well too if you want to measure
14:59
well then you won't be able to run the
15:03
you can play with some other settings to
15:05
improve the accuracy of the frequency
15:09
before finishing this video i want to
15:12
share one more thing
15:14
it's quite possible to measure the high
15:18
in fact i did some tests and you can see
15:21
the result in the picture
15:23
the accuracy even at 10 megahertz
15:26
frequency is quite phenomenal
15:29
this is a different method by the way i
15:32
was able to measure up to 18 megahertz
15:36
and even then the while loop was still
15:40
i was going to make the video on that
15:42
method but as i was about to start
15:44
i saw the cube mx has deprecated some
15:47
settings that are needed for it
15:49
anyway i will post the video on that
15:53
and if the cube mx allows you to do the
15:56
you can use it this is it for the
16:01
i hope you understood the process and
16:05
you can download the code from the link
16:10
leave comments in case of any doubt
16:13
keep watching and have a nice day ahead