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_DateTimeRegs_t NewRegValues
;
18 // Convert new time data to the DS1307's time register layout
19 NewRegValues
.Byte1
.Fields
.TenSec
= (NewTimeDate
->Second
/ 10);
20 NewRegValues
.Byte1
.Fields
.Sec
= (NewTimeDate
->Second
% 10);
21 NewRegValues
.Byte1
.Fields
.CH
= false;
22 NewRegValues
.Byte2
.Fields
.TenMin
= (NewTimeDate
->Minute
/ 10);
23 NewRegValues
.Byte2
.Fields
.Min
= (NewTimeDate
->Minute
% 10);
24 NewRegValues
.Byte3
.Fields
.TenHour
= (NewTimeDate
->Hour
/ 10);
25 NewRegValues
.Byte3
.Fields
.Hour
= (NewTimeDate
->Hour
% 10);
26 NewRegValues
.Byte3
.Fields
.TwelveHourMode
= false;
28 // Convert new date data to the DS1307's date register layout
29 NewRegValues
.Byte4
.Fields
.DayOfWeek
= 0;
30 NewRegValues
.Byte5
.Fields
.TenDay
= (NewTimeDate
->Day
/ 10);
31 NewRegValues
.Byte5
.Fields
.Day
= (NewTimeDate
->Day
% 10);
32 NewRegValues
.Byte6
.Fields
.TenMonth
= (NewTimeDate
->Month
/ 10);
33 NewRegValues
.Byte6
.Fields
.Month
= (NewTimeDate
->Month
% 10);
34 NewRegValues
.Byte7
.Fields
.TenYear
= (NewTimeDate
->Year
/ 10);
35 NewRegValues
.Byte7
.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(NewRegValues
.Byte1
.IntVal
);
44 TWI_SendByte(NewRegValues
.Byte2
.IntVal
);
45 TWI_SendByte(NewRegValues
.Byte3
.IntVal
);
47 // Write date data to the second set of device registers
48 TWI_SendByte(NewRegValues
.Byte4
.IntVal
);
49 TWI_SendByte(NewRegValues
.Byte5
.IntVal
);
50 TWI_SendByte(NewRegValues
.Byte6
.IntVal
);
51 TWI_SendByte(NewRegValues
.Byte7
.IntVal
);
53 TWI_StopTransmission();
57 void DS1307_GetTimeDate(TimeDate_t
* const TimeDate
)
59 #if defined(DUMMY_RTC)
71 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
, 10))
73 // Must start reading from the first address within the device
75 TWI_StopTransmission();
78 DS1307_DateTimeRegs_t CurrentRegValues
;
80 if (TWI_StartTransmission(DS1307_ADDRESS_READ
, 10))
82 // First set of registers store the current time
83 TWI_ReceiveByte(&CurrentRegValues
.Byte1
.IntVal
, false);
84 TWI_ReceiveByte(&CurrentRegValues
.Byte2
.IntVal
, false);
85 TWI_ReceiveByte(&CurrentRegValues
.Byte3
.IntVal
, false);
87 // Second set of registers store the current date
88 TWI_ReceiveByte(&CurrentRegValues
.Byte4
.IntVal
, false);
89 TWI_ReceiveByte(&CurrentRegValues
.Byte5
.IntVal
, false);
90 TWI_ReceiveByte(&CurrentRegValues
.Byte6
.IntVal
, false);
91 TWI_ReceiveByte(&CurrentRegValues
.Byte7
.IntVal
, true);
93 TWI_StopTransmission();
96 // Convert stored time value into decimal
97 TimeDate
->Second
= (CurrentRegValues
.Byte1
.Fields
.TenSec
* 10) + CurrentRegValues
.Byte1
.Fields
.Sec
;
98 TimeDate
->Minute
= (CurrentRegValues
.Byte2
.Fields
.TenMin
* 10) + CurrentRegValues
.Byte2
.Fields
.Min
;
99 TimeDate
->Hour
= (CurrentRegValues
.Byte3
.Fields
.TenHour
* 10) + CurrentRegValues
.Byte3
.Fields
.Hour
;
101 // Convert stored date value into decimal
102 TimeDate
->Day
= (CurrentRegValues
.Byte5
.Fields
.TenDay
* 10) + CurrentRegValues
.Byte5
.Fields
.Day
;
103 TimeDate
->Month
= (CurrentRegValues
.Byte6
.Fields
.TenMonth
* 10) + CurrentRegValues
.Byte6
.Fields
.Month
;
104 TimeDate
->Year
= (CurrentRegValues
.Byte7
.Fields
.TenYear
* 10) + CurrentRegValues
.Byte7
.Fields
.Year
;