DS3231 setTime.ino

Formore Info: DS3231.cpp
/*H*******************************************************
DS3231_set.pde
Eric Ayars
4/11
Test of set-time routines for a DS3231 RTC
********************************************************/
#include <DS3231.h>
#include <Wire.h>

//************************* DEFINES ************************************

//************************* PROTOTYPES ************************************
void getDateStuff( byte &year, byte &month, byte &date, byte &dOW, byte &hour
    , byte &minute, byte &second); 

//************************* VARIABLES ************************************
DS3231 myRTC;
byte   year, month, date, dOW, hour, minute, second;

/*F********************************************************************
*
**********************************************************************/
void 
setup() 
{
    Serial.begin( BAUD );                               // START SERIAL PORT
    Wire.begin();                                        // START I2C IRFACE
}
/*F********************************************************************
*
**********************************************************************/
void 
loop() 
{
    // IF SOMETHING IS COMING IN ON SERIAL LINE, IT's A TIME CORRECTION SO
    // SET CLOCK ACCORDINGLY
    if( Serial.available()) 
    {
        getDateStuff( year, month, date, dOW, hour, minute, second);
        myRTC.setClockMode( false );                           // SET TO 24H
        //setClockMode( true );                                // SET TO 12H
        myRTC.setYear( year );
        myRTC.setMonth( month );
        myRTC.setDate( date );
        myRTC.setDoW( dOW );
        myRTC.setHour( hour );
        myRTC.setMinute( minute );
        myRTC.setSecond( second );
        // TEST ALARM FUNCTIONS
// SET A1 TO ONE MINUTE PAST TIME WE JUST SET CLOCK ON CURRENT DAY OF WEEK.
        myRTC.setA1Time( dOW, hour, minute+1, second, 0x0, true
            , false, false );
        // SET A2 TO TWO MINUTES PAST, ON CURRENT DAY OF MONTH
        myRTC.setA2Time( date, hour, minute+2, 0x0, false
            , false, false );
        // TURN ON BOTH ALARMS, WITH EXTERNAL INTERRUPT
        myRTC.turnOnAlarm( 1 );
        myRTC.turnOnAlarm( 2 );
    }
    delay( 1000 );
}
/*F********************************************************************
* READS Dat-Time from Serial Monitor
**********************************************************************/
void 
getDateStuff( byte &year, byte &month, byte &date, byte &dOW, byte &hour
    , byte &minute, byte &second) 
{
    // IF CHARS ARRIVING SERIAL. IN ORDER YYMMDDwHHMMSS, WITH AN 'X' END
    bool  gotString = false;
    char  inChar, inString[20];
    byte  temp1, temp2, j;

    j =0;
    while( !gotString )     
    {
        if( Serial.available()) 
        {
            inChar = Serial.read();
            inString[j] = inChar;
            j += 1;
            if( inChar == 'x') 
                gotString = true;
        }
    }
    Serial.println( inString ); // 48 IS HEX NUMERIC PREFIX '0' = X30
    temp1 = (byte)inString[0] -48;                        // READ YEAR FIRST
    temp2 = (byte)inString[1] -48;
    year = temp1*10 + temp2;
    temp1 = (byte)inString[2] -48;                             // READ MONTH
    temp2 = (byte)inString[3] -48;
    month = temp1*10 + temp2;
    temp1 = (byte)inString[4] -48;                               // NOW DATE
    temp2 = (byte)inString[5] -48;
    date = temp1*10 + temp2;
    dOW = (byte)inString[6] - 48;                         // NOW dAY OF wEEK
    temp1 = (byte)inString[7] -48;                               // NOW HOUR
    temp2 = (byte)inString[8] -48;
    hour = temp1*10 + temp2;
    temp1 = (byte)inString[9] -48;                             // NOW MINUTE
    temp2 = (byte)inString[10] -48;
    minute = temp1*10 + temp2;
    temp1 = (byte)inString[11] -48;                            // NOW SECOND
    temp2 = (byte)inString[12] -48;
    second = temp1*10 + temp2;
}