forked from chrisfryer78/ArduinoRTClibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtuabotixRTC.cpp
710 lines (692 loc) · 71.6 KB
/
virtuabotixRTC.cpp
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
'@ @@@: @@@@@@ @@` '@@
.@@@@@# @`@@@@@. @@ @@# @'@
@@@. `@ ;@ .@@ @@ @.@ @.@
`@ @@ @@ @+ @@ @ @ @.@
@` @: @` @` @@ @ @@@.@
@# ;@@@@@@@@ @ @@ @ #@,.@
'@ @ @@ @ .@ .@
;@ @@. @@@
`@ `@@. +@@ @@`@@
@@@' @@@ @@; @@ @@
@@ ,@@#@@ ,@@@ @@
@@ #@ , @+
@@ .#@@@@@@@@@@@` @,
@ @@@@@+, `:+@@ @@
#@ @: @@ '@
@@ @# @. @,
@@ `@ `@ ++ @
`@ # @+ @@ ;@@@ @@
@+ @@@ .@ ` @` .@@@@@ @,
+@ @@@@@ @+'@@, @@@#@ @@@@@@@ '@
@. @@@@@@+ `@@@@ :@'@; @@@@@@@@@ @@ .+`
@@ @@@@@@@@` @; @@ #@@@@@@@@@, @@@@@@@
@@@: `@ +@@@@@@@@@ +@ @. `@@@@@@@@@@ '` @
@,@@@@; @@@@@@@@@@ @: @@ +@ ;@@@@@@@@@# @;
@ @@@@@@@@@@, #@ @@@@ @# #@@@@@@@@@ @@
'@ @@@@@@@@@@ @, @``@` `@ @@@@@@@@@@, `@
@@ ;@@@@@@@@@+ @@ @@ @@ @@ @@@@@@@@@@@ @`
@, @@@@@@@@@@@ @. .@ @# @' @@@@@@@@@@@@ @@:
@ @@@@@@@@@@@@ @@ @@ ,@ :@'@@@@@@@@@@@@ :@@@`
@@# @@@@@@@@@@@@,@` ;@ '@@' @@ @@@@@:@@@@@@@@` @'
;@@@ ;@@@@@@@@@@@@@@ @'@@.,@@,@; @@@@@ #@@@@@@@` @
@ @@@@@@@@' @@@@@ #@@+ ;@@@.@@@@. @@@; @
@ .;+@ '@@@@#@'@ ;@@@@@@@ @
@ @@@@@@:@ +@;@@@@@ @
@` @@@@@@:@ #@@@@@@@@ @@@: @
@` .@@@# @@@@@@+@ @@@@@@+ @;+@+@@@@@@ @
@` @@@@@;;@ @@.@@@@@@# :@@@@@@` ;@@# .@ @@+
@` '@, @.@ @@@@@@@@' @@@@@@@@@ @@, +@ :@@@.
`@@` ;@ @@# +@@@@@@ @@@ @@@@@ @+ @ @+ @@
+@@@ `@ .@ @, @@@@@ @@@@@ `@ @ '@
#@: @ @@ #@@@@. @@@@@ @@ .@ @#
@ @ @ @@@@@ @@@@@ :@ @@ @`
@; @# +@ .@@@@@.@@@@# `@: @ @
#@ ,@ @@ @@@@@@@@@@ @# @@ , @@
@ @, @# @@@@@@@@# @` `@ @@@@@@;
@@@@@@@ :@ @ '@@@@@@@ +@ @# ;@
#;. @@ @@ @@ @@@@@@@.@ @@ @@ @;
`@ @+ @@ #@@@@@@@@@@@+ @+ @@ +@
@@ @@ #@ @@@@@@@@@@@@@ `@.@@ @:
@. @@.:@ @@@@@@@@@@@@@# '@# +@
'@ #@@, #@@@@@@@@@@@@@@ @;
@; @@@@@@@@@@@@@@@' `@`
`@ @@@@@@@@@@@@@@@@ #@
@+ '@@@@@@@@@@@@@@@@ @@
@@ '@@@@@@@@@@@: @:
@@ ' @@
+@; `@; @@@@ @@
:@+ ,@'@@ ;@@ @@# @@
.@@ :@; @@@ @@: .@@@
@@@; @@: `@@ '
@; ;@@ .@
'@ +@@@@@@@. @
@@ .@###@@@@@ @`
@; @@ @. @+
@` @ @@ ,@@
@@@. ;@ `@;@@@@@`
:@@@@@# @@@.
@@@@@ @@@@@@ @@@@@@ @@@ @@@ @@@@@ @@@@@ @@@@@@: @@@@@
@@@@@@@ @@@@@@@ @@@@@@ @@@ @@@ @@@@@@@ @@@@@@@ @@@@@@@: @@@@@
@@@ @@@ @@@@@@@ @@@@@@ @@@ @@@ @@@ @@@ @@@ @@@ @@ @@@ @@@@@
@@@ @@@ @@@ @@ @@@ @@@ @@@ @@@ @@@ @@@ @@@ @@ @@ @@@
@@@ @@@ @@@@@@@ @@@ @@@@@@@ @@@ @@@ @@@ @@@ @@ @@ @@@
@@@ @@@ @@@@@@@ @@@@@ @@@@@@@ @@@ @@@ @@@ @@ @@ @@@@@
@@@ @@@ @@@@@@@ @@@@@ @@@@@@@ @@@ @@@ @@@ @@ @@ @@@@@
@@@ @@@ @@@@ @@@ @@ @@@@ @@@ @@@ @@@ @@@ @@ @@ @@@
@@@ @@@ @@@@ @@@ @@ @@@@ @@@ @@@ @@@ @@@ @@ @@ @@@
@@@ @@@ @@@@ @@@ @@ @@@@ @@@ @@@ @@@ @@@ @@ @@@ @@@
@@@@@@@ @@@@ @@@@@@ @@ @@@ @@@@@@@ @@@@@@@ @@@@@@@ @@@@@
@@@@@ @@@@ @@@@@@ @@ @@@ @@@@@ @@@@@ @@@@@@ @@@@@
*/
//=======================================================================================================//|
// //|
// BEGINNNING OF FILE //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// TITLE: DS1302 Real Time Clock //|
// FILENAME: virtuabotixRTC.h //|
// PREPARED FOR: Virtuabotic, LLC //|
// PROGRAMMER(S): Arduino.cc user "Krodal" //|
// Joseph Datillo //|
// T. Daniel Murphy //|
// DEVELOPMENT DATE: 06 January 2014 //|
// COMPILER USED: Arduino 1.0.5 and xCode 5.0.2 (Version 5A3005) //|
// TARGET PLATFORM: Versilino and Arduino Microcontrollers //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// Program Description Begin //|
// //|
//=======================================================================================================//|
// //|
// This file defines the functions declared within the associated header file. This allows for the //|
// structure of the Real Time Clock to be defined, set, updated, then accessed. //|
// //|
//=======================================================================================================//|
// //|
// Program Description End //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// This is the Revision Section Begin //|
// //|
//=======================================================================================================//|
// //|
// Date Programmer Revisions Made //|
// //|
// XX June 2012 Krodal Original //|
// XX March 2013 Krodal Added bcd2bin, bin2bcd_h, and bin2bcd_l functions//|
// 07 January 2014 Joseph Datillo Converted file into library format. //|
// T. Daniel Murphy Added functions to allow for user implentation //|
// as a library, vice a stand alone program. //|
// Added function for converting time into current //|
// UNIX time. //|
// //|
//=======================================================================================================//|
// //|
// Revision Section End //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// Acknowledgements Begin //|
// //|
//=======================================================================================================//|
// //|
// The original program was provided by Arduino.cc user Kodal on URL: //|
// http://playground.arduino.cc/Main/DS1302 //|
// //|
//=======================================================================================================//|
// //|
// Acknowledgements End //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// Reference Links Begin //|
// //|
//=======================================================================================================//|
//|
// Datasheet: http://www.hobbytronics.co.uk/datasheets/DS1302.pdf //|
// GET UPDATES: https://www.virtuabotix.com/resources.php?product=versalino //|
//|
//=======================================================================================================//|
// //|
// Reference Links End //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// Include files required for Program Function Begin //|
// //|
//=======================================================================================================//|
//|
#include "virtuabotixRTC.h" //|
//|
//=======================================================================================================//|
// //|
// Include files required for Program Function End //|
// //|
//=======================================================================================================//|
//=======================================================================================================//|
// //|
// Defines Required for Library Begin //|
// //|
//=======================================================================================================//|
//|
//+++++++++++++++++++++++++++++++++++++++++ Set Pin Assignments +++++++++++++++++++++++++++++++++++++++++//|
#define DS1302_SCLK_PIN SCLK // Arduino pin for the Serial Clock //|
#define DS1302_IO_PIN IO // Arduino pin for the Data I/O //|
#define DS1302_CE_PIN C_E // Arduino pin for the Chip Enable //|
//|
//++++++++++++++++++++++++++++++++++++++++++ Conversion Macros ++++++++++++++++++++++++++++++++++++++++++//|
// Macros to convert the bcd values of the registers to normal integer variables. The code uses //|
// seperate variables for the high byte and the low byte of the bcd, so these macros handle both bytes //|
// seperately. //|
#define bcd2bin(h,l) (((h)*10) + (l)) //|
#define bin2bcd_h(x) ((x)/10) //|
#define bin2bcd_l(x) ((x)%10) //|
//|
//+++++++++++++++++++++++++++++++++++++++++ Set Register Names ++++++++++++++++++++++++++++++++++++++++++//|
// Since the highest bit is always '1', the registers start at 0x80. If the register is read, the //|
// lowest bit should be '1'. //|
#define DS1302_SECONDS 0x80 //|
#define DS1302_MINUTES 0x82 //|
#define DS1302_HOURS 0x84 //|
#define DS1302_DATE 0x86 //|
#define DS1302_MONTH 0x88 //|
#define DS1302_DAY 0x8A //|
#define DS1302_YEAR 0x8C //|
#define DS1302_CLOCK_BURST 0xBE //|
#define DS1302_CLOCK_BURST_WRITE 0xBE //|
#define DS1302_CLOCK_BURST_READ 0xBF //|
#define DS1302_RAMSTART 0xC0 //|
#define DS1302_RAMEND 0xFC //|
#define DS1302_RAM_BURST 0xFE //|
#define DS1302_RAM_BURST_WRITE 0xFE //|
#define DS1302_RAM_BURST_READ 0xFF //|
//|
//+++++++++++++++++++++++++++++++++++++++++++++ Bit Defines +++++++++++++++++++++++++++++++++++++++++++++//|
// Defines for the bits, to be able to change between bit number and binary definition. By using the //|
// bit number, using the DS1302 is like programming an AVR microcontroller. But instead of using //|
// "(1<<X)", or "_BV(X)", the Arduino "bit(X)" is used. //|
#define DS1302_D0 0 //|
#define DS1302_D1 1 //|
#define DS1302_D2 2 //|
#define DS1302_D3 3 //|
#define DS1302_D4 4 //|
#define DS1302_D5 5 //|
#define DS1302_D6 6 //|
#define DS1302_D7 7 //|
//|
//++++++++++++++++++++++++++++++++++++++++++++++++ Random Bits ++++++++++++++++++++++++++++++++++++++++++//|
// Bit for reading (bit in address) //|
#define DS1302_READBIT DS1302_D0 // READBIT=1: read instruction //|
//|
// Bit for clock (0) or ram (1) area, called R/C-bit (bit in address) //|
#define DS1302_RC DS1302_D6 //|
//|
// Seconds Register //|
#define DS1302_CH DS1302_D7 // 1 = Clock Halt, 0 = start //|
//|
// Hour Register //|
#define DS1302_AM_PM DS1302_D5 // 0 = AM, 1 = PM //|
#define DS1302_12_24 DS1302 D7 // 0 = 24 hour, 1 = 12 hour //|
//|
// Enable Register //|
#define DS1302_WP DS1302_D7 // 1 = Write Protect, 0 = enabled //|
//|
// Trickle Register //|
#define DS1302_ROUT0 DS1302_D0 //|
#define DS1302_ROUT1 DS1302_D1 //|
#define DS1302_DS0 DS1302_D2 //|
#define DS1302_DS1 DS1302_D2 //|
#define DS1302_TCS0 DS1302_D4 //|
#define DS1302_TCS1 DS1302_D5 //|
#define DS1302_TCS2 DS1302_D6 //|
#define DS1302_TCS3 DS1302_D7 //|
//|
//=======================================================================================================//|
// //|
// Defines Required for Library End //|
// //|
//=======================================================================================================//|
//============================================================================================================//|
// //|
// virtuabotixRTC Class Functions Begin //|
// //|
//============================================================================================================//|
//=======================================================================================================//| |
// //| |
// virtuabotixRTC Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// This is the CONSTRUCTOR of the class. It sets the pins assignments for the component, as well as //| |
// the Clock Enable and the trickle charge disablement. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
virtuabotixRTC::virtuabotixRTC(uint8_t inSCLK, uint8_t inIO, uint8_t inC_E) { //| |
SCLK = inSCLK; //| |
IO = inIO; //| |
C_E = inC_E; //| |
DS1302_write (DS1302_ENABLE, 0); // Sets the Clock Enable to ON. //| |
DS1302_write (DS1302_TRICKLE, 0x00); // Disable Trickle Charger. //| |
} //| |
//=======================================================================================================//| |
// //| |
// virtuabotixRTC Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// ds1302_struct Structure Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// This is the structure of the rtc object. This assigns how many bits, and their location for each of //| |
// the elements within the time data. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
//++++++++++++++++++++ This struct sets the format for the Time fields on the DS1302 ++++++++++++++++++++//| |
struct ds1302_struct { //| |
uint8_t Seconds:4; // 4-bits to hold low decimal digits 0-9 //| |
uint8_t Seconds10:3; // 3-bits to hold high decimal digit 0-5 //| |
uint8_t CH:1; // 1-bit to hold CH = Clock Halt //| |
uint8_t Minutes:4; // 4-bits to hold low decimal digits 0-9 //| |
uint8_t Minutes10:3; // 3-bits to hold high decimal digit 0-5 //| |
uint8_t reserved1:1; //| |
union { //| |
struct { // 24-hour section //| |
uint8_t Hour:4; // 4-bits to hold low decimal digits 0-9 //| |
uint8_t Hour10:2; // 2-bits to hold high decimal digits 0-2 //| |
uint8_t reserved2:1; //| |
uint8_t hour_12_24:1; // 1-bit to set 0 for 24 hour format //| |
} h24; //| |
struct { // 12 hour section //| |
uint8_t Hour:4; // 4-bits to hold low decimal digits 0-9 //| |
uint8_t Hour10:1; // 2-bits to hold high decimal digits 0-2 //| |
uint8_t AM_PM:1; // 1-bit to set AM or PM, 0 = AM, 1 = PM //| |
uint8_t reserved2:1; //| |
uint8_t hour_12_24:1; // 1-bit to set 1 for 12 hour format //| |
} h12; //| |
}; //| |
uint8_t Date:4; // 4-bits to hold low decimal digits 0-9 //| |
uint8_t Date10:2; // 2-bits to hold high decimal digits 0-3 //| |
uint8_t reserved3:2; //| |
uint8_t Month:4; // 4-bits to hold low decimal digits 0-9 //| |
uint8_t Month10:1; // 3-bits to hold high decimal digits 0-5 //| |
uint8_t reserved4:3; //| |
uint8_t Day:3; // 3-bits to hold decimal digit 1-7 //| |
uint8_t reserved5:5; //| |
uint8_t Year:4; // 4-bits to hold high decimal digit 20 //| |
uint8_t Year10:4; // 4-bits to hold high decimal digit 14 //| |
uint8_t reserved6:7; //| |
uint8_t WP:1; // WP = Write Protect //| |
}; //| |
//+++++++++++++++++++++++++++++++++ instantiation of the struct object ++++++++++++++++++++++++++++++++++//| |
ds1302_struct rtc; // Creates the Real Time Clock object //| |
//| |
//=======================================================================================================//| |
// //| |
// ds1302_struct Structure End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// DS1302_clock_burst_read Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// This function reads 8 bytes clock data in burst mode from the DS1302. This function may be called //| |
// as the first function, also the pinMode is set. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::DS1302_clock_burst_read( uint8_t *p) { //| |
int i; //| |
_DS1302_start(); //| |
//| |
// Instead of the address, the CLOCK_BURST_READ command is issued the I/O-line is released for the data //| |
_DS1302_togglewrite( DS1302_CLOCK_BURST_READ, true); //| |
//| |
for( i=0; i<8; i++) { //| |
*p++ = _DS1302_toggleread(); //| |
} //| |
_DS1302_stop(); //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// DS1302_clock_burst_read Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// DS1302_clock_burst_write Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// This function writes 8 bytes clock data in burst mode to the DS1302. This function may be called //| |
// as the first function, also the pinMode is set. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::DS1302_clock_burst_write( uint8_t *p) { //| |
int i; //| |
_DS1302_start(); //| |
//| |
// Instead of the address, the CLOCK_BURST_WRITE command is issued. The I/O-line is not released //| |
_DS1302_togglewrite( DS1302_CLOCK_BURST_WRITE, false); //| |
//| |
for( i=0; i<8; i++) { //| |
// the I/O-line is not released //| |
_DS1302_togglewrite( *p++, false); //| |
} //| |
_DS1302_stop(); //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// DS1302_clock_burst_write Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// DS1302_read Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// This function reads a byte from the DS1302 (clock or ram). The address could be like "0x80" or //| |
// "0x81", the lowest bit is set anyway. This function may be called as the first function, also the //| |
// pinMode is set. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
uint8_t virtuabotixRTC::DS1302_read(int address) { //| |
uint8_t data; //| |
//| |
// set lowest bit (read bit) in address //| |
bitSet( address, DS1302_READBIT); //| |
//| |
_DS1302_start(); //| |
// the I/O-line is released for the data //| |
_DS1302_togglewrite( address, true); //| |
data = _DS1302_toggleread(); //| |
_DS1302_stop(); //| |
//| |
return (data); //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// DS1302_read Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// DS1302_write Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// This function writes a byte to the DS1302 (clock or ram). The address could be like "0x80" or //| |
// "0x81", the lowest bit is cleared anyway. This function may be called as the first function, //| |
// also the pinMode is set. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::DS1302_write( int address, uint8_t data) { //| |
// clear lowest bit (read bit) in address //| |
bitClear( address, DS1302_READBIT); //| |
//| |
_DS1302_start(); //| |
//| |
// don't release the I/O-line //| |
_DS1302_togglewrite( address, false); //| |
//| |
// don't release the I/O-line //| |
_DS1302_togglewrite( data, false); //| |
_DS1302_stop(); //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// DS1302_write Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// _DS1302_start Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// A helper function to setup the start condition. An 'init' function is not used. But now the //| |
// pinMode is set every time. That's not a big deal, and it's valid. At startup, the pins of the //| |
// Arduino are high impedance. Since the DS1302 has pull-down resistors, the signals are low //| |
// (inactive) until the DS1302 is used. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::_DS1302_start( void ) { //| |
digitalWrite( DS1302_CE_PIN, LOW ); // default, not enabled //| |
pinMode( DS1302_CE_PIN, OUTPUT ); //| |
//| |
digitalWrite( DS1302_SCLK_PIN, LOW ); // default, clock low //| |
pinMode( DS1302_SCLK_PIN, OUTPUT ); //| |
//| |
pinMode( DS1302_IO_PIN, OUTPUT ); //| |
//| |
digitalWrite( DS1302_CE_PIN, HIGH ); // start the session //| |
delayMicroseconds( 4 ); // tCC = 4us //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// _DS1302_start Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// _DS1302_stop Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// A helper function to finish the communication. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::_DS1302_stop( void ) { //| |
// Set CE low //| |
digitalWrite( DS1302_CE_PIN, LOW ); //| |
//| |
delayMicroseconds( 4 ); // tCWH = 4us //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// _DS1302_stop Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// _DS1302_toggleread Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// A helper function for reading a byte with bit toggle. This function assumes that the SCLK is still //| |
// high. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
uint8_t virtuabotixRTC::_DS1302_toggleread( void ) { //| |
uint8_t i, data; //| |
data = 0; //| |
//| |
// Issue a clock pulse for the next databit. If the 'togglewrite' function was used before this //| |
// function, the SCLK is already high. //| |
for( i = 0; i <= 7; i++ ) { //| |
digitalWrite( DS1302_SCLK_PIN, HIGH ); //| |
delayMicroseconds( 1) ; //| |
//| |
// Clock down, data is ready after some time. //| |
digitalWrite( DS1302_SCLK_PIN, LOW ); //| |
delayMicroseconds( 1 ); // tCL=1000ns, tCDD=800ns //| |
//| |
// read bit, and set it in place in 'data' variable //| |
bitWrite( data, i, digitalRead( DS1302_IO_PIN ) ); //| | //| |
} //| |
return( data ); //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// _DS1302_toggleread Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// _DS1302_togglewrite Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// A helper function for writing a byte with bit toggle. The 'release' parameter is for a read after //| |
// this write. It will release the I/O-line and will keep the SCLK high. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::_DS1302_togglewrite( uint8_t data, uint8_t release) { //| |
int i; //| |
//| |
for( i = 0; i <= 7; i++ ) { //| |
// set a bit of the data on the I/O-line //| |
digitalWrite( DS1302_IO_PIN, bitRead(data, i) ); //| |
delayMicroseconds( 1 ); // tDC = 200ns //| |
//| |
// clock up, data is read by DS1302 //| |
digitalWrite( DS1302_SCLK_PIN, HIGH ); //| |
delayMicroseconds( 1 ); // tCH = 1000ns, tCDH = 800ns //| |
//| |
// If this write is followed by a read, the I/O-line should be released after the last bit, before the //| |
// clock line is made low. This is according the datasheet. I have seen other programs that don't //| |
// release the I/O-line at this moment, and that could cause a shortcut spike on the I/O-line. //| |
if( release && i == 7 ) { //| |
pinMode( DS1302_IO_PIN, INPUT ); //| |
} else { //| |
digitalWrite( DS1302_SCLK_PIN, LOW ); //| |
delayMicroseconds( 1 ); // tCL=1000ns, tCDD=800ns //| |
} //| |
} //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// _DS1302_togglewrite Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// setDS1302Time Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// A function to set the initial time within the DS1302 Real Time Clock. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::setDS1302Time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t dayofweek, //| |
uint8_t dayofmonth, uint8_t month, int year) { //| |
//| |
//+++++++++++++++++++++++++++++++++++++++ Variable Declarations +++++++++++++++++++++++++++++++++++++++++//| |
seconds = seconds; //| |
minutes = minutes; //| |
hours = hours; //| |
dayofweek = dayofweek; //| |
dayofmonth = dayofmonth; //| |
month = month; //| |
year = year; //| |
//| |
//++++++++++++++++++++ This will fill the output array of the rtc object with ZEROs +++++++++++++++++++++//| |
memset ((char *) &rtc, 0, sizeof(rtc)); //| |
//| |
//+++++++++++++++++++ This contains the conversion to BCD, and assigns to the elements ++++++++++++++++++//| |
rtc.Seconds = bin2bcd_l( seconds); //| |
rtc.Seconds10 = bin2bcd_h( seconds); //| |
rtc.CH = 0; // 1 for Clock Halt, 0 to run //| |
rtc.Minutes = bin2bcd_l( minutes); //| |
rtc.Minutes10 = bin2bcd_h( minutes); //| |
rtc.h24.Hour = bin2bcd_l( hours); //| |
rtc.h24.Hour10 = bin2bcd_h( hours); //| |
rtc.h24.hour_12_24 = 0; // 0 for 24 hour format //| |
rtc.Date = bin2bcd_l( dayofmonth); //| |
rtc.Date10 = bin2bcd_h( dayofmonth); //| |
rtc.Month = bin2bcd_l( month); //| |
rtc.Month10 = bin2bcd_h( month); //| |
rtc.Day = dayofweek; //| |
rtc.Year = bin2bcd_l( year - 2000); //| |
rtc.Year10 = bin2bcd_h( year - 2000); //| |
rtc.WP = 0; //| |
//| |
//+++++++++++++++++++++++++++++++ Write all clock data at once (burst mode) +++++++++++++++++++++++++++++//| |
DS1302_clock_burst_write( (uint8_t *) &rtc); //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// setDS1302Time Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//=======================================================================================================//| |
// //| |
// updateTime Function Begin //| |
// //| |
//=======================================================================================================//| |
// //| |
// A function that updates the time. All other functions that access the time after the initial set //| |
// will call this function for the actual updating time. This is the only function that updates the //| |
// time to the current data. //| |
// //| |
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//| |
void virtuabotixRTC::updateTime() { //| |
//| |
DS1302_clock_burst_read( (uint8_t *) &rtc); // Read all clock data at once (burst mode). //| |
//| |
char buffer[80]; // the code uses 70 characters. //| |
seconds = ( rtc.Seconds10 * 10 ) + rtc. Seconds; //| |
minutes = ( rtc. Minutes10 * 10 ) + rtc.Minutes; //| |
hours = ( rtc.h24.Hour10 * 10 ) + rtc.h24.Hour; //| |
dayofweek = ( rtc.Day ); //| |
dayofmonth = ( rtc.Date10 * 10 ) + rtc.Date; //| |
month = ( rtc.Month10 * 10 ) + rtc.Month; //| |
year = ( rtc.Year10 * 10 ) + rtc.Year + 2000; //| |
} //| |
//| |
//=======================================================================================================//| |
// //| |
// updateTime Function End //| |
// //| |
//=======================================================================================================//| |
// |
// |
//============================================================================================================//|
// //|
// virtuabotixRTC Class Functions End //|
// //|
//============================================================================================================//|