0:16
welcome to controller's tech this is an
0:19
update video for the last one
0:21
which covered the encoder stm32 using
0:27
it was working fine but as few users
0:30
pointed out that it would have been
0:31
better to use the timer feature
0:34
so this video covers the sdm32 timer in
0:37
encoder mode basically
0:39
and i will use the same encoder here
0:42
the first part of the video explains the
0:46
and it's the same as the other video if
0:49
you have already seen the previous video
0:51
just skip to four minutes so let's
0:55
start with the encoder first i have this
0:59
and it's rotary as you can rotate the
1:05
the shaft is free to rotate in either
1:08
as there is no limit on the rotation
1:14
it have five pins but we are interested
1:16
in the bottom two pins
1:19
the pin names do not justify their
1:22
so we will call the clock pin as pin a
1:26
pin as pin b the middle one is the
1:29
and it represents the push button on the
1:33
that's it about the encoder now let's
1:41
i got this gif from the wikipedia and it
1:44
shows exactly what happens
1:47
think of the black region as ground and
1:52
so it start with both the pins in
1:53
contact with the white region
1:55
so both the pins are high now let's
1:59
say we move the shaft clockwise
2:02
so the outer pin goes low while the
2:04
inner one is still high
2:06
now the inner pin goes low and both the
2:11
and now they are going back to high
2:19
if we move it counterclockwise the inner
2:24
then the outer one and then they both
2:28
this is the entire working basically
2:32
we just have to check which pin goes low
2:34
first and based on that we can figure
2:37
whether the shaft moved clockwise or
2:41
let's see one more time with the logic
2:46
here i have connected the channels to
3:03
pay attention i am rotating the shaft
3:08
here we got the signal on both pins
3:15
let's zoom in we have some unwanted
3:19
but we will take care of them in the
3:23
let's focus on the main part as you can
3:27
the first pin goes low and after some
3:30
the second one goes low
3:38
now i am rotating it in the other
3:44
this time the second one goes to low
3:46
first and after some time
3:48
the first one goes low
3:54
this is exactly what's shown in this
4:00
this is the entire working of this
4:02
encoder and we will use these pins to
4:05
identify the direction of rotation
4:11
now let's see the timer part here is the
4:14
reference manual for f103 controller
4:18
and i am choosing the general purpose
4:20
timers for this function
4:26
here we have the encoder mode
4:31
you can go through this explanation i'll
4:34
just get to the point
4:35
and that is this particular figure
4:42
here you can see we have signal on the
4:44
two different channels
4:46
as we have two outputs from the encoder
4:52
notice that if the t1 goes to high first
4:56
and then t2 the counter will start
5:02
as long as both the signals have rise
5:04
and fall the counter will keep updating
5:10
but in case of a jitter only one of the
5:14
and in that case the counter will not
5:19
and if the t2 goes to high before t1
5:22
the counter will start counting down
5:25
this will indicate that the movement is
5:27
in opposite direction
5:30
again in case of jitters the counter
5:32
will not update at all
5:34
this is pretty convenient considering
5:36
what we did in the last video
5:38
to get rid of these jitters
5:42
let's keep this in mind and create a new
5:48
i will be using the same f103 controller
5:53
enable the external crystal for the
5:56
i have eight megahertz crystal and i
5:58
want the system to run at maximum 72
6:05
i am using timer 2 for the encoder
6:08
here we will use the combined channels
6:11
encoder mode you see the pins pa00
6:21
keep in mind that do not use any
6:25
or else it won't work
6:30
let's keep the auto reload to maximum
6:40
encoder mode will be both t1 and t2
6:44
as we will be using two inputs let's
6:47
change this to falling edge polarity
6:50
as you saw in the first half that's how
6:52
this encoder sends the signal
6:55
i tested it with rising edge also and
6:58
somehow it was working all right
7:03
enable the timer interrupt as we will
7:06
perform this entire operation in the
7:08
interrupt service routine
7:12
that's it now click save to generate the
7:19
here i have the encoder connected with
7:27
and it is powered by 3.3 volts
7:35
let's start the encoder in the it mode
7:38
here we have the channel parameter let's
7:41
see what we need to input in this
7:54
as mentioned here for two channels we
8:06
now once the encoder sends the signals
8:09
the interrupt will be called
8:11
and we will use the input capture
8:15
the encode mode is the input capture in
8:22
here we will go one step at a time
8:25
let's start with reading the counter
8:34
some people have confusion with this h
8:38
actually since we are calling this
8:40
function inside the isr
8:42
so whatever time i have called isr the
8:44
same time a handler will be used here
8:48
that's why i prefer to keep it this way
8:51
let's test this much part first and see
8:54
what count of values do we get
8:59
i am rotating the encoder in clockwise
9:02
one click at a time the counter
9:10
second click and it is eight now
9:15
now there are totally five clicks and
9:17
the counter is twenty
9:19
this means the counter is increasing 4
9:22
counts per click on average
9:24
now i am rotating in counterclockwise
9:28
and again one click at a time you see
9:31
the counter is decreasing in
9:33
counterclockwise direction
9:35
just like it's mentioned in the manual
9:39
counts per click is quite persistent
9:42
i can't say for sure but maybe that's
9:44
why it's shown four counts here also
9:47
anyway if you try this and find that the
9:50
counter is not increasing by four
9:52
counts let me know in the comments
9:55
i tried with other mcus that i have and
10:00
counts this is fine but there is one
10:04
if the counter goes below zero it will
10:07
and start from the auto reload value
10:11
we need to tackle this situation and to
10:14
i am defining a sign 16-bit integer
10:28
now we will typecast the counter and
10:30
store the value in this variable
10:39
let's check the result
10:52
now the things are working fine you can
10:54
have the negative values if you want
11:00
still we are getting 4 counts per click
11:03
and i would prefer the number of clicks
11:13
let's create another variable position
11:15
and this will just be count divide by
11:18
four this way for every four counts
11:21
the position variable will be updated
11:38
notice the movement is clockwise and the
11:41
increasing also the position is around
11:45
one-fourth of the count value
11:48
and now a counter-clockwise movement
11:50
will decrease the position
11:55
this is very easy to deal with and the
11:57
results are very accurate too
12:08
i just want to add one more thing here
12:11
and that's the speed
12:13
i haven't tested this part properly so i
12:16
can't say that it will work for sure
12:19
but it seems pretty okay to me and you
12:21
can see how i am approaching it
12:24
in this case we will do the calculations
12:35
the position is defined in the main file
12:38
bold position is of course the old
12:41
speed is also defined in the main file
12:44
and indexes to keep track of
12:49
the systick handler is called every one
12:53
and inside it we will update the index
12:56
once the 500 milliseconds have passed we
12:59
will calculate the speed
13:02
you can reduce this time if you want
13:05
updates on speed speed is basically be
13:09
how much difference is there in the old
13:16
i am multiplying by 2 because this code
13:19
will be executed every 500 milliseconds
13:22
and therefore it will give speed every
13:27
but we want it every second so the speed
13:31
this will give us the speed in clicks
13:33
like how many clicks per second
13:36
we will then update the old position and
13:41
you can convert these clicks to rpm or
13:44
based on how your encoder works
13:48
for me i have found out that it takes 20
13:50
clicks per revolution
13:59
this should be 16-bit integer here
14:02
that's how it's defined in the main file
14:12
let's see the working now
14:22
let's start it slow the speed is around
14:25
20 clicks per second and now i am going
14:30
and the speed is around 70 clicks per
14:33
we have speed in the negative direction
14:38
i would say this is pretty good like i
14:40
said one revolution takes around 20
14:43
and i think i am making around three and
14:45
a half revolutions per second
14:50
as i mentioned earlier i am not sure if
14:54
so if someone can test and report or if
14:57
anyone is willing to work on it
14:59
feel free to contact me this is it for
15:04
i hope explanation was okay and you
15:06
understood the timer in encoder mode
15:09
you can download the code from the link
15:13
leave comments in case of any doubt
15:16
keep watching and have a nice day ahead