@@ -189,43 +189,31 @@ uint8_t DS1307::getHours()
189
189
190
190
void DS1307::setHours (uint8_t hours)
191
191
{
192
- bool reg_hour, h_mode;
192
+ uint8_t reg_hour;
193
+ bool h_mode, meridiem;
193
194
194
195
reg_hour = _read_one_register (R_HOURS);
195
196
h_mode = bitRead (reg_hour, 6 ); // Get hour mode
196
197
197
198
if (hours >= 00 && hours <= 23 )
198
199
{
199
- h_mode = getHourMode ();
200
+ // h_mode = getHourMode();
200
201
201
202
if (h_mode == CLOCK_H24)
202
203
{
203
204
_write_one_register (R_HOURS,bin2bcd (hours));
204
205
}
205
206
else if (h_mode == CLOCK_H12)
206
207
{
207
- if (hours < 12 )
208
- {
209
- hours = bin2bcd (hours);
210
- bitSet (hours, 6 );
211
- bitClear (hours, 5 ); // AM
212
- _write_one_register (R_HOURS,hours);
213
- }
214
- else if (hours == 12 )
215
- {
216
- hours = bin2bcd (hours);
217
- bitSet (hours, 6 ); // Set to 12 Hour mode again
218
- bitSet (hours, 5 ); // PM
219
- _write_one_register (R_HOURS,hours);
220
- }
221
- else if (hours > 12 )
222
- {
223
- hours = hours % 12 ;
224
- hours = bin2bcd (hours);
225
- bitSet (hours, 6 ); // Set to 12 Hour mode again
226
- bitSet (hours, 5 ); // PM
227
- _write_one_register (R_HOURS,hours);
228
- }
208
+ meridiem = (hours >= 12 );
209
+ if (hours == 0 )
210
+ hours = 12 ;
211
+ if (hours > 12 )
212
+ hours -= 12 ;
213
+ hours = bin2bcd (hours);
214
+ bitWrite (hours, 5 , meridiem);
215
+ bitSet (hours, 6 );
216
+ _write_one_register (R_HOURS,hours);
229
217
}
230
218
}
231
219
}
@@ -388,6 +376,7 @@ void DS1307::setDateTime(String date, String time)
388
376
{
389
377
uint8_t reg_hours, reg_seconds, h_mode, ch_bit;
390
378
uint8_t day, month, hours, minutes, seconds, year;
379
+ bool meridiem;
391
380
392
381
reg_hours = _read_one_register (R_HOURS);
393
382
reg_seconds = _read_one_register (R_SECONDS);
@@ -430,28 +419,15 @@ void DS1307::setDateTime(String date, String time)
430
419
}
431
420
else if (h_mode == CLOCK_H12)
432
421
{
433
- if (hours < 12 )
434
- {
435
- hours = bin2bcd (hours);
436
- bitSet (hours, 6 );
437
- bitClear (hours, 5 ); // AM
438
- Wire.write (hours);
439
- }
440
- else if (hours == 12 )
441
- {
442
- hours = bin2bcd (hours);
443
- bitSet (hours, 6 ); // Set to 12 Hour mode again
444
- bitSet (hours, 5 ); // PM
445
- Wire.write (hours);
446
- }
447
- else if (hours > 12 )
448
- {
449
- hours = hours % 12 ;
450
- hours = bin2bcd (hours);
451
- bitSet (hours, 6 ); // Set to 12 Hour mode again
452
- bitSet (hours, 5 ); // PM
453
- Wire.write (hours);
454
- }
422
+ meridiem = (hours >= 12 );
423
+ if (hours == 0 )
424
+ hours = 12 ;
425
+ if (hours > 12 )
426
+ hours -= 12 ;
427
+ hours = bin2bcd (hours);
428
+ bitWrite (hours, 5 , meridiem);
429
+ bitSet (hours, 6 );
430
+ Wire.write (hours);
455
431
}
456
432
457
433
Wire.write (calculateDayOfWeek (day, month, year));
@@ -474,6 +450,7 @@ void DS1307::setDateTime(String timestamp)
474
450
uint8_t reg_hours, reg_seconds, h_mode, ch_bit;
475
451
uint8_t day, month, hours, week, minutes, seconds, year;
476
452
String month_str, week_str;
453
+ bool meridiem;
477
454
478
455
reg_hours = _read_one_register (R_HOURS);
479
456
reg_seconds = _read_one_register (R_SECONDS);
@@ -524,36 +501,21 @@ void DS1307::setDateTime(String timestamp)
524
501
}
525
502
else if (h_mode == CLOCK_H12)
526
503
{
527
- if (hours < 12 )
528
- {
529
- hours = bin2bcd (hours);
530
- bitSet (hours, 6 );
531
- bitClear (hours, 5 ); // AM
532
- Wire.write (hours);
533
- }
534
- else if (hours == 12 )
535
- {
536
- hours = bin2bcd (hours);
537
- bitSet (hours, 6 ); // Set to 12 Hour mode again
538
- bitSet (hours, 5 ); // PM
539
- Wire.write (hours);
540
- }
541
- else if (hours > 12 )
542
- {
543
- hours = hours % 12 ;
544
- hours = bin2bcd (hours);
545
- bitSet (hours, 6 ); // Set to 12 Hour mode again
546
- bitSet (hours, 5 ); // PM
547
- Wire.write (hours);
548
- }
504
+ meridiem = (hours >= 12 );
505
+ if (hours == 0 )
506
+ hours = 12 ;
507
+ if (hours > 12 )
508
+ hours -= 12 ;
509
+ hours = bin2bcd (hours);
510
+ bitWrite (hours, 5 , meridiem);
511
+ bitSet (hours, 6 );
512
+ Wire.write (hours);
549
513
}
550
-
551
514
Wire.write (week);
552
515
Wire.write (bin2bcd (day));
553
516
Wire.write (bin2bcd (month));
554
517
Wire.write (bin2bcd (year));
555
518
Wire.endTransmission ();
556
-
557
519
}
558
520
559
521
tm DS1307::getDateTime ()
@@ -614,6 +576,7 @@ void DS1307::setEpoch(time_t epoch)
614
576
struct tm epoch_tm, * ptr_epoch_tm;
615
577
uint8_t reg_hours, reg_seconds, h_mode, ch_bit;
616
578
uint8_t day, month, hours, week, minutes, seconds, year;
579
+ bool meridiem;
617
580
618
581
if (epoch >= UNIX_OFFSET)
619
582
{
@@ -662,28 +625,15 @@ void DS1307::setEpoch(time_t epoch)
662
625
}
663
626
else if (h_mode == CLOCK_H12)
664
627
{
665
- if (hours < 12 )
666
- {
667
- hours = bin2bcd (hours);
668
- bitSet (hours, 6 );
669
- bitClear (hours, 5 ); // AM
670
- Wire.write (hours);
671
- }
672
- else if (hours == 12 )
673
- {
674
- hours = bin2bcd (hours);
675
- bitSet (hours, 6 ); // Set to 12 Hour mode again
676
- bitSet (hours, 5 ); // PM
677
- Wire.write (hours);
678
- }
679
- else if (hours > 12 )
680
- {
681
- hours = hours % 12 ;
682
- hours = bin2bcd (hours);
683
- bitSet (hours, 6 ); // Set to 12 Hour mode again
684
- bitSet (hours, 5 ); // PM
685
- Wire.write (hours);
686
- }
628
+ meridiem = (hours >= 12 );
629
+ if (hours == 0 )
630
+ hours = 12 ;
631
+ if (hours > 12 )
632
+ hours -= 12 ;
633
+ hours = bin2bcd (hours);
634
+ bitWrite (hours, 5 , meridiem);
635
+ bitSet (hours, 6 );
636
+ Wire.write (hours);
687
637
}
688
638
689
639
Wire.write (week);
@@ -925,6 +875,127 @@ String DS1307::getDateTimeString()
925
875
return (dateTimeString);
926
876
}
927
877
878
+ String DS1307::getTimeString ()
879
+ {
880
+ uint8_t seconds, minutes, hours, meridiem, h_mode;
881
+ uint16_t year;
882
+ String timeString;
883
+
884
+ Wire.beginTransmission (DS1307_ADDR);
885
+ Wire.write (R_SECONDS);
886
+ Wire.endTransmission ();
887
+ Wire.requestFrom (DS1307_ADDR, 3 );
888
+
889
+ seconds = Wire.read ();
890
+ bitClear (seconds, 3 );
891
+ seconds = bcd2bin (seconds);
892
+
893
+ minutes = Wire.read ();
894
+ minutes = bcd2bin (minutes);
895
+
896
+ hours = Wire.read ();
897
+ h_mode = bitRead (hours, 6 );
898
+ meridiem = bitRead (hours, 5 );
899
+
900
+ if (h_mode == CLOCK_H24)
901
+ {
902
+ hours = bcd2bin (hours);
903
+ }
904
+ else // h_mode == CLOCK_H12
905
+ {
906
+ bitClear (hours, 5 );
907
+ bitClear (hours, 6 );
908
+ hours = bcd2bin (hours);
909
+ }
910
+
911
+ if (hours<10 )
912
+ timeString.concat (" 0" );
913
+ timeString.concat (hours);
914
+ timeString.concat (" :" );
915
+ if (minutes<10 )
916
+ timeString.concat (" 0" );
917
+ timeString.concat (minutes);
918
+ timeString.concat (" :" );
919
+ if (seconds<10 )
920
+ timeString.concat (" 0" );
921
+ timeString.concat (seconds);
922
+ if (h_mode == CLOCK_H12)
923
+ {
924
+ switch (meridiem)
925
+ {
926
+ case HOUR_AM :
927
+ timeString.concat (" AM" );
928
+ break ;
929
+ case HOUR_PM :
930
+ timeString.concat (" PM" );
931
+ break ;
932
+ }
933
+ }
934
+ return (timeString);
935
+ }
936
+
937
+ String DS1307::getDateString ()
938
+ {
939
+ uint8_t week, day, month, meridiem, h_mode;
940
+ uint16_t year;
941
+ String dateString;
942
+
943
+ Wire.beginTransmission (DS1307_ADDR);
944
+ Wire.write (R_WEEKDAY);
945
+ Wire.endTransmission ();
946
+ Wire.requestFrom (DS1307_ADDR, 7 );
947
+
948
+ week = Wire.read ();
949
+ week = week;
950
+
951
+ day = Wire.read ();
952
+ day = bcd2bin (day);
953
+
954
+ month = Wire.read ();
955
+ month = bcd2bin (month);
956
+
957
+ year = Wire.read ();
958
+ year = bcd2bin (year) + 2000 ;
959
+
960
+ switch (week)
961
+ {
962
+ case 1 :
963
+ dateString.concat (" SUN" );
964
+ break ;
965
+ case 2 :
966
+ dateString.concat (" MON" );
967
+ break ;
968
+ case 3 :
969
+ dateString.concat (" TUE" );
970
+ break ;
971
+ case 4 :
972
+ dateString.concat (" WED" );
973
+ break ;
974
+ case 5 :
975
+ dateString.concat (" THU" );
976
+ break ;
977
+ case 6 :
978
+ dateString.concat (" FRI" );
979
+ break ;
980
+ case 7 :
981
+ dateString.concat (" SAT" );
982
+ break ;
983
+ }
984
+
985
+ dateString.concat (" " );
986
+ if (day<10 )
987
+ dateString.concat (" 0" );
988
+ dateString.concat (day);
989
+ dateString.concat (" -" );
990
+ if (month<10 )
991
+ dateString.concat (" 0" );
992
+ dateString.concat (month);
993
+ dateString.concat (" -" );
994
+ dateString.concat (year);
995
+
996
+ return (dateString);
997
+ }
998
+
928
999
/* NVRAM Functions */
929
1000
930
1001
bool NVRAM::begin ()
0 commit comments