Commit 9c7594e7 authored by Dean Camera's avatar Dean Camera
Browse files

Oops - The DS1307 contains an unused "DayOfWeek" register that must be...

Oops - The DS1307 contains an unused "DayOfWeek" register that must be accounted for in the new unified DS1307 Time/Day read and write routines.
parent 7776aa4e
......@@ -13,26 +13,26 @@ void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate)
return;
#endif
DS1307_TimeRegs_t CurrentRTCTime;
DS1307_DateRegs_t CurrentRTCDate;
DS1307_DateTimeRegs_t NewRegValues;
// Convert new time data to the DS1307's time register layout
CurrentRTCTime.Byte1.Fields.TenSec = (NewTimeDate->Second / 10);
CurrentRTCTime.Byte1.Fields.Sec = (NewTimeDate->Second % 10);
CurrentRTCTime.Byte1.Fields.CH = false;
CurrentRTCTime.Byte2.Fields.TenMin = (NewTimeDate->Minute / 10);
CurrentRTCTime.Byte2.Fields.Min = (NewTimeDate->Minute % 10);
CurrentRTCTime.Byte3.Fields.TenHour = (NewTimeDate->Hour / 10);
CurrentRTCTime.Byte3.Fields.Hour = (NewTimeDate->Hour % 10);
CurrentRTCTime.Byte3.Fields.TwelveHourMode = false;
NewRegValues.Byte1.Fields.TenSec = (NewTimeDate->Second / 10);
NewRegValues.Byte1.Fields.Sec = (NewTimeDate->Second % 10);
NewRegValues.Byte1.Fields.CH = false;
NewRegValues.Byte2.Fields.TenMin = (NewTimeDate->Minute / 10);
NewRegValues.Byte2.Fields.Min = (NewTimeDate->Minute % 10);
NewRegValues.Byte3.Fields.TenHour = (NewTimeDate->Hour / 10);
NewRegValues.Byte3.Fields.Hour = (NewTimeDate->Hour % 10);
NewRegValues.Byte3.Fields.TwelveHourMode = false;
// Convert new date data to the DS1307's date register layout
CurrentRTCDate.Byte1.Fields.TenDay = (NewTimeDate->Day / 10);
CurrentRTCDate.Byte1.Fields.Day = (NewTimeDate->Day % 10);
CurrentRTCDate.Byte2.Fields.TenMonth = (NewTimeDate->Month / 10);
CurrentRTCDate.Byte2.Fields.Month = (NewTimeDate->Month % 10);
CurrentRTCDate.Byte3.Fields.TenYear = (NewTimeDate->Year / 10);
CurrentRTCDate.Byte3.Fields.Year = (NewTimeDate->Year % 10);
NewRegValues.Byte4.Fields.DayOfWeek = 0;
NewRegValues.Byte5.Fields.TenDay = (NewTimeDate->Day / 10);
NewRegValues.Byte5.Fields.Day = (NewTimeDate->Day % 10);
NewRegValues.Byte6.Fields.TenMonth = (NewTimeDate->Month / 10);
NewRegValues.Byte6.Fields.Month = (NewTimeDate->Month % 10);
NewRegValues.Byte7.Fields.TenYear = (NewTimeDate->Year / 10);
NewRegValues.Byte7.Fields.Year = (NewTimeDate->Year % 10);
if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
{
......@@ -40,14 +40,15 @@ void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate)
TWI_SendByte(0);
// Write time data to the first set of device registers
TWI_SendByte(CurrentRTCTime.Byte1.IntVal);
TWI_SendByte(CurrentRTCTime.Byte2.IntVal);
TWI_SendByte(CurrentRTCTime.Byte3.IntVal);
TWI_SendByte(NewRegValues.Byte1.IntVal);
TWI_SendByte(NewRegValues.Byte2.IntVal);
TWI_SendByte(NewRegValues.Byte3.IntVal);
// Write date data to the second set of device registers
TWI_SendByte(CurrentRTCDate.Byte1.IntVal);
TWI_SendByte(CurrentRTCDate.Byte2.IntVal);
TWI_SendByte(CurrentRTCDate.Byte3.IntVal);
TWI_SendByte(NewRegValues.Byte4.IntVal);
TWI_SendByte(NewRegValues.Byte5.IntVal);
TWI_SendByte(NewRegValues.Byte6.IntVal);
TWI_SendByte(NewRegValues.Byte7.IntVal);
TWI_StopTransmission();
}
......@@ -74,32 +75,32 @@ void DS1307_GetTimeDate(TimeDate_t* const TimeDate)
TWI_StopTransmission();
}
DS1307_TimeRegs_t CurrentRTCTime;
DS1307_DateRegs_t CurrentRTCDate;
DS1307_DateTimeRegs_t CurrentRegValues;
if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10))
{
// First set of registers store the current time
TWI_ReceiveByte(&CurrentRTCTime.Byte1.IntVal, false);
TWI_ReceiveByte(&CurrentRTCTime.Byte2.IntVal, false);
TWI_ReceiveByte(&CurrentRTCTime.Byte3.IntVal, false);
TWI_ReceiveByte(&CurrentRegValues.Byte1.IntVal, false);
TWI_ReceiveByte(&CurrentRegValues.Byte2.IntVal, false);
TWI_ReceiveByte(&CurrentRegValues.Byte3.IntVal, false);
// Second set of registers store the current date
TWI_ReceiveByte(&CurrentRTCDate.Byte1.IntVal, false);
TWI_ReceiveByte(&CurrentRTCDate.Byte2.IntVal, false);
TWI_ReceiveByte(&CurrentRTCDate.Byte3.IntVal, true);
TWI_ReceiveByte(&CurrentRegValues.Byte4.IntVal, false);
TWI_ReceiveByte(&CurrentRegValues.Byte5.IntVal, false);
TWI_ReceiveByte(&CurrentRegValues.Byte6.IntVal, false);
TWI_ReceiveByte(&CurrentRegValues.Byte7.IntVal, true);
TWI_StopTransmission();
}
// Convert stored time value into decimal
TimeDate->Second = (CurrentRTCTime.Byte1.Fields.TenSec * 10) + CurrentRTCTime.Byte1.Fields.Sec;
TimeDate->Minute = (CurrentRTCTime.Byte2.Fields.TenMin * 10) + CurrentRTCTime.Byte2.Fields.Min;
TimeDate->Hour = (CurrentRTCTime.Byte3.Fields.TenHour * 10) + CurrentRTCTime.Byte3.Fields.Hour;
TimeDate->Second = (CurrentRegValues.Byte1.Fields.TenSec * 10) + CurrentRegValues.Byte1.Fields.Sec;
TimeDate->Minute = (CurrentRegValues.Byte2.Fields.TenMin * 10) + CurrentRegValues.Byte2.Fields.Min;
TimeDate->Hour = (CurrentRegValues.Byte3.Fields.TenHour * 10) + CurrentRegValues.Byte3.Fields.Hour;
// Convert stored date value into decimal
TimeDate->Day = (CurrentRTCDate.Byte1.Fields.TenDay * 10) + CurrentRTCDate.Byte1.Fields.Day;
TimeDate->Month = (CurrentRTCDate.Byte2.Fields.TenMonth * 10) + CurrentRTCDate.Byte2.Fields.Month;
TimeDate->Year = (CurrentRTCDate.Byte3.Fields.TenYear * 10) + CurrentRTCDate.Byte3.Fields.Year;
TimeDate->Day = (CurrentRegValues.Byte5.Fields.TenDay * 10) + CurrentRegValues.Byte5.Fields.Day;
TimeDate->Month = (CurrentRegValues.Byte6.Fields.TenMonth * 10) + CurrentRegValues.Byte6.Fields.Month;
TimeDate->Year = (CurrentRegValues.Byte7.Fields.TenYear * 10) + CurrentRegValues.Byte7.Fields.Year;
}
......@@ -62,10 +62,18 @@
uint8_t IntVal;
} Byte3;
} DS1307_TimeRegs_t;
typedef struct
{
union
{
struct
{
unsigned int DayOfWeek : 3;
unsigned int Reserved : 5;
} Fields;
uint8_t IntVal;
} Byte4;
union
{
struct
......@@ -76,7 +84,7 @@
} Fields;
uint8_t IntVal;
} Byte1;
} Byte5;
union
{
......@@ -88,7 +96,7 @@
} Fields;
uint8_t IntVal;
} Byte2;
} Byte6;
union
{
......@@ -99,8 +107,8 @@
} Fields;
uint8_t IntVal;
} Byte3;
} DS1307_DateRegs_t;
} Byte7;
} DS1307_DateTimeRegs_t;
/* Macros: */
#define DS1307_ADDRESS_READ (0xD0 | TWI_ADDRESS_READ)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment