-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS3231.c
472 lines (420 loc) · 13.1 KB
/
DS3231.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
///////////////////////////////////////////////////////////////////////////
//// ////
//// DS3231.c ////
//// ////
//// Driver for CCS C compiler ////
//// ////
//// Driver for Maxim DS3231 serial I2C real-time clock (RTC). ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// ////
//// https://simple-circuit.com/ ////
//// ////
///////////////////////////////////////////////////////////////////////////
#if defined DS3231_I2C_NO_STREAM
#define RTC_I2C_START() i2c_start()
#define RTC_I2C_STOP() i2c_stop()
#define RTC_I2C_WRITE(x) i2c_write(x)
#define RTC_I2C_READ(x) i2c_read(x)
#elif defined DS3231_I2C_STREAM
#define RTC_I2C_START() i2c_start(DS3231_I2C_STREAM)
#define RTC_I2C_STOP() i2c_stop(DS3231_I2C_STREAM)
#define RTC_I2C_WRITE(x) i2c_write(DS3231_I2C_STREAM, x)
#define RTC_I2C_READ(x) i2c_read(DS3231_I2C_STREAM, x)
#else
#define RTC_I2C_START() i2c_start(DS3231_STREAM)
#define RTC_I2C_STOP() i2c_stop(DS3231_STREAM)
#define RTC_I2C_WRITE(x) i2c_write(DS3231_STREAM, x)
#define RTC_I2C_READ(x) i2c_read(DS3231_STREAM, x)
#endif
#include <stdint.h>
#define DS3231_ADDRESS 0xD0
#define DS3231_REG_SECONDS 0x00
#define DS3231_REG_AL1_SEC 0x07
#define DS3231_REG_AL2_MIN 0x0B
#define DS3231_REG_CONTROL 0x0E
#define DS3231_REG_STATUS 0x0F
#define DS3231_REG_TEMP_MSB 0x11
typedef enum
{
SUNDAY = 1,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY
} RTC_DOW;
typedef enum
{
JANUARY = 1,
FEBRUARY,
MARCH,
APRIL,
MAY,
JUNE,
JULY,
AUGUST,
SEPTEMBER,
OCTOBER,
NOVEMBER,
DECEMBER
} RTC_Month;
typedef struct rtc_tm
{
uint8_t seconds;
uint8_t minutes;
uint8_t hours;
RTC_DOW dow;
uint8_t day;
RTC_Month month;
uint8_t year;
} RTC_Time;
typedef enum
{
ONCE_PER_SECOND = 0x0F,
SECONDS_MATCH = 0x0E,
MINUTES_SECONDS_MATCH = 0x0C,
HOURS_MINUTES_SECONDS_MATCH = 0x08,
DATE_HOURS_MINUTES_SECONDS_MATCH = 0x0,
DAY_HOURS_MINUTES_SECONDS_MATCH = 0x10
} al1;
typedef enum
{
ONCE_PER_MINUTE = 0x0E,
MINUTES_MATCH = 0x0C,
HOURS_MINUTES_MATCH = 0x08,
DATE_HOURS_MINUTES_MATCH = 0x0,
DAY_HOURS_MINUTES_MATCH = 0x10
} al2;
typedef enum
{
OUT_OFF = 0x00,
OUT_INT = 0x04,
OUT_1Hz = 0x40,
OUT_1024Hz = 0x48,
OUT_4096Hz = 0x50,
OUT_8192Hz = 0x58
} INT_SQW;
RTC_Time c_time, c_alarm1, c_alarm2;
///////////////////////// All Functions /////////////////////////
//
uint8_t bcd_to_decimal(uint8_t number); //
uint8_t decimal_to_bcd(uint8_t number); //
void RTC_Set(RTC_Time *time_t); //
RTC_Time *RTC_Get(); //
void Alarm1_Set(RTC_Time *time_t, al1 _config); //
RTC_Time *Alarm1_Get(); //
void Alarm1_Enable(); //
void Alarm1_Disable(); //
int1 Alarm1_IF_Check(); //
void Alarm1_IF_Reset(); //
int1 Alarm1_Status(); //
void Alarm2_Set(RTC_Time *time_t, al2 _config); //
RTC_Time *Alarm2_Get(); //
void Alarm2_Enable(); //
void Alarm2_Disable(); //
int1 Alarm2_IF_Check(); //
void Alarm2_IF_Reset(); //
int1 Alarm2_Status(); //
void IntSqw_Set(INT_SQW _config); //
void Enable_32kHZ(); //
void Disable_32kHZ(); //
void OSC_Start(); //
void OSC_Stop(); //
int16_t Get_Temperature(); //
uint8_t RTC_Read_Reg(uint8_t reg_address); //
void RTC_Write_Reg(uint8_t reg_address, uint8_t reg_value); //
//
/////////////////////////////////////////////////////////////////
// converts BCD to decimal
uint8_t bcd_to_decimal(uint8_t number)
{
return ( (number >> 4) * 10 + (number & 0x0F) );
}
// converts decimal to BCD
uint8_t decimal_to_bcd(uint8_t number)
{
return ( ((number / 10) << 4) + (number % 10) );
}
// sets time and date
void RTC_Set(RTC_Time *time_t)
{
// convert decimal to BCD
time_t->day = decimal_to_bcd(time_t->day);
time_t->month = decimal_to_bcd(time_t->month);
time_t->year = decimal_to_bcd(time_t->year);
time_t->hours = decimal_to_bcd(time_t->hours);
time_t->minutes = decimal_to_bcd(time_t->minutes);
time_t->seconds = decimal_to_bcd(time_t->seconds);
// end conversion
// write data to the RTC chip
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_SECONDS);
RTC_I2C_WRITE(time_t->seconds);
RTC_I2C_WRITE(time_t->minutes);
RTC_I2C_WRITE(time_t->hours);
RTC_I2C_WRITE(time_t->dow);
RTC_I2C_WRITE(time_t->day);
RTC_I2C_WRITE(time_t->month);
RTC_I2C_WRITE(time_t->year);
RTC_I2C_STOP();
}
// reads time and date
RTC_Time *RTC_Get()
{
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_SECONDS);
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
c_time.seconds = RTC_I2C_READ(1);
c_time.minutes = RTC_I2C_READ(1);
c_time.hours = RTC_I2C_READ(1);
c_time.dow = RTC_I2C_READ(1);
c_time.day = RTC_I2C_READ(1);
c_time.month = RTC_I2C_READ(1);
c_time.year = RTC_I2C_READ(0);
RTC_I2C_STOP();
// convert BCD to decimal
c_time.seconds = bcd_to_decimal(c_time.seconds);
c_time.minutes = bcd_to_decimal(c_time.minutes);
c_time.hours = bcd_to_decimal(c_time.hours);
c_time.day = bcd_to_decimal(c_time.day);
c_time.month = bcd_to_decimal(c_time.month);
c_time.year = bcd_to_decimal(c_time.year);
// end conversion
return &c_time;
}
// sets alarm1 details
void Alarm1_Set(RTC_Time *time_t, al1 _config)
{
// convert decimal to BCD
time_t->day = decimal_to_bcd(time_t->day);
time_t->hours = decimal_to_bcd(time_t->hours);
time_t->minutes = decimal_to_bcd(time_t->minutes);
time_t->seconds = decimal_to_bcd(time_t->seconds);
// end conversion
// write data to the RTC chip
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_AL1_SEC);
RTC_I2C_WRITE( (time_t->seconds) | (bit_test(_config, 0) << 7) );
RTC_I2C_WRITE( (time_t->minutes) | (bit_test(_config, 1) << 7) );
RTC_I2C_WRITE( (time_t->hours) | (bit_test(_config, 2) << 7) );
if ( bit_test(_config, 4) )
RTC_I2C_WRITE( (time_t->dow) | 0x40 | (bit_test(_config, 3) << 7) );
else
RTC_I2C_WRITE( (time_t->day) | (bit_test(_config, 3) << 7) );
RTC_I2C_STOP();
}
// reads alarm1 details
RTC_Time *Alarm1_Get()
{
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_AL1_SEC);
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
c_alarm1.seconds = RTC_I2C_READ(1) & 0x7F;
c_alarm1.minutes = RTC_I2C_READ(1) & 0x7F;
c_alarm1.hours = RTC_I2C_READ(1) & 0x3F;
c_alarm1.dow = c_alarm1.day = RTC_I2C_READ(0) & 0x3F;
RTC_I2C_STOP();
// convert BCD to decimal
c_alarm1.seconds = bcd_to_decimal(c_alarm1.seconds);
c_alarm1.minutes = bcd_to_decimal(c_alarm1.minutes);
c_alarm1.hours = bcd_to_decimal(c_alarm1.hours);
c_alarm1.day = bcd_to_decimal(c_alarm1.day);
// end conversion
return &c_alarm1;
}
// enables alarm1
void Alarm1_Enable()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg |= 0x01;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
// disables alarm1
void Alarm1_Disable()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg &= 0xFE;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
// checks if alarm1 occurred, returns 1 if yes and 0 if no
int1 Alarm1_IF_Check()
{
uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
return bit_test(stat_reg, 0);
}
// resets alarm1 flag bit
void Alarm1_IF_Reset()
{
uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
stat_reg &= 0xFE;
RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}
// returns TRUE (1) if alarm1 is enabled and FALSE (0) if disabled
int1 Alarm1_Status()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
if(ctrl_reg & 0x01)
return 1;
else
return 0;
}
// sets alarm2 details
void Alarm2_Set(RTC_Time *time_t, al2 _config)
{
// convert decimal to BCD
time_t->day = decimal_to_bcd(time_t->day);
time_t->hours = decimal_to_bcd(time_t->hours);
time_t->minutes = decimal_to_bcd(time_t->minutes);
// end conversion
// write data to the RTC chip
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_AL2_MIN);
RTC_I2C_WRITE( (time_t->minutes) | (bit_test(_config, 1) << 7) );
RTC_I2C_WRITE( (time_t->hours) | (bit_test(_config, 2) << 7) );
if ( bit_test(_config, 4) )
RTC_I2C_WRITE( (time_t->dow) | 0x40 | (bit_test(_config, 3) << 7) );
else
RTC_I2C_WRITE( (time_t->day) | (bit_test(_config, 3) << 7) );
RTC_I2C_STOP();
}
// reads alarm2 details
RTC_Time *Alarm2_Get()
{
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_AL2_MIN);
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
c_alarm2.minutes = RTC_I2C_READ(1) & 0x7F;
c_alarm2.hours = RTC_I2C_READ(1) & 0x3F;
c_alarm2.dow = c_alarm2.day = RTC_I2C_READ(0) & 0x3F;
RTC_I2C_STOP();
// convert BCD to decimal
c_alarm2.minutes = bcd_to_decimal(c_alarm2.minutes);
c_alarm2.hours = bcd_to_decimal(c_alarm2.hours);
c_alarm2.day = bcd_to_decimal(c_alarm2.day);
// end conversion
return &c_alarm2;
}
// enables alarm2
void Alarm2_Enable()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg |= 0x02;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
//disables alarm2
void Alarm2_Disable()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg &= 0xFD;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
// checks if alarm2 occurred, returns 1 if yes and 0 if no
int1 Alarm2_IF_Check()
{
uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
return bit_test(stat_reg, 1);
}
// resets alarm2 flag bit
void Alarm2_IF_Reset()
{
uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
stat_reg &= 0xFD;
RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}
// returns TRUE (1) if alarm2 is enabled and FALSE (0) if disabled
int1 Alarm2_Status()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
if(ctrl_reg & 0x02)
return 1;
else
return 0;
}
// writes 'reg_value' to register of address 'reg_address'
void RTC_Write_Reg(uint8_t reg_address, uint8_t reg_value)
{
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(reg_address);
RTC_I2C_WRITE(reg_value);
RTC_I2C_STOP();
}
// returns the value stored in register of address 'reg_address'
uint8_t RTC_Read_Reg(uint8_t reg_address)
{
uint8_t reg_data;
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(reg_address);
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
reg_data = RTC_I2C_READ(0);
RTC_I2C_STOP();
return reg_data;
}
// sets INT/SQW pin configuration
void IntSqw_Set(INT_SQW _config)
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg &= 0xA3;
ctrl_reg |= _config;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
// enables 32kHz (pin 32kHz)
void Enable_32kHZ()
{
uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
stat_reg |= 0x08;
RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}
// disables 32kHz (pin 32kHz)
void Disable_32kHZ()
{
uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
stat_reg &= 0xF7;
RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}
// starts RTC oscillator
void OSC_Start()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg &= 0x7F;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
// stops RTC oscillator
void OSC_Stop()
{
uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
ctrl_reg |= 0x80;
RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}
// returns chip temperature
// Temperature is stored in hundredths C (output value of "3125" equals 31.25 °C).
int16_t Get_Temperature()
{
uint8_t t_msb, t_lsb;
uint16_t c_temp;
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS);
RTC_I2C_WRITE(DS3231_REG_TEMP_MSB);
RTC_I2C_START();
RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
t_msb = RTC_I2C_READ(1);
t_lsb = RTC_I2C_READ(0);
RTC_I2C_STOP();
c_temp = (uint16_t)t_msb << 2 | t_lsb >> 6;
if(t_msb & 0x80)
c_temp |= 0xFC00;
return c_temp * 25;
}