Changed TempDataLogger project's DS1307 driver to simplify the function interface...
[pub/lufa.git] / Projects / TempDataLogger / Lib / DS1307.c
1 /*
2 Copyright (C) Dean Camera, 2011.
3
4 dean [at] fourwalledcubicle [dot] com
5 www.lufa-lib.org
6 */
7
8 #include "DS1307.h"
9
10 void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate)
11 {
12 #if defined(DUMMY_RTC)
13 return;
14 #endif
15
16 DS1307_TimeRegs_t CurrentRTCTime;
17 DS1307_DateRegs_t CurrentRTCDate;
18
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;
28
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);
36
37 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
38 {
39 // Must start writing to the first address within the device
40 TWI_SendByte(0);
41
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);
46
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);
51
52 TWI_StopTransmission();
53 }
54 }
55
56 void DS1307_GetTimeDate(TimeDate_t* const TimeDate)
57 {
58 #if defined(DUMMY_RTC)
59 TimeDate->Hour = 1;
60 TimeDate->Minute = 1;
61 TimeDate->Second = 1;
62
63 TimeDate->Day = 1;
64 TimeDate->Month = 1;
65 TimeDate->Year = 1;
66
67 return;
68 #endif
69
70 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
71 {
72 // Must start reading from the first address within the device
73 TWI_SendByte(0);
74 TWI_StopTransmission();
75 }
76
77 DS1307_TimeRegs_t CurrentRTCTime;
78 DS1307_DateRegs_t CurrentRTCDate;
79
80 if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10))
81 {
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);
86
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);
91
92 TWI_StopTransmission();
93 }
94
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;
99
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;
104 }
105