// RealTimeClock // Tim Hirzel // // This Code talks to a DC1307 RTC chip #include char * daynames = "SunMonTueWedThuFriSat"; char * monthnames = "JanFebMarAprMayJunJulAugSepOctNovDec"; int readSeconds() { int sec = 0; byte b; Wire.beginTransmission(0x68); Wire.send(0x0); Wire.endTransmission(); Wire.requestFrom(0x68, 1); // request 6 bytes from slave device #2 b = Wire.receive(); // receive a byte as character sec = 10 * (b >> 4) + (b & B00001111); return sec; } int _readFromPosition(byte position) { byte b; Wire.beginTransmission(0x68); Wire.send(position); Wire.endTransmission(); Wire.requestFrom(0x68, 1); // request 5 bytes from RTC b = Wire.receive(); // receive a byte as character return 10* (b >> 4) + (b & B00001111); } int readHour() { return _readFromPosition(0x2); } int readMinute() { return _readFromPosition(0x1); } void readTimeString(char * timestr, boolean terminate) { // char * timestr = " "; // int charcount = 16; // char timestr [charcount]; byte b; int i; Wire.beginTransmission(0x68); Wire.send(0x1); Wire.endTransmission(); Wire.requestFrom(0x68, 5); // request 5 bytes from RTC b = Wire.receive(); // receive a byte as character timestr[15] = 48 + (b & B00001111); timestr[14] = 48 + (b >> 4); timestr[13] = ':'; b = Wire.receive(); // receive a byte as character timestr[12] = 48 + (b & B00001111); timestr[11] = 48 + ((b & B00110000) >> 4); timestr[10] = ' '; b = Wire.receive(); // Day of Week int daystart = ((int)(b & B00000111) - 1)*3; for (i = 0; i < 3; i++) { timestr[i] = daynames[daystart +i]; } timestr[3] = ' '; b = Wire.receive(); // Date timestr[9] = (char) (48 + (b & B00001111)); timestr[8] = (char) (48 + ((b & B00110000) >> 4)); b = Wire.receive(); // Month int month = (10 * (int)((b & B00010000) >> 4)) + (b & B00001111); for (i = 0; i < 3; i++) { timestr[i+4] = monthnames[(month-1)*3 + i]; } timestr[7] = ' '; if (terminate) timestr[16] = '\0'; } /* int readYear() { return _readFromPosition(0x6); } int readMonth() { return _readFromPosition(0x5); } int readDate() { return _readFromPosition( 0x4); } int readDay() { return _readFromPosition(0x3); } int readSecond() { return _readFromPosition(0x0); } */ /* void _writeToPosition(byte val, byte position) { Wire.beginTransmission(0x68); Wire.send(position); Wire.send(((val/10) <<4) + (val%10 & B00001111)); Wire.endTransmission(); } void writeYear(byte year) { _writeToPosition(year, 0x6); } void writeMonth(byte month) { _writeToPosition(month, 0x5); } void writeDate(byte date) { _writeToPosition(date, 0x4); } void writeDay(byte day) { _writeToPosition(day, 0x3); } void writeHour(byte hour) { _writeToPosition(hour, 0x2); } void writeMinute(byte minute) { _writeToPosition(minute, 0x1); } void writeSecond(byte second) { _writeToPosition(second, 0x0); } */