DS3231 Set
/*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 dow, hour, minute; second;
/*F********************************************************************
*
**********************************************************************/
void
setup()
{
Serial.begin( BAUD ); // START SERIAL PORT
Wire.begin(); // START I2C IFACE
}
/*F********************************************************************
*
**********************************************************************/
void
loop()
{
// If something is coming in on serial line, it's a time correction so set clock
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 of alarm functions
// set A1 to one minute past the time we just set the 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********************************************************************
*
**********************************************************************/
void
getDateStuff( byte& year, byte& month, byte& date, byte& dOW,
byte& hour, byte& minute, byte& second )
{
// Call this if you notice something coming in on serial port. stuff coming
// in should be in order YYMMDDwHHMMSS, with an 'x' at end.
boolean gotString = false;
char inChar;
byte temp1, temp2;
char inString[20];
byte j=0;
while ( !gotString )
{
if ( Serial.available() )
{
inChar = Serial.read();
inString[j] = inChar;
j += 1;
if ( inChar == 'x' )
gotString = true;
}
}
Serial.println( inString );
temp1 = ( byte )inString[0] -48; // READ YEAR FIRST
temp2 = ( byte )inString[1] -48;
year = temp1*10 + temp2;
temp1 = ( byte )inString[2] -48; // NOW 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;
}