X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/9c7594e7db5b06b2703a9975f2b250056ee0c212..a9abe1fdea5ccf67aaccab6fdde300d6fcf2d15a:/Projects/TempDataLogger/Lib/DS1307.c?ds=sidebyside diff --git a/Projects/TempDataLogger/Lib/DS1307.c b/Projects/TempDataLogger/Lib/DS1307.c index 40aba3204..459030f84 100644 --- a/Projects/TempDataLogger/Lib/DS1307.c +++ b/Projects/TempDataLogger/Lib/DS1307.c @@ -1,5 +1,5 @@ /* - Copyright (C) Dean Camera, 2011. + Copyright (C) Dean Camera, 2012. dean [at] fourwalledcubicle [dot] com www.lufa-lib.org @@ -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); @@ -33,64 +34,39 @@ void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate) 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)) + + // 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; TimeDate->Minute = 1; TimeDate->Second = 1; - + TimeDate->Day = 1; TimeDate->Month = 1; TimeDate->Year = 1; - - return; -#endif - if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10)) - { - // Must start reading from the first address within the device - TWI_SendByte(0); - TWI_StopTransmission(); - } + return true; +#endif DS1307_DateTimeRegs_t CurrentRegValues; - - if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10)) + const uint8_t ReadAddress = 0; + + // 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 @@ -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; }