0:07
hello welcome to controllers tech few
0:12
months ago I made a video about using
0:15
internal RTC in stm32 although
0:19
everything was pretty ok with that
0:21
there was the problem of storing time
0:24
whenever the controller lost power the
0:27
RTC used to reset itself today in this
0:31
video we are going to interface ds3231
0:34
RTC module with SDM 32 as mentioned in
0:39
the datasheet you can use 2.3 to 5 point
0:42
5 volts to power the module we can also
0:51
use i2c in fast mode or the standard
0:54
mode to read or write data and then we
1:03
have these registers that we are going
1:05
to write to and read the time from so
1:09
let's start with the cube IDE
1:27
create a new project I am using stm32
1:34
f103 c8 controller give a name to the
1:42
project and click Next
1:52
just want to show you the firmware
1:54
version at the time of making this video
1:57
click finish and the cube MX will open
2:04
so first things first I am enabling the
2:08
clock from the external crystal next
2:12
enable the i2c and clock and data pins
2:15
will be selected this is how the
2:24
connection will be ds3231 is connected
2:29
to the controller via i2c one and PCF
2:33
eight five seven four is also connected
2:35
to the same i2c bus and then to the LCD
2:39
I want to run the clock at maximum so
2:43
just type in 72 and hit enter go to sis
2:51
debug and select serial wire click Save
2:56
and the project will be generated
3:07
here is the main C file first of all we
3:13
need to include the LCD library because
3:15
I want to display the time and date on
3:17
the LCD copy the C file in the source
3:21
and header file in the include folders
3:24
you can get these files after you
3:27
download the project from the link in
3:29
the description this address is defined
3:32
for PCF eight five seven four if you are
3:35
using any other I - C extender you have
3:52
let's include the header file in our
4:05
if you look at the datasheet of the
4:08
ds3231 you can find the i2c address this
4:13
is the address defined for the right
4:15
cycle in stm32 the address should be 8
4:19
bit long which includes 7 bits of the
4:23
address and 1 bit for the right this
4:26
makes up the address of the ds3231 as 0
4:30
cross d0 let's define that address these
4:42
are the functions to convert from BCD
4:44
format to decimal format and vice versa
4:47
the data written to the ds3231 must be
4:51
in the binary coded decimal format and
4:54
that's why we need these functions I am
5:01
defining a structure to save the time
5:03
and date that we are going to read from
5:17
now let's write a function to set the
5:20
time the parameters are seconds minutes
5:26
our day of week day of month month and
5:30
year I am defining an array to store
5:43
the data written to the module must be
5:46
in this order first time than day of
5:53
week and at last date
6:01
basically we have to write these seven
6:04
registers first we need to convert these
6:08
values to the BCD format using decimal
6:10
to BCD converter the array that I
6:14
created is going to store these BCD
6:16
values for the respective register now
6:22
we are going to write these values to
6:24
the registers we can do that by taking
6:30
advantage of the fact that the address
6:32
of the registers in the module will
6:33
automatically increment with every right
6:35
operation so we just have to write 7
6:39
bytes to this first register and the
6:42
first seven registers will be
6:44
automatically written I am using memory
6:47
write function of how to write the data
6:49
to the specific location in the memory
6:52
device address address of the register
6:55
size of addresses one byte the data that
6:58
we want to write size of data is 7 bytes
7:01
and time out next we need a function to
7:08
read the time from the RTC again
7:12
creating an array to store the BCD
7:14
values that we are going to receive from
7:17
the RTC I am using Hal i2c memory to
7:21
read the data from the memory device
7:27
address address of the memory
7:38
sighs of addresses one bite where we
7:41
want to store this data how many bytes
7:43
to read and time out address of the
7:46
registers Auto increments and we get the
7:49
reading from the first seven registers
7:52
next we need to convert these values to
7:55
the decimal format and then store in the
7:58
time structure I am creating this buffer
8:06
to store the equivalent characters so
8:09
that we can display the result on the
8:10
LCD in the main function initialize the
8:14
LCD then inside the while loop first we
8:19
have to read the time and then convert
8:21
the numbers to the characters using s
8:52
put the cursor at 0-0 and display the
8:56
time next convert the date in the
9:23
put the cursor in the next line and
9:32
I am giving 500 milliseconds delay
9:35
between each refresh we have to set the
9:38
time first I am setting it 20 seconds
9:42
later than the actual time let's build
9:48
this code there seems to be some problem
10:00
we have to include the stdio dot h
10:12
I have to change the time again let's
10:17
flash the code select stm32 application
10:40
run it for a little amount of time now
10:47
we need to again flash the code without
10:49
the time setting line so if we reset the
10:53
controller in future the time will not
10:55
set to the same value again now it's
11:07
time to see the actual working the time
11:11
and date is working as it should even if
11:14
I try to reset the controller the time
11:16
continues to work properly
11:28
so I have disconnected them from the
11:30
supply the time is stored in the RTC now
11:34
and we can access it anytime we want as
11:37
you can see it is around nine seconds
11:40
more than the last one
11:45
you need to have a battery if you want
11:48
to store the time so we saw how can we
11:52
set and read the time from the ds3231
11:56
next we are going to read the
11:59
temperature from this module yes it have
12:02
a temperature sensor also as mentioned
12:13
in the datasheet address is 11 H and 12
12:16
H stores the temperature 11 H stores the
12:20
MSB of the temperature data and 12 H
12:32
yet temp function is being used to read
12:36
the temperature basically I am going to
12:39
use how memory to read two bytes of data
12:52
then we need to combine the MSB and LSB
12:56
LSB must be shifted six places to the
12:59
right the result must be divided by four
13:03
because the resolution is 0.25 degrees
13:06
Celsius per bit there is one important
13:17
piece of information before we read the
13:19
temperature the bsy bit in the status
13:27
register this bit goes to 1 when the
13:31
conversion signal is given
13:47
other than this we have convert
13:49
temperature bit in the control register
13:53
setting this bit to one will force the
13:55
conversion if we don't force the
13:58
conversion the temperature conversion
14:00
would take place every 64 seconds by
14:27
this is the function to force the
14:30
conversion here first I am reading the
14:48
if the BSY bit is not set read the
15:17
and right the modified value with the
15:19
conversion bit as one
15:38
I am going to force the conversion read
15:42
the temperature and store the value in
15:45
temp variable we need to again use s
15:49
printf to convert this value to the
15:51
characters to convert the float type
15:59
numbers we need to change the setup a
16:11
go to project setting C++ build settings
16:23
and enable the you printf float feature
16:37
now send the buffer to the LCD let's run
16:50
the code now so here we have the time
16:57
date and temperature from the ds3231 RTC
17:01
module if I hold the device you can see
17:09
the rise in temperature
17:33
again if I disconnect the things would
17:37
still be working fine this is it guys
17:42
ds3231 still have few more features left
17:45
to be covered for example it have two
17:49
alarms oscillator output square wave
17:52
output etc I will cover those in the
17:56
next video real time was the important
17:59
part and so we have covered that today I
18:02
hope you understood the video you can
18:06
download the code from the link in the
18:08
description leave comments if you have
18:12
any doubt keep watching