Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CONNECTIONS:
- // DS1302 CLK/SCLK --> 5
- // DS1302 DAT/IO --> 4
- // DS1302 RST/CE --> 2
- // DS1302 VCC --> 3.3v - 5v
- // DS1302 GND --> GND
- #include <ThreeWire.h>
- #include <RtcDS1302.h>
- #include <TFT.h>
- #include <SPI.h>
- #define cs 10
- #define dc 9
- #define rst 8
- unsigned long previousMillis = 0;
- int Min=0;
- TFT TFTscreen = TFT(cs, dc, rst);
- ThreeWire myWire(6,7,5); // IO, SCLK, CE
- RtcDS1302<ThreeWire> Rtc(myWire);
- void setup ()
- {
- Serial.begin(57600);
- //initialize the library
- TFTscreen.begin();
- // clear the screen with a black background
- TFTscreen.background(0, 0, 0);
- //TFTscreen.setRotation(0);
- Serial.print("compiled: ");
- Serial.print(__DATE__);
- Serial.println(__TIME__);
- Rtc.Begin();
- RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
- printData(compiled);
- Serial.println();
- if (!Rtc.IsDateTimeValid())
- {
- // Common Causes:
- // 1) first time you ran and the device wasn't running yet
- // 2) the battery on the device is low or even missing
- Serial.println("RTC lost confidence in the DateTime!");
- Rtc.SetDateTime(compiled);
- }
- if (Rtc.GetIsWriteProtected())
- {
- Serial.println("RTC was write protected, enabling writing now");
- Rtc.SetIsWriteProtected(false);
- }
- if (!Rtc.GetIsRunning())
- {
- Serial.println("RTC was not actively running, starting now");
- Rtc.SetIsRunning(true);
- }
- RtcDateTime now = Rtc.GetDateTime();
- if (now < compiled)
- {
- Serial.println("RTC is older than compile time! (Updating DateTime)");
- Rtc.SetDateTime(compiled);
- }
- else if (now > compiled)
- {
- Serial.println("RTC is newer than compile time. (this is expected)");
- }
- else if (now == compiled)
- {
- Serial.println("RTC is the same as compile time! (not expected but all is fine)");
- }
- }
- void loop ()
- {
- TFTscreen.stroke(155, 155, 155);
- RtcDateTime now = Rtc.GetDateTime();
- printData(now);
- printOra(now);
- printMin(now);
- Serial.println();
- if (!now.IsValid())
- {
- // Common Causes:
- // 1) the battery on the device is low or even missing and the power line was disconnected
- Serial.println("RTC lost confidence in the DateTime!");
- }
- //delay(6000);
- // unsigned long currentMillis = millis();
- // if (currentMillis - previousMillis >= 6000) {
- // save the last time
- //previousMillis = currentMillis;
- //TFTscreen.background(0,0,0);
- //}
- }
- #define countof(a) (sizeof(a) / sizeof(a[0]))
- void printData(const RtcDateTime& dt)
- {
- char datestring[20];
- //set the text size
- TFTscreen.setTextSize(2);
- snprintf_P(datestring,
- countof(datestring),
- PSTR("%02u/%02u/%04u"),
- dt.Day(),
- dt.Month(),
- dt.Year() );
- //Serial.print(datestring);
- TFTscreen.text(datestring, 22, 10);
- }
- void printOra(const RtcDateTime& dt)
- {
- char datestring[20];
- //set the text size
- TFTscreen.setTextSize(5);
- snprintf_P(datestring,
- countof(datestring),
- PSTR("%02u:%02u"),
- dt.Hour(),
- dt.Minute());
- //Serial.print(datestring);
- TFTscreen.text(datestring, 6, 57);
- }
- void printMin(const RtcDateTime& dt)
- {
- char datestring[3];
- int Idatestring=atoi(datestring);
- //set the text size
- TFTscreen.setTextSize(5);
- snprintf_P(datestring,
- countof(datestring),
- PSTR("%02u"),
- dt.Minute());
- //Serial.print(datestring);
- Serial.print(Idatestring-Min); //daca se sterge nu mai compara (Idatestring-Min)>0....ramane = cu 0 in permanenta
- //TFTscreen.text(datestring, 6, 57);
- if ((Idatestring-Min)>0)
- {
- Min=Idatestring;
- TFTscreen.background(0,0,0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement