2 Copyright (C) Dean Camera, 2011.
4 dean [at] fourwalledcubicle [dot] com
10 void DS1307_SetTimeDate(const TimeDate_t
* NewTimeDate
)
12 #if defined(DUMMY_RTC)
16 DS1307_TimeRegs_t CurrentRTCTime
;
17 DS1307_DateRegs_t CurrentRTCDate
;
19 // Convert new time data to the DS1307's time register layout
20 CurrentRTCTime
.Byte1
.Fields
.TenSec
= (NewTimeDate
->Second
/ 10);
21 CurrentRTCTime
.Byte1
.Fields
.Sec
= (NewTimeDate
->Second
% 10);
22 CurrentRTCTime
.Byte1
.Fields
.CH
= false;
23 CurrentRTCTime
.Byte2
.Fields
.TenMin
= (NewTimeDate
->Minute
/ 10);
24 CurrentRTCTime
.Byte2
.Fields
.Min
= (NewTimeDate
->Minute
% 10);
25 CurrentRTCTime
.Byte3
.Fields
.TenHour
= (NewTimeDate
->Hour
/ 10);
26 CurrentRTCTime
.Byte3
.Fields
.Hour
= (NewTimeDate
->Hour
% 10);
27 CurrentRTCTime
.Byte3
.Fields
.TwelveHourMode
= false;
29 // Convert new date data to the DS1307's date register layout
30 CurrentRTCDate
.Byte1
.Fields
.TenDay
= (NewTimeDate
->Day
/ 10);
31 CurrentRTCDate
.Byte1
.Fields
.Day
= (NewTimeDate
->Day
% 10);
32 CurrentRTCDate
.Byte2
.Fields
.TenMonth
= (NewTimeDate
->Month
/ 10);
33 CurrentRTCDate
.Byte2
.Fields
.Month
= (NewTimeDate
->Month
% 10);
34 CurrentRTCDate
.Byte3
.Fields
.TenYear
= (NewTimeDate
->Year
/ 10);
35 CurrentRTCDate
.Byte3
.Fields
.Year
= (NewTimeDate
->Year
% 10);
37 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
, 10))
39 // Must start writing to the first address within the device
42 // Write time data to the first set of device registers
43 TWI_SendByte(CurrentRTCTime
.Byte1
.IntVal
);
44 TWI_SendByte(CurrentRTCTime
.Byte2
.IntVal
);
45 TWI_SendByte(CurrentRTCTime
.Byte3
.IntVal
);
47 // Write date data to the second set of device registers
48 TWI_SendByte(CurrentRTCDate
.Byte1
.IntVal
);
49 TWI_SendByte(CurrentRTCDate
.Byte2
.IntVal
);
50 TWI_SendByte(CurrentRTCDate
.Byte3
.IntVal
);
52 TWI_StopTransmission();
56 void DS1307_GetTimeDate(TimeDate_t
* const TimeDate
)
58 #if defined(DUMMY_RTC)
70 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
, 10))
72 // Must start reading from the first address within the device
74 TWI_StopTransmission();
77 DS1307_TimeRegs_t CurrentRTCTime
;
78 DS1307_DateRegs_t CurrentRTCDate
;
80 if (TWI_StartTransmission(DS1307_ADDRESS_READ
, 10))
82 // First set of registers store the current time
83 TWI_ReceiveByte(&CurrentRTCTime
.Byte1
.IntVal
, false);
84 TWI_ReceiveByte(&CurrentRTCTime
.Byte2
.IntVal
, false);
85 TWI_ReceiveByte(&CurrentRTCTime
.Byte3
.IntVal
, false);
87 // Second set of registers store the current date
88 TWI_ReceiveByte(&CurrentRTCDate
.Byte1
.IntVal
, false);
89 TWI_ReceiveByte(&CurrentRTCDate
.Byte2
.IntVal
, false);
90 TWI_ReceiveByte(&CurrentRTCDate
.Byte3
.IntVal
, true);
92 TWI_StopTransmission();
95 // Convert stored time value into decimal
96 TimeDate
->Second
= (CurrentRTCTime
.Byte1
.Fields
.TenSec
* 10) + CurrentRTCTime
.Byte1
.Fields
.Sec
;
97 TimeDate
->Minute
= (CurrentRTCTime
.Byte2
.Fields
.TenMin
* 10) + CurrentRTCTime
.Byte2
.Fields
.Min
;
98 TimeDate
->Hour
= (CurrentRTCTime
.Byte3
.Fields
.TenHour
* 10) + CurrentRTCTime
.Byte3
.Fields
.Hour
;
100 // Convert stored date value into decimal
101 TimeDate
->Day
= (CurrentRTCDate
.Byte1
.Fields
.TenDay
* 10) + CurrentRTCDate
.Byte1
.Fields
.Day
;
102 TimeDate
->Month
= (CurrentRTCDate
.Byte2
.Fields
.TenMonth
* 10) + CurrentRTCDate
.Byte2
.Fields
.Month
;
103 TimeDate
->Year
= (CurrentRTCDate
.Byte3
.Fields
.TenYear
* 10) + CurrentRTCDate
.Byte3
.Fields
.Year
;