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