Changed over www.fourwalledcubicle.com links to the new www.lufa-lib.org redirect...
[pub/USBasp.git] / Projects / TempDataLogger / Lib / DS1307.c
1 /*
2 Copyright (C) Dean Camera, 2010.
3
4 dean [at] fourwalledcubicle [dot] com
5 www.lufa-lib.org
6 */
7
8 #include "DS1307.h"
9
10 void DS1307_SetDate(const uint8_t Day,
11 const uint8_t Month,
12 const uint8_t Year)
13 {
14 #if defined(DUMMY_RTC)
15 return;
16 #endif
17
18 DS1307_DateRegs_t CurrentRTCDate;
19 CurrentRTCDate.Byte1.Fields.TenDay = (Day / 10);
20 CurrentRTCDate.Byte1.Fields.Day = (Day % 10);
21 CurrentRTCDate.Byte2.Fields.TenMonth = (Month / 10);
22 CurrentRTCDate.Byte2.Fields.Month = (Month % 10);
23 CurrentRTCDate.Byte3.Fields.TenYear = (Year / 10);
24 CurrentRTCDate.Byte3.Fields.Year = (Year % 10);
25
26 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
27 {
28 TWI_SendByte(DS1307_DATEREG_START);
29 TWI_SendByte(CurrentRTCDate.Byte1.IntVal);
30 TWI_SendByte(CurrentRTCDate.Byte2.IntVal);
31 TWI_SendByte(CurrentRTCDate.Byte3.IntVal);
32
33 TWI_StopTransmission();
34 }
35 }
36
37 void DS1307_SetTime(const uint8_t Hour,
38 const uint8_t Minute,
39 const uint8_t Second)
40 {
41 #if defined(DUMMY_RTC)
42 return;
43 #endif
44
45 DS1307_TimeRegs_t CurrentRTCTime;
46 CurrentRTCTime.Byte1.Fields.TenSec = (Second / 10);
47 CurrentRTCTime.Byte1.Fields.Sec = (Second % 10);
48 CurrentRTCTime.Byte1.Fields.CH = false;
49 CurrentRTCTime.Byte2.Fields.TenMin = (Minute / 10);
50 CurrentRTCTime.Byte2.Fields.Min = (Minute % 10);
51 CurrentRTCTime.Byte3.Fields.TenHour = (Hour / 10);
52 CurrentRTCTime.Byte3.Fields.Hour = (Hour % 10);
53 CurrentRTCTime.Byte3.Fields.TwelveHourMode = false;
54
55 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
56 {
57 TWI_SendByte(DS1307_TIMEREG_START);
58 TWI_SendByte(CurrentRTCTime.Byte1.IntVal);
59 TWI_SendByte(CurrentRTCTime.Byte2.IntVal);
60 TWI_SendByte(CurrentRTCTime.Byte3.IntVal);
61
62 TWI_StopTransmission();
63 }
64 }
65
66 void DS1307_GetDate(uint8_t* const Day,
67 uint8_t* const Month,
68 uint8_t* const Year)
69 {
70 #if defined(DUMMY_RTC)
71 *Day = 1;
72 *Month = 1;
73 *Year = 1;
74 return;
75 #endif
76
77 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
78 {
79 TWI_SendByte(DS1307_DATEREG_START);
80
81 TWI_StopTransmission();
82 }
83
84 DS1307_DateRegs_t CurrentRTCDate;
85
86 if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10))
87 {
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 *Day = (CurrentRTCDate.Byte1.Fields.TenDay * 10) + CurrentRTCDate.Byte1.Fields.Day;
96 *Month = (CurrentRTCDate.Byte2.Fields.TenMonth * 10) + CurrentRTCDate.Byte2.Fields.Month;
97 *Year = (CurrentRTCDate.Byte3.Fields.TenYear * 10) + CurrentRTCDate.Byte3.Fields.Year;
98 }
99
100 void DS1307_GetTime(uint8_t* const Hour,
101 uint8_t* const Minute,
102 uint8_t* const Second)
103 {
104 #if defined(DUMMY_RTC)
105 *Hour = 1;
106 *Minute = 1;
107 *Second = 1;
108 return;
109 #endif
110
111 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
112 {
113 TWI_SendByte(DS1307_TIMEREG_START);
114
115 TWI_StopTransmission();
116 }
117
118 DS1307_TimeRegs_t CurrentRTCTime;
119
120 if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10))
121 {
122 TWI_ReceiveByte(&CurrentRTCTime.Byte1.IntVal, false);
123 TWI_ReceiveByte(&CurrentRTCTime.Byte2.IntVal, false);
124 TWI_ReceiveByte(&CurrentRTCTime.Byte3.IntVal, true);
125
126 TWI_StopTransmission();
127 }
128
129 *Second = (CurrentRTCTime.Byte1.Fields.TenSec * 10) + CurrentRTCTime.Byte1.Fields.Sec;
130 *Minute = (CurrentRTCTime.Byte2.Fields.TenMin * 10) + CurrentRTCTime.Byte2.Fields.Min;
131 *Hour = (CurrentRTCTime.Byte3.Fields.TenHour * 10) + CurrentRTCTime.Byte3.Fields.Hour;
132 }
133