2 Copyright (C) Dean Camera, 2010.
4 dean [at] fourwalledcubicle [dot] com
5 www.fourwalledcubicle.com
10 void DS1307_Init(void)
12 // Nothing to initialize
15 void DS1307_SetDate(uint8_t Day
, uint8_t Month
, uint8_t Year
)
17 #if defined(DUMMY_RTC)
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);
29 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
))
31 TWI_SendByte(DS1307_DATEREG_START
);
32 TWI_SendByte(CurrentRTCDate
.Byte1
.IntVal
);
33 TWI_SendByte(CurrentRTCDate
.Byte2
.IntVal
);
34 TWI_SendByte(CurrentRTCDate
.Byte3
.IntVal
);
36 TWI_StopTransmission();
40 void DS1307_SetTime(uint8_t Hour
, uint8_t Minute
, uint8_t Second
)
42 #if defined(DUMMY_RTC)
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;
56 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
))
58 TWI_SendByte(DS1307_TIMEREG_START
);
59 TWI_SendByte(CurrentRTCTime
.Byte1
.IntVal
);
60 TWI_SendByte(CurrentRTCTime
.Byte2
.IntVal
);
61 TWI_SendByte(CurrentRTCTime
.Byte3
.IntVal
);
63 TWI_StopTransmission();
67 void DS1307_GetDate(uint8_t* Day
, uint8_t* Month
, uint8_t* Year
)
69 #if defined(DUMMY_RTC)
76 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
))
78 TWI_SendByte(DS1307_DATEREG_START
);
80 TWI_StopTransmission();
83 DS1307_DateRegs_t CurrentRTCDate
;
85 if (TWI_StartTransmission(DS1307_ADDRESS_READ
))
87 TWI_ReceiveByte(&CurrentRTCDate
.Byte1
.IntVal
, false);
88 TWI_ReceiveByte(&CurrentRTCDate
.Byte2
.IntVal
, false);
89 TWI_ReceiveByte(&CurrentRTCDate
.Byte3
.IntVal
, true);
91 TWI_StopTransmission();
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
;
99 void DS1307_GetTime(uint8_t* Hour
, uint8_t* Minute
, uint8_t* Second
)
101 #if defined(DUMMY_RTC)
108 if (TWI_StartTransmission(DS1307_ADDRESS_WRITE
))
110 TWI_SendByte(DS1307_TIMEREG_START
);
112 TWI_StopTransmission();
115 DS1307_TimeRegs_t CurrentRTCTime
;
117 if (TWI_StartTransmission(DS1307_ADDRESS_READ
))
119 TWI_ReceiveByte(&CurrentRTCTime
.Byte1
.IntVal
, false);
120 TWI_ReceiveByte(&CurrentRTCTime
.Byte2
.IntVal
, false);
121 TWI_ReceiveByte(&CurrentRTCTime
.Byte3
.IntVal
, true);
123 TWI_StopTransmission();
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
;