2 Copyright (C) Dean Camera, 2012.
4 dean [at] fourwalledcubicle [dot] com
10 bool DS1307_SetTimeDate(const TimeDate_t
* NewTimeDate
)
12 #if defined(DUMMY_RTC)
16 DS1307_DateTimeRegs_t NewRegValues
;
17 const uint8_t WriteAddress
= 0;
19 // Convert new time data to the DS1307's time register layout
20 NewRegValues
.Byte1
.Fields
.TenSec
= (NewTimeDate
->Second
/ 10);
21 NewRegValues
.Byte1
.Fields
.Sec
= (NewTimeDate
->Second
% 10);
22 NewRegValues
.Byte1
.Fields
.CH
= false;
23 NewRegValues
.Byte2
.Fields
.TenMin
= (NewTimeDate
->Minute
/ 10);
24 NewRegValues
.Byte2
.Fields
.Min
= (NewTimeDate
->Minute
% 10);
25 NewRegValues
.Byte3
.Fields
.TenHour
= (NewTimeDate
->Hour
/ 10);
26 NewRegValues
.Byte3
.Fields
.Hour
= (NewTimeDate
->Hour
% 10);
27 NewRegValues
.Byte3
.Fields
.TwelveHourMode
= false;
29 // Convert new date data to the DS1307's date register layout
30 NewRegValues
.Byte4
.Fields
.DayOfWeek
= 0;
31 NewRegValues
.Byte5
.Fields
.TenDay
= (NewTimeDate
->Day
/ 10);
32 NewRegValues
.Byte5
.Fields
.Day
= (NewTimeDate
->Day
% 10);
33 NewRegValues
.Byte6
.Fields
.TenMonth
= (NewTimeDate
->Month
/ 10);
34 NewRegValues
.Byte6
.Fields
.Month
= (NewTimeDate
->Month
% 10);
35 NewRegValues
.Byte7
.Fields
.TenYear
= (NewTimeDate
->Year
/ 10);
36 NewRegValues
.Byte7
.Fields
.Year
= (NewTimeDate
->Year
% 10);
38 // Write the new Time and Date into the DS1307
39 if (TWI_WritePacket(DS1307_ADDRESS
, 10, &WriteAddress
, sizeof(WriteAddress
),
40 (uint8_t*)&NewRegValues
, sizeof(DS1307_DateTimeRegs_t
)) != TWI_ERROR_NoError
)
48 bool DS1307_GetTimeDate(TimeDate_t
* const TimeDate
)
50 #if defined(DUMMY_RTC)
62 DS1307_DateTimeRegs_t CurrentRegValues
;
63 const uint8_t ReadAddress
= 0;
65 // Read in the stored Time and Date from the DS1307
66 if (TWI_ReadPacket(DS1307_ADDRESS
, 10, &ReadAddress
, sizeof(ReadAddress
),
67 (uint8_t*)&CurrentRegValues
, sizeof(DS1307_DateTimeRegs_t
)) != TWI_ERROR_NoError
)
72 // Convert stored time value into decimal
73 TimeDate
->Second
= (CurrentRegValues
.Byte1
.Fields
.TenSec
* 10) + CurrentRegValues
.Byte1
.Fields
.Sec
;
74 TimeDate
->Minute
= (CurrentRegValues
.Byte2
.Fields
.TenMin
* 10) + CurrentRegValues
.Byte2
.Fields
.Min
;
75 TimeDate
->Hour
= (CurrentRegValues
.Byte3
.Fields
.TenHour
* 10) + CurrentRegValues
.Byte3
.Fields
.Hour
;
77 // Convert stored date value into decimal
78 TimeDate
->Day
= (CurrentRegValues
.Byte5
.Fields
.TenDay
* 10) + CurrentRegValues
.Byte5
.Fields
.Day
;
79 TimeDate
->Month
= (CurrentRegValues
.Byte6
.Fields
.TenMonth
* 10) + CurrentRegValues
.Byte6
.Fields
.Month
;
80 TimeDate
->Year
= (CurrentRegValues
.Byte7
.Fields
.TenYear
* 10) + CurrentRegValues
.Byte7
.Fields
.Year
;