2 Copyright (C) Dean Camera, 2013.
4 dean [at] fourwalledcubicle [dot] com
10 #if defined(DUMMY_RTC)
12 /** Current dummy RTC time and date */
13 static volatile TimeDate_t DummyRTC_Count
;
17 DummyRTC_Count
.Hour
= 0;
18 DummyRTC_Count
.Minute
= 0;
19 DummyRTC_Count
.Second
= 0;
20 DummyRTC_Count
.Day
= 1;
21 DummyRTC_Count
.Month
= 1;
22 DummyRTC_Count
.Year
= 00;
25 void RTC_Tick500ms(void)
27 static bool HalfSecondElapsed
= false;
29 HalfSecondElapsed
= !HalfSecondElapsed
;
30 if (HalfSecondElapsed
== false)
33 if (++DummyRTC_Count
.Second
< 60)
36 DummyRTC_Count
.Second
= 0;
38 if (++DummyRTC_Count
.Minute
< 60)
41 DummyRTC_Count
.Minute
= 0;
43 if (++DummyRTC_Count
.Hour
< 24)
46 DummyRTC_Count
.Hour
= 0;
48 static const char MonthLength
[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
49 uint8_t DaysInMonth
= MonthLength
[DummyRTC_Count
.Month
- 1];
51 /* Check if we need to account for a leap year */
52 if ((DummyRTC_Count
.Month
== 2) &&
53 ((!(DummyRTC_Count
.Year
% 400)) || ((DummyRTC_Count
.Year
% 100) && !(DummyRTC_Count
.Year
% 4))))
58 if (++DummyRTC_Count
.Day
<= DaysInMonth
)
61 DummyRTC_Count
.Day
= 1;
63 if (++DummyRTC_Count
.Month
<= 12)
66 DummyRTC_Count
.Month
= 1;
67 DummyRTC_Count
.Year
++;
70 bool RTC_SetTimeDate(const TimeDate_t
* NewTimeDate
)
72 GlobalInterruptDisable();
73 DummyRTC_Count
= *NewTimeDate
;
74 GlobalInterruptEnable();
79 bool RTC_GetTimeDate(TimeDate_t
* const TimeDate
)
81 GlobalInterruptDisable();
82 *TimeDate
= DummyRTC_Count
;
83 GlobalInterruptEnable();
92 /* Unused for a real external DS1307 RTC device */
95 void RTC_Tick500ms(void)
97 /* Unused for a real external DS1307 RTC device */
100 bool RTC_SetTimeDate(const TimeDate_t
* NewTimeDate
)
102 DS1307_DateTimeRegs_t NewRegValues
;
103 const uint8_t WriteAddress
= 0;
105 // Convert new time data to the DS1307's time register layout
106 NewRegValues
.Byte1
.Fields
.TenSec
= (NewTimeDate
->Second
/ 10);
107 NewRegValues
.Byte1
.Fields
.Sec
= (NewTimeDate
->Second
% 10);
108 NewRegValues
.Byte1
.Fields
.CH
= false;
109 NewRegValues
.Byte2
.Fields
.TenMin
= (NewTimeDate
->Minute
/ 10);
110 NewRegValues
.Byte2
.Fields
.Min
= (NewTimeDate
->Minute
% 10);
111 NewRegValues
.Byte3
.Fields
.TenHour
= (NewTimeDate
->Hour
/ 10);
112 NewRegValues
.Byte3
.Fields
.Hour
= (NewTimeDate
->Hour
% 10);
113 NewRegValues
.Byte3
.Fields
.TwelveHourMode
= false;
115 // Convert new date data to the DS1307's date register layout
116 NewRegValues
.Byte4
.Fields
.DayOfWeek
= 0;
117 NewRegValues
.Byte5
.Fields
.TenDay
= (NewTimeDate
->Day
/ 10);
118 NewRegValues
.Byte5
.Fields
.Day
= (NewTimeDate
->Day
% 10);
119 NewRegValues
.Byte6
.Fields
.TenMonth
= (NewTimeDate
->Month
/ 10);
120 NewRegValues
.Byte6
.Fields
.Month
= (NewTimeDate
->Month
% 10);
121 NewRegValues
.Byte7
.Fields
.TenYear
= (NewTimeDate
->Year
/ 10);
122 NewRegValues
.Byte7
.Fields
.Year
= (NewTimeDate
->Year
% 10);
124 // Write the new Time and Date into the DS1307
125 if (TWI_WritePacket(DS1307_ADDRESS
, 10, &WriteAddress
, sizeof(WriteAddress
),
126 (uint8_t*)&NewRegValues
, sizeof(DS1307_DateTimeRegs_t
)) != TWI_ERROR_NoError
)
134 bool RTC_GetTimeDate(TimeDate_t
* const TimeDate
)
136 DS1307_DateTimeRegs_t CurrentRegValues
;
137 const uint8_t ReadAddress
= 0;
139 // Read in the stored Time and Date from the DS1307
140 if (TWI_ReadPacket(DS1307_ADDRESS
, 10, &ReadAddress
, sizeof(ReadAddress
),
141 (uint8_t*)&CurrentRegValues
, sizeof(DS1307_DateTimeRegs_t
)) != TWI_ERROR_NoError
)
146 // Convert stored time value into decimal
147 TimeDate
->Second
= (CurrentRegValues
.Byte1
.Fields
.TenSec
* 10) + CurrentRegValues
.Byte1
.Fields
.Sec
;
148 TimeDate
->Minute
= (CurrentRegValues
.Byte2
.Fields
.TenMin
* 10) + CurrentRegValues
.Byte2
.Fields
.Min
;
149 TimeDate
->Hour
= (CurrentRegValues
.Byte3
.Fields
.TenHour
* 10) + CurrentRegValues
.Byte3
.Fields
.Hour
;
151 // Convert stored date value into decimal
152 TimeDate
->Day
= (CurrentRegValues
.Byte5
.Fields
.TenDay
* 10) + CurrentRegValues
.Byte5
.Fields
.Day
;
153 TimeDate
->Month
= (CurrentRegValues
.Byte6
.Fields
.TenMonth
* 10) + CurrentRegValues
.Byte6
.Fields
.Month
;
154 TimeDate
->Year
= (CurrentRegValues
.Byte7
.Fields
.TenYear
* 10) + CurrentRegValues
.Byte7
.Fields
.Year
;