Added new high level TWI packet read/write commands, altered behaviour of the TWI_StartTransmission() function.
Spell check source code files.
This commit is contained in:
parent
9c7594e7db
commit
70d55f6e0c
13 changed files with 225 additions and 92 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue