@ -35,6 +35,8 @@ DS1337 RTC = DS1337();
# ifdef WIRE_LIB_SCAN_MOD
# ifdef WIRE_LIB_SCAN_MOD
int8_t DS1337 : : Init ( void )
int8_t DS1337 : : Init ( void )
{
{
delay ( 500 ) ; //Account for the crystal startup time
// Check address and returns false is there is an error
// Check address and returns false is there is an error
if ( Wire . checkAddress ( DS1337_WADDR ) ) {
if ( Wire . checkAddress ( DS1337_WADDR ) ) {
// Possibly set the default registers here
// Possibly set the default registers here
@ -42,17 +44,13 @@ int8_t DS1337::Init(void)
clockExists = true ;
clockExists = true ;
// Start the oscillator if need
// Start the oscillator if need
if ( getRegisterBit ( DS1337_SP , DS1337_SP_EOSC ) )
if ( getRegisterBit ( DS1337_SP , DS1337_SP_EOSC ) | | getRegisterBit ( DS1337_STATUS , DS1337_STATUS_OSF ) )
{
{
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_OSF ) )
clockStart ( ) ;
{
unsetRegister ( DS1337_STATUS , DS1337_STATUS_OSF ) ;
}
# if defined(DS1337_USE_ALARM_INTERRUPTS) && !defined(DS1337_USE_SQW_OUTPUT)
# if defined(DS1337_USE_ALARM_INTERRUPTS) && !defined(DS1337_USE_SQW_OUTPUT)
setRegister ( DS1337_SP , DS1337_SQW_INTCN ) ;
setRegister ( DS1337_SP , DS1337_SQW_INTCN ) ;
# endif
# endif
clockStart ( ) ;
}
}
return DS1337_WADDR ;
return DS1337_WADDR ;
@ -63,25 +61,17 @@ int8_t DS1337::Init(void)
# else
# else
int8_t DS1337 : : Init ( void )
int8_t DS1337 : : Init ( void )
{
{
// Account for the crystal power up!
delay ( 500 ) ; //Account for the crystal startup time
delay ( 250 ) ;
clockExists = true ;
clockExists = true ;
// Start the oscillator if need
// Start the oscillator if need
if ( getRegisterBit ( DS1337_SP , DS1337_SP_EOSC ) )
if ( getRegisterBit ( DS1337_SP , DS1337_SP_EOSC ) | | getRegisterBit ( DS1337_STATUS , DS1337_STATUS_OSF ) )
{
{
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_OSF ) )
{
unsetRegister ( DS1337_STATUS , DS1337_STATUS_OSF ) ;
}
clockStart ( ) ;
clockStart ( ) ;
return DS1337_WADDR ;
}
}
return - 1 ;
return DS1337_WADDR ;
}
}
# endif
# endif
@ -122,6 +112,15 @@ uint8_t DS1337::getRegister(uint8_t registerNumber)
return Wire . receive ( ) ;
return Wire . receive ( ) ;
}
}
void DS1337 : : clockStart ( void )
{
// Start the oscillator
unsetRegister ( DS1337_SP , DS1337_SP_EOSC ) ;
// Reset the status register
writeRegister ( DS1337_STATUS , B00000000 ) ;
}
void DS1337 : : clockRead ( void )
void DS1337 : : clockRead ( void )
{
{
if ( ! clockExists ) return ;
if ( ! clockExists ) return ;
@ -144,6 +143,9 @@ void DS1337::clockSave(void)
{
{
if ( ! clockExists ) return ;
if ( ! clockExists ) return ;
// Stop the clock
clockStop ( ) ;
Wire . beginTransmission ( DS1337_WADDR ) ;
Wire . beginTransmission ( DS1337_WADDR ) ;
Wire . send ( 0x00 ) ;
Wire . send ( 0x00 ) ;
@ -154,7 +156,8 @@ void DS1337::clockSave(void)
Wire . endTransmission ( ) ;
Wire . endTransmission ( ) ;
delay ( 10 ) ;
// Restart the oscillator
clockStart ( ) ;
}
}
void DS1337 : : clockGet ( uint16_t * rtc )
void DS1337 : : clockGet ( uint16_t * rtc )
@ -452,14 +455,8 @@ void DS1337::clockSetWithUTS(uint32_t unixTimeStamp, boolean correctedTime)
// Serial.print(tt, DEC);
// Serial.print(tt, DEC);
// SPrint(" ");
// SPrint(" ");
// Stop the clock
//clockStop(); // Uneeded with the ds1337
// Save buffer to the RTC
// Save buffer to the RTC
clockSave ( ) ;
clockSave ( ) ;
// Restart the oscillator
//clockStart(); // Uneeded with the ds1337
}
}
# endif
# endif
@ -498,13 +495,13 @@ void DS1337::alarmChecks(void)
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_A1F ) )
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_A1F ) )
{
{
unsetRegister ( DS1337_STATUS , DS1337_STATUS_A1F ) ; delay ( 10 ) ;
unsetRegister ( DS1337_STATUS , DS1337_STATUS_A1F ) ; delay ( 10 ) ;
if ( DS1337callbackFunc ) DS1337callbackFunc [ 0 ] ( ) ;
if ( DS1337callbackFunc [0 ] ) DS1337callbackFunc [ 0 ] ( ) ;
}
}
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_A2F ) )
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_A2F ) )
{
{
unsetRegister ( DS1337_STATUS , DS1337_STATUS_A2F ) ; delay ( 10 ) ;
unsetRegister ( DS1337_STATUS , DS1337_STATUS_A2F ) ; delay ( 10 ) ;
if ( DS1337callbackFunc ) DS1337callbackFunc [ 1 ] ( ) ;
if ( DS1337callbackFunc [1 ] ) DS1337callbackFunc [ 1 ] ( ) ;
}
}
return ;
return ;
@ -522,28 +519,28 @@ void DS1337::alarmSet(uint8_t timeSection, uint8_t timeValue)
case DS1337_SEC :
case DS1337_SEC :
if ( alarmId = = false & & timeValue < 60 )
if ( alarmId = = false & & timeValue < 60 )
{
{
rtc_alarm [ DS1337_SEC ] = binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK | ( rtc_alarm [ DS1337_SEC ] & DS1337_ALARM_MASK ) ;
rtc_alarm [ DS1337_SEC ] = ( binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK ) | ( rtc_alarm [ DS1337_SEC ] & DS1337_ALARM_MASK ) ;
}
}
break ;
break ;
case DS1337_MIN :
case DS1337_MIN :
if ( timeValue < 60 )
if ( timeValue < 60 )
{
{
rtc_alarm [ DS1337_MIN ] = binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK | ( rtc_alarm [ DS1337_MIN ] & DS1337_ALARM_MASK ) ;
rtc_alarm [ DS1337_MIN ] = ( binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK ) | ( rtc_alarm [ DS1337_MIN ] & DS1337_ALARM_MASK ) ;
}
}
break ;
break ;
case DS1337_HR :
case DS1337_HR :
if ( timeValue < 24 )
if ( timeValue < 24 )
{
{
rtc_alarm [ DS1337_HR ] = binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK | ( rtc_alarm [ DS1337_HR ] & DS1337_ALARM_MASK ) ;
rtc_alarm [ DS1337_HR ] = ( binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK ) | ( rtc_alarm [ DS1337_HR ] & DS1337_ALARM_MASK ) ;
}
}
break ;
break ;
case DS1337_DOW :
case DS1337_DOW :
if ( timeValue < 31 )
if ( timeValue < 31 )
{
{
rtc_alarm [ DS1337_DOW ] = binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK & ~ DS1337_ALARM_DT_MASK | ( rtc_alarm [ DS1337_DOW ] & DS1337_ALARM_MASK ) | ( rtc_alarm [ DS1337_DOW ] & DS1337_ALARM_DT_MASK ) ;
rtc_alarm [ DS1337_DOW ] = ( binToBcd ( timeValue ) & ~ DS1337_ALARM_MASK & ~ DS1337_ALARM_DT_MASK ) | ( rtc_alarm [ DS1337_DOW ] & DS1337_ALARM_MASK ) | ( rtc_alarm [ DS1337_DOW ] & DS1337_ALARM_DT_MASK ) ;
}
}
break ;
break ;
@ -552,15 +549,19 @@ void DS1337::alarmSet(uint8_t timeSection, uint8_t timeValue)
if ( alarmId ) {
if ( alarmId ) {
timeValue = ( timeValue > > 1 ) < < 1 ;
timeValue = ( timeValue > > 1 ) < < 1 ;
} else
} else
// A1M1
rtc_alarm [ DS1337_SEC ] = ( rtc_alarm [ DS1337_SEC ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 7 ;
rtc_alarm [ DS1337_SEC ] = ( rtc_alarm [ DS1337_SEC ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 7 ;
// AM2
rtc_alarm [ DS1337_MIN ] = ( rtc_alarm [ DS1337_MIN ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 6 ;
rtc_alarm [ DS1337_MIN ] = ( rtc_alarm [ DS1337_MIN ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 6 ;
// AM3
rtc_alarm [ DS1337_HR ] = ( rtc_alarm [ DS1337_HR ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 5 ;
rtc_alarm [ DS1337_HR ] = ( rtc_alarm [ DS1337_HR ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 5 ;
// AM4
rtc_alarm [ DS1337_DOW ] = ( rtc_alarm [ DS1337_DOW ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 4 ;
rtc_alarm [ DS1337_DOW ] = ( rtc_alarm [ DS1337_DOW ] & ~ DS1337_ALARM_MASK ) | DS1337_ALARM_MASK & timeValue < < 4 ;
if ( timeValue = = DS1337_ALARM_MCH_DOWHRMINSEC )
if ( timeValue = = DS1337_ALARM_MCH_DOWHRMINSEC | | timeValue = = DS1337_ALARM_MCH_DOWHRMIN )
{
{
rtc_alarm [ DS1337_DOW ] = rtc_alarm [ DS1337_DOW ] & ~ DS1337_ALARM_DT_MASK | DS1337_ALARM_DT_MASK ;
rtc_alarm [ DS1337_DOW ] = ( rtc_alarm [ DS1337_DOW ] & ~ DS1337_ALARM_DT_MASK ) | DS1337_ALARM_DT_MASK ;
}
}
}
}
@ -568,7 +569,7 @@ void DS1337::alarmSet(uint8_t timeSection, uint8_t timeValue)
case DS1337_ALARM_DT :
case DS1337_ALARM_DT :
{
{
rtc_alarm [ DS1337_DOW ] = rtc_alarm [ DS1337_DOW ] & ~ DS1337_ALARM_DT_MASK | ( timeValue = = 1 ? DS1337_ALARM_DT_MASK : 0 ) ;
rtc_alarm [ DS1337_DOW ] = ( rtc_alarm [ DS1337_DOW ] & ~ DS1337_ALARM_DT_MASK ) | ( timeValue = = 1 ? DS1337_ALARM_DT_MASK : 0 ) ;
}
}
break ;
break ;
} // end switch
} // end switch
@ -664,12 +665,9 @@ void DS1337::clockIntegrityCallback(void (*userFunc)(void))
void DS1337 : : clockChecks ( void )
void DS1337 : : clockChecks ( void )
{
{
# ifdef DS1337_USE_OSC_INTEGRITY
# ifdef DS1337_USE_OSC_INTEGRITY
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_OSF ) )
if ( getRegisterBit ( DS1337_STATUS , DS1337_STATUS_OSF ) & & ! getRegisterBit ( DS1337_SP , DS1337_SP_EOSC ) )
{
{
clockStop ( ) ;
clockStop ( ) ;
unsetRegister ( DS1337_STATUS , DS1337_STATUS_OSF ) ;
unsetRegister ( DS1337_STATUS , DS1337_STATUS_A1F ) ;
unsetRegister ( DS1337_STATUS , DS1337_STATUS_A2F ) ;
if ( DS1337callbackFunc [ 2 ] ) DS1337callbackFunc [ 2 ] ( ) ;
if ( DS1337callbackFunc [ 2 ] ) DS1337callbackFunc [ 2 ] ( ) ;