0:11
welcome to controllers tech this video
0:14
series will be dedicated to the timers
0:18
we will cover all different kind of
0:20
functions that the timers can be used
0:24
starting today we will see how to use
0:26
the pulse width modulation
0:29
this video will cover the pwm output how
0:33
signal how to change the duty cycle or
0:37
and how to use dma with pwm
0:41
so let's start by creating the project
0:52
give some name to the project and click
1:00
first thing we are going to do is set
1:03
this part seems to be confusing for few
1:06
people so pay attention
1:08
i am selecting the hse high speed
1:11
external crystal to provide the clock
1:15
don't randomly use the values i use in
1:19
you need to know the crystal frequency
1:23
for example you can see in this nucleo
1:26
here is the crystal with eight megahertz
1:31
in some nuclear boards this one is not
1:34
but there is another one shared between
1:38
and the mcu you can use this one
1:41
also here is another board
1:45
which is comparatively newer and this
1:47
one have the 24 megahertz crystal on
1:51
there is another one right beside it but
1:55
and it's used for the real-time clock
1:59
so hse in this particular board
2:06
input the proper crystal value here
2:09
now choose the pll source as hse
2:13
choose the pclk and type in the
2:19
hit enter and the clocks will be
2:21
configured automatically
2:23
that's it about the clock configuration
2:27
now let's do the rest of the setup
2:30
select the serial wire debug
2:33
i am going to use the timer one for the
2:37
there are four channels here and since i
2:41
i am choosing only channel one you can
2:45
pa8 got selected as the timer one
2:50
this will be our pin for the pwm output
2:54
select the clock source as the internal
2:58
now we need to configure the timer
3:09
here are three formulas that you are
3:11
going to need for the pwm
3:14
first we need to set the proper timer
3:16
clock and that can be done
3:18
with prescaler then the frequency can be
3:21
set using the timer clock
3:23
and the auto reload register the width
3:26
of the signal can be varied using the
3:28
capture compare register
3:30
and the auto reload register
3:33
let's start with the timer clock and the
3:38
first of all we need to know where the
3:40
respective timer is connected to
3:43
this information is provided in the data
3:48
here is the clock diagram for f103
3:51
and it describes the connection between
3:53
the peripherals and the buses
3:56
here we are looking for the timer 1 and
3:58
you can see that it is connected
4:05
now we will check the clock setup we did
4:08
and here you can see the ap b2 timer
4:10
clock is running at 72 megahertz
4:15
since we are using timer we are
4:17
concerned about the timer clock
4:19
not the peripheral clock since the timer
4:22
1 is connected to apb2
4:24
the timer 1 clock is also 72 megahertz
4:29
so we need to bring this clock down to
4:33
and that's where the prescaler comes in
4:36
timer clock is equal to abp timer clock
4:40
divided by the prescaler
4:42
apd timer clock is at 72 megahertz
4:46
so if we use the prescaler of 72 the
4:49
timer clock would come down to 1
4:52
the prescaler value must be 1 less than
4:56
value this is explained in the prescaler
5:00
as you can see this one here gets added
5:03
with the value we input
5:05
so make sure the value should be 1 less
5:07
than the desired value
5:09
so if you keep the prescaler 0 it would
5:12
mean the actual prescaler is 1.
5:15
now our timer clock is at 1 megahertz
5:19
remember that frequency is equal to
5:21
timer clock divided by auto reload value
5:24
so if i choose the auto reload value of
5:28
the frequency will be 10 000 hertz
5:31
the value for the arr should also be one
5:34
less than the actual value
5:36
these registers are set up in this way
5:39
and that's how we should use them
5:41
so our timer clock is running at 1
5:43
megahertz and using the auto reload of
5:46
100 will bring the frequency to 10
5:55
that's all we need to set up here and
5:57
the rest we will take
5:58
care in the code let's enable the dma
6:04
here we are going to send the data to
6:08
so the direction will be memory to
6:11
keep the dma mode normal so that we can
6:14
control when to send the data
6:16
and the data width must be half word as
6:19
we will be sending 16 bit values
6:22
that's all the setup we need let's click
6:25
save to generate the project
6:28
we will start with the simple pwm signal
6:32
load the value in capture compare
6:36
i am using ccr1 because i have selected
6:41
if you are using any other channel use
6:43
the respective ccr register
6:46
i will explain this 50 in a while
6:49
and start the timer in pwm mode
6:54
let's build and run this for now
6:59
all right let's see the output
7:08
you can see the frequency is 10
7:10
kilohertz and the duty is 50 percent
7:15
let's see some calculations
7:18
the frequency is equal to 10 kilohertz
7:22
and we know that the duty is equal to
7:24
ccr divided by the auto reload value
7:28
i have used the ccr of 50 and the auto
7:33
so the duty percentage is 50
7:36
let me explain you how this works the
7:39
timer clock is set at 1 megahertz
7:42
so each count in the counter will take 1
7:46
the auto reload is set at 100 so the
7:49
total counts the counter can count are
7:55
after 100 the counter will overflow and
7:58
start from zero again
8:00
also remember that we have set the
8:02
capture compare value to 50.
8:05
so the pulse will remain high until the
8:08
counter reach this value
8:09
50. once the counter hit this value
8:13
the pulse will go low for rest of the
8:16
so this make the pulse high for 50
8:19
and low for 50 microseconds
8:24
now let's change the ccr to 30.
8:42
the duty cycle is 30 percent now
8:46
also note that the pulse is high for 30
8:49
and the low for 70. this is because the
8:52
capture compare value
8:54
is set to 30. let's change the auto
9:05
also change the ccr to 4.
9:12
now our frequency is 1 megahertz by 10
9:16
that is 100 kilohertz and
9:19
duty cycle will be 40 percent
9:30
you see the frequency is around 100
9:39
so this is how we send the pwm signal
9:43
now we will use the dma with the pwm
9:47
let's define an array to hold 10 values
9:50
that we are going to send to pwm
9:54
now we will set the values the values we
9:58
are for the ccr register
10:07
these values are the duty cycles and
10:09
they will change with every pulse
10:12
start the timer in the pwm and dma
10:18
the data is the pwm data and the length
10:39
also i am changing this back to 100 as i
10:43
have used the ccr of 10 to 100
11:04
here we have the pulse with different
11:07
it starts with 10 percent duty and goes
11:12
duty cycle since we have used the normal
11:15
the dma will only run once and after the
11:18
transfer is complete
11:20
it will stop the dma can be used in the
11:24
where the pulse needs to be changed very
11:28
one of such examples are while using the
11:32
leds there is already a tutorial on it
11:36
you can find the link in the description
11:39
if you don't want to keep the pulse high
11:41
in the end you can send a zero in the
11:44
to indicate the end of transfer or
11:48
so we saw how to use pwm output how to
11:51
change the duty cycle and frequency
11:53
and how to use the dma with pwm
11:57
this is it for this video the next video
12:02
input where we can measure pretty high
12:06
later i will cover other functions of
12:09
that's it for today you can download the
12:12
code form the link in the description
12:16
keep watching be safe and have a nice