|
| 1 | +/*----------------------------------------------------------------------* |
| 2 | + * Digital clock display using an MCP79412 Real-Time Clock/Calendar * |
| 3 | + * and an ATtiny45/85 with a 1MHz system clock. * |
| 4 | + * * |
| 5 | + * Tested with Arduino 1.0.5. Also Arduino-Tiny Core, TinyISP, and * |
| 6 | + * TinyDebugKnockBang from http://code.google.com/p/arduino-tiny/ * |
| 7 | + * * |
| 8 | + * Run TinyISP on an ATmega microcontroller that does not have an LED * |
| 9 | + * connected to pin 13 (SCK). The LED causes problems because the SPI * |
| 10 | + * pins are also the I2C pins on the ATtiny. Connect MISO, MOSI, SCK * |
| 11 | + * on the ATmega to the corresponding pins on the ATtiny through 220Ω * |
| 12 | + * resistors for safety. Use 4.7K pullup resistors on the ATtiny * |
| 13 | + * I2C bus. * |
| 14 | + * * |
| 15 | + * Jack Christensen 21Aug2013 * |
| 16 | + * * |
| 17 | + * This work is licensed under the Creative Commons Attribution- * |
| 18 | + * ShareAlike 3.0 Unported License. To view a copy of this license, * |
| 19 | + * visit http://creativecommons.org/licenses/by-sa/3.0/ or send a * |
| 20 | + * letter to Creative Commons, 171 Second Street, Suite 300, * |
| 21 | + * San Francisco, California, 94105, USA. * |
| 22 | + *----------------------------------------------------------------------*/ |
| 23 | + |
| 24 | +#include <MCP79412RTC.h> //http://github.com/JChristensen/MCP79412RTC |
| 25 | +#include <Time.h> //http://playground.arduino.cc/Code/Time |
| 26 | +#include <TinyDebugKnockBang.h> //http://code.google.com/p/arduino-tiny/ |
| 27 | +#include <TinyWireM.h> //http://playground.arduino.cc/Code/USIi2c |
| 28 | + |
| 29 | +void setup(void) |
| 30 | +{ |
| 31 | + Debug.begin(250000); |
| 32 | + |
| 33 | + //setSyncProvider() causes the Time library to synchronize with the |
| 34 | + //external RTC by calling RTC.get() every five minutes by default. |
| 35 | + setSyncProvider(RTC.get); |
| 36 | + Debug.print(F("RTC Sync")); |
| 37 | + if (timeStatus() != timeSet) Debug.print(F(" FAIL!")); |
| 38 | + Debug.println(); |
| 39 | +} |
| 40 | + |
| 41 | +void loop(void) |
| 42 | +{ |
| 43 | + static time_t tLast; |
| 44 | + |
| 45 | + time_t t = now(); |
| 46 | + if (t != tLast) { |
| 47 | + tLast = t; |
| 48 | + printDateTime(t); |
| 49 | + Debug.println(); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +//print date and time to Serial |
| 54 | +void printDateTime(time_t t) |
| 55 | +{ |
| 56 | + printDate(t); |
| 57 | + Debug.print(' '); |
| 58 | + printTime(t); |
| 59 | +} |
| 60 | + |
| 61 | +//print time to Serial |
| 62 | +void printTime(time_t t) |
| 63 | +{ |
| 64 | + printI00(hour(t), ':'); |
| 65 | + printI00(minute(t), ':'); |
| 66 | + printI00(second(t), ' '); |
| 67 | +} |
| 68 | + |
| 69 | +//print date to Serial |
| 70 | +void printDate(time_t t) |
| 71 | +{ |
| 72 | + printI00(day(t), 0); |
| 73 | + Debug.print(monthShortStr(month(t))); |
| 74 | + Debug.print(year(t), DEC); |
| 75 | +} |
| 76 | + |
| 77 | +//Print an integer in "00" format (with leading zero), |
| 78 | +//followed by a delimiter character to Serial. |
| 79 | +//Input value assumed to be between 0 and 99. |
| 80 | +void printI00(int val, char delim) |
| 81 | +{ |
| 82 | + if (val < 10) Debug.print('0'); |
| 83 | + Debug.print(val, DEC);; |
| 84 | + if (delim > 0) Debug.print(delim); |
| 85 | + return; |
| 86 | +} |
0 commit comments