Changeset 9 for ds1337

Show
Ignore:
Timestamp:
10/17/07 21:24:59 (15 months ago)
Author:
mlalondesvn
Message:

DS1337:

FIXED - All callback functions now work properly
FIXED - Oscillator startup so that it resets the status register
FIXED - All alarm mode bitmasks
MODIF - clockStart() from macro to function
MODIF - clockSave() to reset the clock so that the status flags are reset

twiLCD:

MODIF - setClrscreenCallback() renamed clrsSetCallback()

Location:
ds1337
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • ds1337/ds1337.cpp

    r7 r9  
    3636int8_t DS1337::Init(void) 
    3737{ 
     38    delay(500); //Account for the crystal startup time 
     39     
    3840    // Check address and returns false is there is an error 
    3941    if (Wire.checkAddress(DS1337_WADDR)) { 
     
    4345         
    4446        // Start the oscillator if need 
    45         if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC)) 
    46         { 
    47             if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) 
    48             { 
    49                 unsetRegister(DS1337_STATUS, DS1337_STATUS_OSF); 
    50             } 
     47        if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC) || getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) 
     48        { 
     49            clockStart(); 
     50             
    5151        #if defined(DS1337_USE_ALARM_INTERRUPTS) && !defined(DS1337_USE_SQW_OUTPUT) 
    5252            setRegister(DS1337_SP, DS1337_SQW_INTCN); 
    5353        #endif 
    54              
    55             clockStart(); 
    5654        } 
    5755         
     
    6462int8_t DS1337::Init(void) 
    6563{ 
    66     // Account for the crystal power up! 
    67     delay(250); 
     64    delay(500); //Account for the crystal startup time 
    6865     
    6966    clockExists = true; 
    7067     
    7168    // Start the oscillator if need 
    72     if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC)) 
    73     { 
    74         if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) 
    75         { 
    76             unsetRegister(DS1337_STATUS, DS1337_STATUS_OSF); 
    77         } 
    78          
     69    if (getRegisterBit(DS1337_SP, DS1337_SP_EOSC) || getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) 
     70    {    
    7971        clockStart(); 
    80          
    81         return DS1337_WADDR; 
    82     } 
    83      
    84     return -1; 
     72    } 
     73     
     74    return DS1337_WADDR; 
    8575} 
    8676#endif 
     
    123113} 
    124114 
     115void DS1337::clockStart(void) 
     116{ 
     117    // Start the oscillator 
     118    unsetRegister(DS1337_SP, DS1337_SP_EOSC); 
     119     
     120    // Reset the status register 
     121    writeRegister(DS1337_STATUS, B00000000); 
     122} 
     123 
    125124void DS1337::clockRead(void) 
    126125{ 
     
    145144    if (!clockExists) return; 
    146145     
     146    // Stop the clock 
     147    clockStop(); 
     148     
    147149    Wire.beginTransmission(DS1337_WADDR); 
    148150    Wire.send(0x00); 
     
    155157    Wire.endTransmission(); 
    156158     
    157     delay(10); 
     159    // Restart the oscillator 
     160    clockStart(); 
    158161} 
    159162 
     
    268271        break; 
    269272    } // end switch 
    270      
     273         
    271274    clockSave(); 
    272275} 
     
    453456    // SPrint(" "); 
    454457     
    455     // Stop the clock 
    456     //clockStop(); // Uneeded with the ds1337 
    457      
    458458    // Save buffer to the RTC 
    459459    clockSave(); 
    460      
    461     // Restart the oscillator 
    462     //clockStart(); // Uneeded with the ds1337 
    463460} 
    464461#endif 
     
    499496    { 
    500497        unsetRegister(DS1337_STATUS, DS1337_STATUS_A1F); delay(10); 
    501         if (DS1337callbackFunc) DS1337callbackFunc[0](); 
     498        if (DS1337callbackFunc[0]) DS1337callbackFunc[0](); 
    502499    } 
    503500     
     
    505502    { 
    506503        unsetRegister(DS1337_STATUS, DS1337_STATUS_A2F); delay(10); 
    507         if (DS1337callbackFunc) DS1337callbackFunc[1](); 
     504        if (DS1337callbackFunc[1]) DS1337callbackFunc[1](); 
    508505    } 
    509506     
     
    523520        if(alarmId == false && timeValue<60) 
    524521        { 
    525             rtc_alarm[DS1337_SEC]           = binToBcd(timeValue) & ~DS1337_ALARM_MASK | (rtc_alarm[DS1337_SEC] & DS1337_ALARM_MASK); 
     522            rtc_alarm[DS1337_SEC]           = (binToBcd(timeValue) & ~DS1337_ALARM_MASK) | (rtc_alarm[DS1337_SEC] & DS1337_ALARM_MASK); 
    526523        } 
    527524        break; 
     
    530527        if(timeValue < 60) 
    531528        { 
    532             rtc_alarm[DS1337_MIN]           = binToBcd(timeValue) & ~DS1337_ALARM_MASK | (rtc_alarm[DS1337_MIN] & DS1337_ALARM_MASK); 
     529            rtc_alarm[DS1337_MIN]           = (binToBcd(timeValue) & ~DS1337_ALARM_MASK) | (rtc_alarm[DS1337_MIN] & DS1337_ALARM_MASK); 
    533530        } 
    534531        break; 
     
    537534        if(timeValue < 24) 
    538535        { 
    539             rtc_alarm[DS1337_HR]            = binToBcd(timeValue) & ~DS1337_ALARM_MASK | (rtc_alarm[DS1337_HR] & DS1337_ALARM_MASK); 
     536            rtc_alarm[DS1337_HR]            = (binToBcd(timeValue) & ~DS1337_ALARM_MASK) | (rtc_alarm[DS1337_HR] & DS1337_ALARM_MASK); 
    540537        } 
    541538        break; 
     
    544541        if(timeValue < 31) 
    545542        { 
    546             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); 
     543            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); 
    547544        } 
    548545        break; 
     
    553550                timeValue                   = (timeValue>>1)<<1; 
    554551            } else 
     552                // A1M1 
    555553                rtc_alarm[DS1337_SEC]       = (rtc_alarm[DS1337_SEC] & ~DS1337_ALARM_MASK)  | DS1337_ALARM_MASK & timeValue<<7; 
    556554             
     555            // AM2 
    557556            rtc_alarm[DS1337_MIN]           = (rtc_alarm[DS1337_MIN] & ~DS1337_ALARM_MASK)  | DS1337_ALARM_MASK & timeValue<<6; 
     557            // AM3 
    558558            rtc_alarm[DS1337_HR]            = (rtc_alarm[DS1337_HR] & ~DS1337_ALARM_MASK)   | DS1337_ALARM_MASK & timeValue<<5; 
     559            // AM4 
    559560            rtc_alarm[DS1337_DOW]           = (rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_MASK)  | DS1337_ALARM_MASK & timeValue<<4; 
    560561             
    561             if (timeValue == DS1337_ALARM_MCH_DOWHRMINSEC) 
     562            if (timeValue == DS1337_ALARM_MCH_DOWHRMINSEC || timeValue == DS1337_ALARM_MCH_DOWHRMIN) 
    562563            { 
    563                 rtc_alarm[DS1337_DOW]       = rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK | DS1337_ALARM_DT_MASK; 
     564                rtc_alarm[DS1337_DOW]       = (rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK)   | DS1337_ALARM_DT_MASK; 
    564565            } 
    565566        } 
     
    569570        case DS1337_ALARM_DT: 
    570571        { 
    571             rtc_alarm[DS1337_DOW]           = rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK | (timeValue == 1 ? DS1337_ALARM_DT_MASK : 0); 
     572            rtc_alarm[DS1337_DOW]           = (rtc_alarm[DS1337_DOW] & ~DS1337_ALARM_DT_MASK) | (timeValue == 1 ? DS1337_ALARM_DT_MASK : 0); 
    572573        } 
    573574        break; 
     
    665666{ 
    666667#ifdef DS1337_USE_OSC_INTEGRITY 
    667     if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF)) 
     668    if (getRegisterBit(DS1337_STATUS, DS1337_STATUS_OSF) && !getRegisterBit(DS1337_SP, DS1337_SP_EOSC)) 
    668669    { 
    669670        clockStop(); 
    670         unsetRegister(DS1337_STATUS, DS1337_STATUS_OSF); 
    671         unsetRegister(DS1337_STATUS, DS1337_STATUS_A1F); 
    672         unsetRegister(DS1337_STATUS, DS1337_STATUS_A2F); 
    673671         
    674672        if (DS1337callbackFunc[2]) DS1337callbackFunc[2](); 
  • ds1337/ds1337.h

    r8 r9  
    9191#define DS1337_ALARM_DT_DATE            false 
    9292 
    93 #define DS1337_ALARM_PER_SEC            B00001111 
    94 #define DS1337_ALARM_PER_MIN            B00000111 /* Used for alarm 2 only*/ 
    95 #define DS1337_ALARM_MCH_SEC            B00001110 /* Used for alarm 1 only */ 
    96 #define DS1337_ALARM_MCH_MINSEC         B00001100 
    97 #define DS1337_ALARM_MCH_HRMINSEC       B00001000 /* Used for alarm 1 only */ 
    98 #define DS1337_ALARM_MCH_DATEHRMINSEC   B00000000 
    99 #define DS1337_ALARM_MCH_DOWHRMINSEC    B10000000 
     93#define DS1337_ALARM_PERA               B00001111 
     94#define DS1337_ALARM_PER_SEC            DS1337_ALARM_PERA   /* Used for alarm 1 only*/ 
     95#define DS1337_ALARM_PER_MIN            DS1337_ALARM_PERA   /* Used for alarm 2 only*/ 
     96 
     97#define DS1337_ALARM_MCH_SEC            B00001110           /* Used for alarm 1 only */ 
     98#define DS1337_ALARM_MCH_MIN            B00001100           /* Used for alarm 2 only */ 
     99 
     100#define DS1337_ALARM_MCH_MINSEC         B00001100           /* Used for alarm 1 only */ 
     101#define DS1337_ALARM_MCH_HRMIN          B00001000           /* Used for alarm 2 only */ 
     102 
     103#define DS1337_ALARM_MCH_HRMINSEC       B00001000           /* Used for alarm 1 only */ 
     104 
     105#define DS1337_ALARM_MCH_DATEHRMINSEC   B00000000           /* Used for alarm 1 only */ 
     106#define DS1337_ALARM_MCH_DATEHRMIN      B00000000           /* Used for alarm 2 only */ 
     107 
     108#define DS1337_ALARM_MCH_DOWHRMINSEC    B10000000           /* Used for alarm 1 only */ 
     109#define DS1337_ALARM_MCH_DOWHRMIN       B10000000           /* Used for alarm 2 only */ 
    100110 
    101111// Alarm mode masks 
     
    125135 * Macros 
    126136**/ 
    127 // Starts the oscillator and delay a little to account for long oscillator startup times 
    128 #define clockStart()                    unsetRegister(DS1337_SP, DS1337_SP_EOSC); delay(500); 
    129137#define clockStop()                     setRegister(DS1337_SP, DS1337_SP_EOSC) 
    130138 
     
    214222        **/ 
    215223        int8_t      Init(void); 
     224         
     225        /** 
     226         * clockStart: starts the oscillator and reset the fault flag 
     227        **/ 
     228        void        clockStart(void); 
    216229    #ifdef DS1337_USE_OSC_INTEGRITY 
    217230        /**