Manually pull PDI clock pin high for 1ms before disabling PDI mode in the AVRISP...
[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_Init(void)
11 {
12 // Nothing to initialize
13 }
14
15 void DS1307_SetDate(uint8_t Day, uint8_t Month, uint8_t Year)
16 {
17 #if defined(DUMMY_RTC)
18 return;
19 #endif
20
21 DS1307_DateRegs_t CurrentRTCDate;
22 CurrentRTCDate.Byte1.TenDay = (Day / 10);
23 CurrentRTCDate.Byte1.Day = (Day % 10);
24 CurrentRTCDate.Byte2.TenMonth = (Month / 10);
25 CurrentRTCDate.Byte2.Month = (Month % 10);
26 CurrentRTCDate.Byte3.TenYear = (Year / 10);
27 CurrentRTCDate.Byte3.Year = (Year % 10);
28
29 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
30 {
31 TWI_SendByte(DS1307_DATEREG_START);
32 TWI_SendByte(CurrentRTCDate.Byte1.IntVal);
33 TWI_SendByte(CurrentRTCDate.Byte2.IntVal);
34 TWI_SendByte(CurrentRTCDate.Byte3.IntVal);
35
36 TWI_StopTransmission();
37 }
38 }
39
40 void DS1307_SetTime(uint8_t Hour, uint8_t Minute, uint8_t Second)
41 {
42 #if defined(DUMMY_RTC)
43 return;
44 #endif
45
46 DS1307_TimeRegs_t CurrentRTCTime;
47 CurrentRTCTime.Byte1.TenSec = (Second / 10);
48 CurrentRTCTime.Byte1.Sec = (Second % 10);
49 CurrentRTCTime.Byte1.CH = false;
50 CurrentRTCTime.Byte2.TenMin = (Minute / 10);
51 CurrentRTCTime.Byte2.Min = (Minute % 10);
52 CurrentRTCTime.Byte3.TenHour = (Hour / 10);
53 CurrentRTCTime.Byte3.Hour = (Hour % 10);
54 CurrentRTCTime.Byte3.TwelveHourMode = false;
55
56 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
57 {
58 TWI_SendByte(DS1307_TIMEREG_START);
59 TWI_SendByte(CurrentRTCTime.Byte1.IntVal);
60 TWI_SendByte(CurrentRTCTime.Byte2.IntVal);
61 TWI_SendByte(CurrentRTCTime.Byte3.IntVal);
62
63 TWI_StopTransmission();
64 }
65 }
66
67 void DS1307_GetDate(uint8_t* Day, uint8_t* Month, uint8_t* Year)
68 {
69 #if defined(DUMMY_RTC)
70 *Day = 1;
71 *Month = 1;
72 *Year = 1;
73 return;
74 #endif
75
76 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
77 {
78 TWI_SendByte(DS1307_DATEREG_START);
79
80 TWI_StopTransmission();
81 }
82
83 DS1307_DateRegs_t CurrentRTCDate;
84
85 if (TWI_StartTransmission(DS1307_ADDRESS_READ))
86 {
87 TWI_ReceiveByte(&CurrentRTCDate.Byte1.IntVal, false);
88 TWI_ReceiveByte(&CurrentRTCDate.Byte2.IntVal, false);
89 TWI_ReceiveByte(&CurrentRTCDate.Byte3.IntVal, true);
90
91 TWI_StopTransmission();
92 }
93
94 *Day = (CurrentRTCDate.Byte1.TenDay * 10) + CurrentRTCDate.Byte1.Day;
95 *Month = (CurrentRTCDate.Byte2.TenMonth * 10) + CurrentRTCDate.Byte2.Month;
96 *Year = (CurrentRTCDate.Byte3.TenYear * 10) + CurrentRTCDate.Byte3.Year;
97 }
98
99 void DS1307_GetTime(uint8_t* Hour, uint8_t* Minute, uint8_t* Second)
100 {
101 #if defined(DUMMY_RTC)
102 *Hour = 1;
103 *Minute = 1;
104 *Second = 1;
105 return;
106 #endif
107
108 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE))
109 {
110 TWI_SendByte(DS1307_TIMEREG_START);
111
112 TWI_StopTransmission();
113 }
114
115 DS1307_TimeRegs_t CurrentRTCTime;
116
117 if (TWI_StartTransmission(DS1307_ADDRESS_READ))
118 {
119 TWI_ReceiveByte(&CurrentRTCTime.Byte1.IntVal, false);
120 TWI_ReceiveByte(&CurrentRTCTime.Byte2.IntVal, false);
121 TWI_ReceiveByte(&CurrentRTCTime.Byte3.IntVal, true);
122
123 TWI_StopTransmission();
124 }
125
126 *Second = (CurrentRTCTime.Byte1.TenSec * 10) + CurrentRTCTime.Byte1.Sec;
127 *Minute = (CurrentRTCTime.Byte2.TenMin * 10) + CurrentRTCTime.Byte2.Min;
128 *Hour = (CurrentRTCTime.Byte3.TenHour * 10) + CurrentRTCTime.Byte3.Hour;
129 }