0:10
hello and welcome to controllers tech
0:13
this is fourth video in the timer series
0:16
and today we will see how to use the
0:20
i have already covered few other timer
0:22
videos and before watching this video i
0:25
would suggest that you go through at
0:26
least the pwm output and input videos
0:30
you can check the first two videos in
0:35
so let's start with this video and we
0:38
will create a new project in cube id
0:45
stm32f446re controller
0:47
give some name to the project and click
0:55
the first thing we will do is select the
0:57
external high speed crystal for the
1:03
i have eight megahertz crystal on the
1:05
board and i want the controller to run
1:07
at maximum 180 megahertz frequency
1:13
make sure you input the correct crystal
1:15
frequency as per your board
1:24
now i am going to use the timer 1 for
1:26
the pwm output which i will then measure
1:29
with the timer 2 using the input capture
1:35
enable the pwm generation with timer 1.
1:41
if we check the data sheet of the
1:43
controller here it's shown that the
1:45
timer 1 is connected to the a-p-b-2
1:52
as per our clock setup the apb2 timer
1:56
clock is running at 180 megahertz
2:13
also the timer 2 is connected to the
2:15
a-p-b-1 clock which is running at the 90
2:20
just keep this in mind while we set up
2:22
the parameters for the timers
2:25
let's start with the timer 1.
2:28
right now the timer clock is running at
2:30
180 megahertz and if we use the
2:33
prescaler of 180 this clock will come
2:39
i am further using the auto reload of
2:41
100 which would bring the pwm frequency
2:47
this is already explained in the pwm
2:49
output video and if you don't understand
2:52
it just watch that video in the timer
2:56
that's it for the pwm output now we will
2:59
configure the timer to
3:01
select the clock source as internal
3:05
enable the input capture direct mode
3:09
if you remember the apb1 clock is
3:11
running at 90 megahertz so using a
3:14
prescaler of 90 will bring it down to 1
3:19
this will be our timer clock frequency
3:22
i am leaving everything else to default
3:25
if you want to know more about these
3:27
parameters watch the pwm input video i
3:32
have explained everything in that one
3:35
note here that the polarity selection is
3:37
set to rising edge by default
3:40
also i am not using any prescaler
3:47
now the last thing we need to do is
3:49
enable the timer to interrupt
3:55
this is it for the setup
3:57
click save to generate the project
4:03
here is our main file
4:07
timer 1 will be responsible for
4:09
producing the pwm signal and we have the
4:12
auto reload period of 100 here
4:17
i am using the capture compare value of
4:19
50 so the duty will be 50
4:23
let's start the timer 1 in pwm mode
4:30
now we will start the input capture in
4:32
the interrupt mode for timer 2.
4:46
once the rising edge is detected the
4:48
interrupt will be triggered and the
4:50
input capture callback will be called
5:19
let's define few variables that we are
5:21
going to use in this tutorial
5:27
first i am going to measure the
5:40
this is the code for the same
5:46
first we will check if the interrupt is
5:49
triggered by the channel 1.
5:54
if the first reading hasn't been
5:56
captured we will capture the first value
5:59
this is basically the counter reading
6:01
for the first rising edge
6:05
now we will set the first captured to 1.
6:08
when the second rising edge comes the
6:10
interrupt will be triggered and this
6:12
time it will capture the reading for the
6:18
now we will calculate the difference
6:20
between the two readings
6:25
if there is overflow we will get the
6:29
the timer 2 in my case is 32-bit timer
6:32
and that's why i am using 32-bit value
6:38
next step is to calculate the reference
6:42
this is something you need to define
6:45
timer clock is the frequency of the
6:47
timer 2 which in my case is 90 megahertz
6:51
we also need to define the prescaler
6:55
i have set it to 90 during the setup
6:59
you can check it here
7:07
so it will calculate the reference clock
7:09
based on the parameters
7:11
and finally the frequency will be
7:13
reference clock divided by the
7:20
in the end we will reset the counter
7:23
let's build it once to check for any
7:32
all right we are good to go
7:43
the timer 1 is producing the pwm signal
7:46
at a frequency of 10 kilohertz
7:48
and here we got the result of 10
7:50
kilohertz from the input capture
7:55
the while loop also runs pretty well
7:58
i am going to add some delay here to
8:01
test the while loop a little better
8:15
let's modify the timer 1 prescaler and
8:18
now the output frequency will be 100
8:24
let's see if the input capture is able
8:34
all right here you can see the 100
8:40
and the while loop is still running
8:49
all right we will go a little more
8:51
higher and this time the output
8:53
frequency will be 200 kilohertz
9:06
you can see it's able to measure it
9:12
our while loop is still operational
9:17
fine we will increase it a bit more
9:22
now the output frequency is 300
9:38
now it have started showing errors
9:41
so this particular method is good for
9:43
measuring frequencies up to few hundred
9:47
if you want to measure higher
9:49
frequencies i will release another video
9:56
so we were able to measure the input
9:58
frequency using the rising edge triggers
10:01
now we will see how to measure the width
10:09
i will comment out this frequency code
10:12
and we will write another one for
10:15
most of it will be similar to the
10:17
frequency 1 so i am just using the same
10:20
code and i will edit it in a while
10:35
to measure the width of the signal we
10:37
have to measure the time between the
10:39
rising edge and the falling edge
10:42
so here we want the interrupt to trigger
10:44
in both cases to do so we will change
10:47
the polarity to both edges
11:02
when the rising edge will trigger the
11:04
interrupt the counter value will be
11:06
stored in the i c value 1.
11:09
and similarly i c value 2 will store the
11:11
counter value for the falling edge
11:14
then we will measure the time difference
11:16
between the two values
11:18
this difference in time will depend on
11:21
the input capture timer configuration
11:24
this is why we need to add a little code
11:26
here to change it to microseconds
11:28
irrespective of the timer configuration
11:32
now this microsecond width will always
11:34
show the width of the signal in
11:53
if you notice here our pwm timer is
11:56
running at 1 megahertz clock
11:59
this means each count of the counter
12:01
will take 1 microsecond
12:03
if we are using the capture compare
12:05
value of 20 the signal should be high
12:25
here you can see we got the 20
12:27
microseconds width for it
12:42
let's modify the timer prescaler and now
12:45
it's running at 10 megahertz clock
12:47
now each count in the counter takes 0.1
12:50
micro microsecond and we still have the
12:52
capture compare value of 20.
12:55
so the signal high time should be 2
13:01
and you can see it here that's exactly
13:04
what we got so the input capture works
13:07
for measuring the frequency and the
13:13
you should use it to measure lower
13:15
frequencies only up to few hundred
13:19
if you want to measure very high
13:21
frequencies i will post another video
13:27
if you want to measure both frequency
13:29
and width together watch the previous
13:32
videos in the timer playlist the one
13:39
you can download the code from the link
13:43
leave comments in case of any doubt
13:46
keep watching and have a nice day ahead