Skip to content

Commit befe989

Browse files
committed
DS1302 Driver development part 1
1 parent c97e7a1 commit befe989

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

blog/content/ds1302_p1.md

+241
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
+++
2+
title = "DS1302 RTC Drivers for STM32 Part 1"
3+
date = 2024-08-24
4+
5+
[taxonomies]
6+
tags = ["platformio", "stm32", "stm32-pio-libs"]
7+
+++
8+
9+
# Introduction
10+
The DS1302 is a Real-Time Clock (RTC) IC used to keep track of time and date. It has the capability to store data such as seconds, minutes, hours, day, date, month, and year. The DS1302 can be interfaced with microcontrollers using a simple serial communication protocol (SPI-like but with slight differences). It also includes 31 bytes of static RAM for temporary data storage.
11+
12+
{{ image(src="/img/ds1302_module.png", position="center", style="width:50%;") }}
13+
14+
<br>
15+
<br>
16+
17+
# Interface with module
18+
### Pinout
19+
{{ image(src="/img/ds1302_pinout.png", position="center", style="width:70%;") }}
20+
21+
Above is the pinout of DS1302. This is description of each pin.
22+
+ **V<sub>cc2</sub>**: This is the main power supply which you connect to any 3.3v or 5v power source.
23+
+ **V<sub>cc1</sub>**: Connect a flat lithium sound button cell which powers the low power operation i.e. keeping the clock ticking.
24+
+ **X1/X2**: Connect any 32.768kHz Quartz Crystal.
25+
+ **GND**: Just a ground
26+
+ **CE**: When Chip Enable pin is set high, it initiates the data transfer. It is kept high till the data transfer is being done.
27+
+ **I/O**: This pin is used to send and receive data.
28+
+ **SCLK**: The microcontroller generates clock on this line, this clock is used to synchronise the MCU and DS1302.
29+
30+
## Communication
31+
Communication happens in following step.
32+
1. Set the `CE` pin high to initiate data transfer
33+
2. Start to produce clock signal on `SCLK` pin.
34+
3. First MCU writes the first byte which is the address it want to communicate.
35+
+ If MCU wants to write, send the next byte which is the data.
36+
+ If MCU wants to read, set its pin in input mode and read the coming data from DS1302.
37+
38+
{{ image(src="/img/ds1302_communication.png", position="center", style="width:100%;") }}
39+
40+
## Addressing
41+
{{ image(src="/img/ds1302_addressmap.png", position="center", style="width:100%;") }}
42+
43+
The address space in ds1302 is divided into two parts.
44+
+ **Clock/Calendar**: If the `bit 6` is set to low, it allows you to access Clock/Calendar space. This is where datetime information is stored.
45+
+ **RAM**: If the `bit 6` is set to high, it allows you to access the RAM space. DS1302 provides your 31 bytes of storage where you can write or ready any personal data. It is a non-volatile storage space useful to store some configuration data.
46+
47+
The `bit 7` is always set to high, The `bit 0` lets your specify read or write operation, write operation when set to low and read operation when set to high. `Bit 1-5` used to specify address from 0-31.
48+
49+
### Clock/Calendar address
50+
There are only 7 bytes useful to read or write in Clock/Calendar address space.
51+
52+
0. Byte -> Second
53+
1. Byte -> Minutes
54+
2. Byte -> Hour
55+
3. Byte -> Date
56+
4. Byte -> Month
57+
5. Byte -> Day
58+
6. Byte -> Year
59+
60+
Lets try to calculate some address ourself:
61+
+ How to read Month data
62+
```
63+
Month address -> 4 -> 0b00100
64+
65+
B7 C/R A4 A3 A2 A1 A0 R/W
66+
Address Byte: 1 0 0 0 1 0 0 1
67+
```
68+
69+
+ How to write year data
70+
```
71+
Year address -> 6 -> 0b00110
72+
73+
B7 C/R A4 A3 A2 A1 A0 R/W
74+
Address Byte: 1 0 0 0 1 1 0 0
75+
```
76+
77+
### Ram address
78+
You can access to all 31 bytes to read and write data from the RAM address space
79+
80+
Lets try to calculate some address ourself:
81+
+ How to read from address 0
82+
```
83+
Month address -> 0 -> 0b00000
84+
85+
B7 C/R A4 A3 A2 A1 A0 R/W
86+
Address Byte: 1 1 0 0 0 0 0 1
87+
```
88+
89+
+ How to write to address 31
90+
```
91+
Year address -> 30 -> 0b11110
92+
93+
B7 C/R A4 A3 A2 A1 A0 R/W
94+
Address Byte: 1 1 1 1 1 1 0 0
95+
```
96+
97+
### Clock/Calendar data
98+
Data representation of DS1302 is rather different, I though it just stored the numbers directly in binary but it rather uses a different approach called BCD(binary-coded decimal) which first splits a decimal number into its individual digits and convert each decimal digit to binary using 4 bits.
99+
100+
Here is an example for BCD
101+
```
102+
Decimal Number: 56
103+
Binary Value: 00111000
104+
105+
Decimal Digit: 5 6
106+
BCD Value: 0101 0110
107+
```
108+
109+
This is the complete Clock/Calendar datamap I took from the DS1302 datasheet. Lets go though them one by one
110+
111+
{{ image(src="/img/ds1302_datamap.png", position="center", style="width:100%;") }}
112+
113+
#### Seconds
114+
<table>
115+
<tr>
116+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
117+
</tr>
118+
119+
<tr>
120+
<td>CH</td> <td colspan=3> 10 Second</td> <td colspan=4>Seconds</td> <td>00-59</td>
121+
</tr>
122+
</table>
123+
124+
+ `Bit 7` of second is `CH(Clock Halt)`, when this bit is set high the clock is set to halt and stops ticking and continue ticking when you set low.
125+
+ `Bit 6-4` is the BCD representation of the tens digit of the second value eg. 5 in 56, 3 in 32. Range:0-5
126+
+ `Bit 3-0` is the BCD representation of the ones digit of the second value eg. 6 in 56, 2 in 32. Range:0-9
127+
> **Example:** Data value for 56 second would be 0b01010110
128+
129+
#### Minutes
130+
<table>
131+
<tr>
132+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
133+
</tr>
134+
135+
<tr>
136+
<td></td> <td colspan=3> 10 Minutes</td> <td colspan=4>Minutes</td> <td>00-59</td>
137+
</tr>
138+
</table>
139+
140+
+ `Bit 7` is left empty i.e. logic 0
141+
+ `Bit 6-4` is the BCD representation of the tens digit of the minutes value eg. 5 in 56, 3 in 32. Range:0-5
142+
+ `Bit 3-0` is the BCD representation of the ones digit of the minutes value eg. 6 in 56, 2 in 32. Range:0-9
143+
144+
#### Hour
145+
Hour is bit tricky because it operates in two modes i.e. 12 and 24 hrs.
146+
<table>
147+
<tr>
148+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
149+
</tr>
150+
151+
<tr>
152+
<td rowspan=2>12/<span style="text-decoration: overline;">24</span></td>
153+
<td rowspan=2>0</td> <td>10</td> <td rowspan=2>Hour</td>
154+
<td rowspan=2 colspan=4>Hour</td> <td>0-23</td>
155+
</tr>
156+
<tr>
157+
<td><span style="text-decoration: overline;">AM</span>/PM</td><td>1-12</td>
158+
</tr>
159+
</table>
160+
161+
+ If `Bit 7` is high then 12 hour mode selected
162+
+ `Bit 5` represent the meridiem. PM if High and AM if Low.
163+
+ `Bit 4` is the BCD representation of the tens digit of the minutes value. Range:0-1
164+
+ `Bit 3-0` is the BCD representation of the ones digit of the minutes value. Range:0-9
165+
+ If `Bit 7` is low then 24 hour mode selected
166+
+ `Bit 5-4` is the BCD representation of the tens digit of the minutes value. Range:0-2
167+
+ `Bit 3-0` is the BCD representation of the ones digit of the minutes value. Range:0-9
168+
+ `Bit 6` unused bit, so should set to low
169+
Example:
170+
```
171+
Time: 7:45 PM
172+
Hour: 7, Mode: 12hrs, Meridiem: PM
173+
174+
Mode B6 Mer H10 -----H1------
175+
Data: 1 0 1 0 0 1 1 1
176+
177+
178+
Time: 17:46
179+
Hour: 17, Mode: 24hrs
180+
181+
Mode B6 ---H10-- -----H1------
182+
Data: 0 0 1 1 0 1 1 0
183+
```
184+
185+
#### Date
186+
<table>
187+
<tr>
188+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
189+
</tr>
190+
191+
<tr>
192+
<td>0</td> <td>0</td><td colspan=2> 10 Date</td> <td colspan=4>Date</td> <td>1-31</td>
193+
</tr>
194+
</table>
195+
196+
+ `Bit 7-6` unused leave empty i.e. logic 0
197+
+ `Bit 5-4` is the BCD representation of the tens digit of the Date value. Range:0-3
198+
+ `Bit 3-0` is the BCD representation of the ones digit of the Date value. Range:0-9
199+
200+
#### Month
201+
<table>
202+
<tr>
203+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
204+
</tr>
205+
206+
<tr>
207+
<td>0</td> <td>0</td><td>0</td><td colspan=1> 10 month</td> <td colspan=4>month</td> <td>1-12</td>
208+
</tr>
209+
</table>
210+
211+
+ `Bit 7-5` unused leave empty i.e. logic 0
212+
+ `Bit 4` is the BCD representation of the tens digit of the month value. Range:0-1
213+
+ `Bit 3-0` is the BCD representation of the ones digit of the month value. Range:0-9
214+
215+
#### Day
216+
<table>
217+
<tr>
218+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
219+
</tr>
220+
221+
<tr>
222+
<td>0</td><td>0</td><td>0</td><td>0</td><td>0</td> <td colspan=3> Day</td>
223+
</tr>
224+
</table>
225+
226+
+ `Bit 7-3` unused leave empty i.e. logic 0
227+
+ `Bit 2-0` represent day of the week, range: 1-7
228+
229+
#### Year
230+
<table>
231+
<tr>
232+
<th>BIT 7</th> <th>BIT 6</th> <th>BIT 5</th> <th>BIT 4</th> <th>BIT 3</th> <th>BIT 2</th> <th>BIT 1</th> <th>BIT 0</th> <th>RANGE</th>
233+
</tr>
234+
235+
<tr>
236+
<td colspan=4> 10 year</td> <td colspan=4>year</td> <td>00-99</td>
237+
</tr>
238+
</table>
239+
240+
+ `Bit 7-4` is the BCD representation of the tens digit of the year value. Range:0-9
241+
+ `Bit 3-0` is the BCD representation of the ones digit of the year value. Range:0-9

blog/static/img/ds1302_addressmap.png

18.8 KB
Loading
75.3 KB
Loading

blog/static/img/ds1302_datamap.png

62 KB
Loading

blog/static/img/ds1302_module.png

385 KB
Loading

blog/static/img/ds1302_pinout.png

6.5 KB
Loading

blog/templates/shortcodes/image.html

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
<style>
2+
.center {
3+
display: block;
4+
margin-left: auto;
5+
margin-right: auto;
6+
}
7+
</style>
8+
19
{% if src %}
210
{# If the image's URL is internal to the site... #}
311
{% if src is not starting_with("http") %}

0 commit comments

Comments
 (0)