0:09
hello and welcome to controllers Tech
0:12
this video is the part two of the series
0:14
covering how to interface the S segment
0:17
tm1637 display with SDM
0:20
32 in the previous video we saw how to
0:23
connect the display to the MCU how to
0:26
configure the cube MX and how to make
0:28
the display work by writing some basic
0:31
functions we will continue the same
0:34
project created in the previous video
0:36
but today we will add more functions to
0:39
library we will also use the SDM 32
0:42
inbuilt RTC and display the time on this
0:46
device here I am using the same project
0:49
that we created in the previous
0:52
video we already have a function to
0:54
display numbers on the device but this
0:57
function takes the segment data as the
1:01
today we will write another function
1:03
where we can pass the numbers to the
1:04
parameters instead of passing this
1:06
segment data let's define an array which
1:10
contains the segment data for the
1:12
equivalent number I have already
1:15
explained how this data is created in
1:19
video here I just have the data for the
1:22
numbers as that is only required for the
1:24
clock but you can also add more
1:26
characters to this array all you need to
1:29
do is write a one to the segment which
1:31
you want to turn on and you can pretty
1:34
much have any character you want all
1:37
right let's write a new function to
1:38
display the number on the
1:40
device the parameters will be the
1:43
address where you want to display the
1:45
number the number itself and the
1:48
colon this parameter colon will
1:50
basically decide whether you want to
1:52
display the colon or not let's define a
1:56
buffer to store four bytes of data here
2:00
number in the parameter can range
2:01
anywhere between one digit to four
2:04
digits so we need to First determine the
2:07
number of digits this parameter has if
2:11
we divide the number by 1,000 and we do
2:13
get a quotient that means the number has
2:17
total otherwise if we get a quotient on
2:20
dividing it by 100 that means it has
2:24
digits the similar checks will be
2:26
performed to determine the number of
2:28
digits in the parameter
2:30
number now we will extract the segment
2:33
data for the equivalent number and store
2:36
array note that here I am using the
2:39
modulo operator to extract the digit at
2:42
the unit place of the num
2:44
parameter basically if the number is
2:48
1,234 this operation will extract four
2:51
from it and then the data stored in the
2:53
segment map for this number will be
2:55
stored in the zeroth element of the
2:57
buffer array then update the a num
3:00
variable by dividing it by
3:02
10 this will eliminate the unit place
3:05
from the number so if we divide
3:09
1,234 by 10 the output will be
3:12
123 eliminating four from it this Loop
3:16
repeats as many times as there are a
3:18
number of digits in the num
3:21
variable now we have the segment data
3:23
stored in the buffer array so we will
3:27
display but before that we all also need
3:30
to take care of the colon if the colon
3:33
value is set to one we will enable the
3:35
colon by writing a one to the most
3:37
significant bit of the second digit that
3:40
is the value stored in the first element
3:43
array finally call the function WR data
3:46
to send the data to the given
3:49
address let's define this function in
3:52
tm1637 header file so that we can call
3:55
it in the main file all right let me
3:58
comment out the previous
4:00
code Now call the function right number
4:03
I am setting the address of the first
4:05
digit let's write the number
4:08
5,678 and I want to display the colon
4:12
also all right let's build and Flash the
4:17
board you can see the device is
4:20
displaying the number but it's not quite
4:23
wanted the number is in the
4:26
reverse well this is because as I
4:29
explained explained here we get the
4:31
digit at the unit place so in this case
4:33
we got the number eight this number
4:36
eight will be stored in the zeroth
4:38
element of the buffer
4:39
array when the data is sent to the
4:42
display the zeroth element is sent first
4:45
and hence the number eight is being
4:47
displayed on the address c0
4:49
hex to resolve this issue we can start
4:53
storing the data from the
4:55
end for example we will store the number
4:58
eight in the third element number seven
5:00
in the second element six in the first
5:03
element and five in the zeroth
5:05
element this length minus one is
5:08
important as the array positioning
5:10
starts from zero all right let's flash
5:14
this modified project to the board you
5:17
can see now we have the number
5:22
display let's say I do not want the
5:25
colon in the middle so set this colon
5:27
value to zero and Flash the codeing
5:30
again you can see the colon has
5:33
disappeared we can display any number on
5:35
the device be it a single digit or even
5:40
number let's display the number
5:43
95 you can see it being displayed on the
5:47
device note that the device retains the
5:50
data stored in other addresses as well
5:52
so those values will still remain there
5:56
one way you can handle it is by writing
6:00
digits you can also modify the function
6:03
to send the data bite with value zero to
6:05
turn off all the segments of a
6:07
particular digit we can also display the
6:11
number on a particular
6:12
digit here I want to display the number
6:17
digit you can see the number seven is
6:19
being displayed on the digit number four
6:23
so we are able to display the numbers
6:25
directly on the device without having to
6:27
worry about the segment data for the
6:30
same now we will display the clock on
6:33
device I am going to use the inbuilt RTC
6:36
of the SDM 32 controller to save and
6:39
fetch the clock data I have already
6:42
covered a tutorial explaining how to
6:44
make it work so I am not going to
6:47
explain the RTC part in much detail here
6:50
instead we will focus on fetching and
6:52
displaying the clock data let's enable
6:55
the lowspeed external Crystal first the
7:01
32.768 khz Crystal on it and we will use
7:05
it to provide the clock for the
7:07
RTC now go to the RTC and activate the
7:10
clock Source also activate the calendar
7:14
otherwise we won't be able to set the
7:16
time now go to clock configuration and
7:20
change the clock source to
7:22
LS if your controller board does not
7:25
have a dedicated Crystal for this you
7:27
can still use the internal oscillator of
7:30
khz let's configure the parameters Now
7:34
set the data format to Binary so that we
7:37
can directly deal in decimal values you
7:40
can leave this empty or set the current
7:43
time we are not interested in the
7:45
calendar here so leave it as it is that
7:48
is all the configuration we need click
7:51
save to generate the project we will
7:54
first write a separate function to set
7:56
the RTC time so scroll down to the RTC
8:00
initialization function and copy this
8:02
part where the time is being set also
8:05
comment it out as we do not want the
8:07
time to set every time the board
8:10
resets let's define a function to set
8:12
the time the parameters of this function
8:15
will be hours minutes and seconds also
8:20
copy the RTC time structure from the
8:22
initialization function instead of this
8:25
default time we will pass the parameters
8:28
here now we need to create one more
8:31
function to fetch the current time from
8:33
RTC the parameters of this function will
8:36
be pointed to the array where the
8:38
current time will be stored Define a RTC
8:42
time structure where the current time
8:44
will be stored Now call the function RTC
8:48
get time to fetch the current time and
8:50
store it in the structure we just
8:52
defined the data will be stored in the
8:55
binary format now we need to copy the
8:58
time data from the structure to our
9:00
array all three elements of the time
9:03
data have values in two-digit format so
9:07
we will store the unit Place digit first
9:10
and the 10 Place digit repeat the same
9:13
process for minutes and hours now we
9:16
have separated the time into six
9:18
different digits which is stored in an
9:21
array of six bytes there is one
9:24
important thing I forgot while setting
9:26
the time after the time is set we will
9:29
write a random value to the RTC backup
9:32
register this register is used to make
9:34
sure that the time does not reset when
9:37
the MCU resets for some reason you can
9:40
write any random value to this
9:45
register in the main function after
9:48
everything has been initialized we will
9:50
set the time but before setting the time
9:53
we need to make sure that the backup
9:55
register does not has the same value
9:57
which we stored into it if the backup
10:00
register has the same value that means
10:02
the time was already set and we will not
10:05
set it again the RTC backup register
10:08
does not lose its data when the board is
10:11
reset therefore it is used as a shield
10:13
to protect the execution of the same
10:15
statement again and again let's define
10:18
an array to store the six bytes of the
10:23
time inside the while loop we will call
10:26
the function get time to fetch the
10:27
current time from the RT TC and store
10:30
this time in the array we
10:32
defined now we need to send this data to
10:35
the display and to do that we will write
10:38
a new function in the
10:42
file the parameters of this function
10:44
will be the pointer to the array which
10:46
contain the times data and the colon
10:49
variable let's define a buffer to store
10:54
bytes now we need to store the segment
10:56
data with respect to the number we have
10:58
in the time array the segment data will
11:02
be stored in Reverse storing the seconds
11:04
in the last two digits and minutes or
11:07
hours in the first two the time array
11:10
contains the frequently changing data in
11:13
the beginning of the array for example
11:15
we have seconds first then minutes and
11:18
hours in the end the time array already
11:21
contains the individual numbers so we
11:24
don't need to extract them as we did in
11:27
function this much should be enough to
11:29
display the time but we also need to
11:32
deal with the colon part I want the
11:35
colon to Blink every second usually that
11:38
is how it is in most of the
11:40
clocks let's define static variables D
11:45
change the D colon variable will be used
11:48
to change the colon state every second
11:52
but we don't know how frequently this
11:55
called for example you can use this code
11:58
in free R toos and call this function
12:04
milliseconds in order to make sure that
12:06
a second has been passed we will compare
12:08
the value of the S change variable with
12:10
the first element of the time
12:13
array this element contains the unit
12:17
second if the value here is different
12:20
that means 1 second has been passed and
12:22
we can change the state of the Decon
12:25
variable but again this change in the
12:28
state of the decol depends on whether
12:30
the colon variable is sector one or not
12:34
now if the D colon is changed to true we
12:37
will update the first element of the
12:38
buffer by writing a one in the seventh
12:41
bit otherwise leave the data as it is
12:45
now update the S change variable with
12:48
the latest value of the unit position in
12:51
array also we don't want to send the
12:54
data continuously so we will call the
12:56
send function here itself
12:59
this will only send the data to the
13:01
device if there is some change in the
13:03
unit position in the time
13:05
array I am calling it unit position
13:07
because since we have only four digits
13:10
in the display we can either display
13:12
minutes with seconds or hours with
13:15
minutes in either case the unit position
13:18
contains the data which changes most
13:21
often all right let's define this
13:23
function in the header file so that we
13:25
can call in from the main file in inside
13:29
the while loop after fetching the time
13:32
pass the array to the right time
13:34
function note that the array current
13:36
time has six bytes data but the buffer
13:39
array defined inside the Righttime
13:41
function can only store four bytes so
13:44
the first four bytes which contains
13:47
seconds and minutes will be stored in
13:49
the buffer array there is some error
13:52
here and this is because we need to pass
13:54
the address of the RTC time
13:57
structure let's give a delay of 200
14:00
milliseconds in the while
14:04
loop also set the current time in the
14:08
function let's build and Flash the code
14:11
to the board you can see the minutes and
14:14
seconds are being displayed on the
14:16
device note the colon in the middle it
14:19
is blinking every second just as we
14:21
wanted it to I am fast forwarding the
14:25
video so that you can see the change in
14:27
the minutes at this point even if we
14:30
reset the board the time will not start
14:33
from the beginning it will continue to
14:35
run let me show this in the debugger you
14:39
can see even when the board is reset the
14:41
time does not reset it continues where
14:44
it was stopped it happened because we
14:47
have added a layer of protection on the
14:49
set time function now let's say I want
14:52
to display the hours and minutes instead
14:55
of minutes and seconds the minutes data
14:58
starts from the second element of the
15:00
time array so here instead of passing
15:04
the array from the beginning we will
15:05
pass it from the second
15:09
element here I do not want to Blink the
15:11
colon every minute so comment out this
15:14
line if you want to display the colon
15:17
change it to one or leave it zero
15:20
otherwise I want to display the colon so
15:24
I am setting it to one let's build and
15:26
un Flash the code again you can see the
15:29
current hours and minutes are being
15:31
displayed on the device so I hope you
15:34
understood how to display the clock on
15:38
1637 since the device has only four
15:40
digits we can either display the minutes
15:43
and seconds or hours and minutes the
15:46
latter seems more logical of course as I
15:49
mentioned earlier you can also modify
15:52
this segment data and add more
15:55
display this is it for the video
15:59
you can download the code from the link
16:02
description leave comments in case of
16:05
any doubt keep watching and have a nice