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