X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/9c7594e7db5b06b2703a9975f2b250056ee0c212..6e7fc21a94f29e09b4bdce7c17cade5b1322bc7d:/Projects/TempDataLogger/Lib/DS1307.c diff --git a/Projects/TempDataLogger/Lib/DS1307.c b/Projects/TempDataLogger/Lib/DS1307.c index 40aba3204..2f84a18cf 100644 --- a/Projects/TempDataLogger/Lib/DS1307.c +++ b/Projects/TempDataLogger/Lib/DS1307.c @@ -7,13 +7,14 @@ #include "DS1307.h" -void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate) +bool DS1307_SetTimeDate(const TimeDate_t* NewTimeDate) { #if defined(DUMMY_RTC) - return; + return true; #endif DS1307_DateTimeRegs_t NewRegValues; + const uint8_t WriteAddress = 0; // Convert new time data to the DS1307's time register layout NewRegValues.Byte1.Fields.TenSec = (NewTimeDate->Second / 10); @@ -34,27 +35,17 @@ void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate) NewRegValues.Byte7.Fields.TenYear = (NewTimeDate->Year / 10); NewRegValues.Byte7.Fields.Year = (NewTimeDate->Year % 10); - if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10)) + // Write the new Time and Date into the DS1307 + if (TWI_WritePacket(DS1307_ADDRESS, 10, &WriteAddress, sizeof(WriteAddress), + (uint8_t*)&NewRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError) { - // Must start writing to the first address within the device - TWI_SendByte(0); - - // Write time data to the first set of device registers - 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(NewRegValues.Byte4.IntVal); - TWI_SendByte(NewRegValues.Byte5.IntVal); - TWI_SendByte(NewRegValues.Byte6.IntVal); - TWI_SendByte(NewRegValues.Byte7.IntVal); - - TWI_StopTransmission(); + return false; } + + return true; } -void DS1307_GetTimeDate(TimeDate_t* const TimeDate) +bool DS1307_GetTimeDate(TimeDate_t* const TimeDate) { #if defined(DUMMY_RTC) TimeDate->Hour = 1; @@ -65,34 +56,19 @@ void DS1307_GetTimeDate(TimeDate_t* const TimeDate) TimeDate->Month = 1; TimeDate->Year = 1; - return; + return true; #endif - if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10)) - { - // Must start reading from the first address within the device - TWI_SendByte(0); - TWI_StopTransmission(); - } - DS1307_DateTimeRegs_t CurrentRegValues; + const uint8_t ReadAddress = 0; - if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10)) + // Read in the stored Time and Date from the DS1307 + if (TWI_ReadPacket(DS1307_ADDRESS, 10, &ReadAddress, sizeof(ReadAddress), + (uint8_t*)&CurrentRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError) { - // First set of registers store the current time - 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(&CurrentRegValues.Byte4.IntVal, false); - TWI_ReceiveByte(&CurrentRegValues.Byte5.IntVal, false); - TWI_ReceiveByte(&CurrentRegValues.Byte6.IntVal, false); - TWI_ReceiveByte(&CurrentRegValues.Byte7.IntVal, true); - - TWI_StopTransmission(); + return false; } - + // Convert stored time value into decimal TimeDate->Second = (CurrentRegValues.Byte1.Fields.TenSec * 10) + CurrentRegValues.Byte1.Fields.Sec; TimeDate->Minute = (CurrentRegValues.Byte2.Fields.TenMin * 10) + CurrentRegValues.Byte2.Fields.Min; @@ -102,5 +78,7 @@ void DS1307_GetTimeDate(TimeDate_t* const TimeDate) 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; + + return true; }