Advertisement
Guest User

Afisaj TFT, refresh la 1 minut

a guest
Jan 29th, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // CONNECTIONS:
  2. // DS1302 CLK/SCLK --> 5
  3. // DS1302 DAT/IO --> 4
  4. // DS1302 RST/CE --> 2
  5. // DS1302 VCC --> 3.3v - 5v
  6. // DS1302 GND --> GND
  7.  
  8. #include <ThreeWire.h>  
  9. #include <RtcDS1302.h>
  10. #include <TFT.h>  
  11. #include <SPI.h>
  12.  
  13. #define cs   10
  14. #define dc   9
  15. #define rst  8
  16. unsigned long previousMillis = 0;
  17. int Min=0;
  18. TFT TFTscreen = TFT(cs, dc, rst);
  19.  
  20. ThreeWire myWire(6,7,5); // IO, SCLK, CE
  21. RtcDS1302<ThreeWire> Rtc(myWire);
  22.  
  23. void setup ()
  24. {
  25.     Serial.begin(57600);
  26.  
  27.      //initialize the library
  28.      TFTscreen.begin();
  29.  
  30.      // clear the screen with a black background
  31.      TFTscreen.background(0, 0, 0);
  32.      
  33.      //TFTscreen.setRotation(0);
  34.  
  35.     Serial.print("compiled: ");
  36.     Serial.print(__DATE__);
  37.     Serial.println(__TIME__);
  38.  
  39.     Rtc.Begin();
  40.  
  41.     RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
  42.     printData(compiled);
  43.     Serial.println();
  44.  
  45.     if (!Rtc.IsDateTimeValid())
  46.     {
  47.         // Common Causes:
  48.         //    1) first time you ran and the device wasn't running yet
  49.         //    2) the battery on the device is low or even missing
  50.  
  51.         Serial.println("RTC lost confidence in the DateTime!");
  52.         Rtc.SetDateTime(compiled);
  53.     }
  54.  
  55.     if (Rtc.GetIsWriteProtected())
  56.     {
  57.         Serial.println("RTC was write protected, enabling writing now");
  58.         Rtc.SetIsWriteProtected(false);
  59.     }
  60.  
  61.     if (!Rtc.GetIsRunning())
  62.     {
  63.         Serial.println("RTC was not actively running, starting now");
  64.         Rtc.SetIsRunning(true);
  65.     }
  66.  
  67.     RtcDateTime now = Rtc.GetDateTime();
  68.     if (now < compiled)
  69.     {
  70.         Serial.println("RTC is older than compile time!  (Updating DateTime)");
  71.         Rtc.SetDateTime(compiled);
  72.     }
  73.     else if (now > compiled)
  74.     {
  75.         Serial.println("RTC is newer than compile time. (this is expected)");
  76.     }
  77.     else if (now == compiled)
  78.     {
  79.         Serial.println("RTC is the same as compile time! (not expected but all is fine)");
  80.     }
  81. }
  82.  
  83. void loop ()
  84. {
  85.  
  86.     TFTscreen.stroke(155, 155, 155);
  87.     RtcDateTime now = Rtc.GetDateTime();
  88.  
  89.     printData(now);
  90.     printOra(now);
  91.     printMin(now);
  92.     Serial.println();
  93.  
  94.     if (!now.IsValid())
  95.     {
  96.         // Common Causes:
  97.         //    1) the battery on the device is low or even missing and the power line was disconnected
  98.         Serial.println("RTC lost confidence in the DateTime!");
  99.     }
  100.  
  101.     //delay(6000);
  102.  
  103.     // unsigned long currentMillis = millis();
  104.      
  105.     // if (currentMillis - previousMillis >= 6000) {
  106.     // save the last time
  107.     //previousMillis = currentMillis;
  108.    
  109.    
  110.     //TFTscreen.background(0,0,0);
  111.    
  112.     //}
  113.    
  114.    
  115. }
  116.  
  117. #define countof(a) (sizeof(a) / sizeof(a[0]))
  118.  
  119. void printData(const RtcDateTime& dt)
  120. {
  121.     char datestring[20];
  122.     //set the text size
  123.      TFTscreen.setTextSize(2);
  124.    
  125.     snprintf_P(datestring,
  126.             countof(datestring),
  127.             PSTR("%02u/%02u/%04u"),
  128.             dt.Day(),
  129.             dt.Month(),
  130.             dt.Year() );
  131.     //Serial.print(datestring);
  132.     TFTscreen.text(datestring, 22, 10);
  133.    
  134. }
  135.  
  136. void printOra(const RtcDateTime& dt)
  137. {
  138.     char datestring[20];
  139.     //set the text size
  140.     TFTscreen.setTextSize(5);
  141.     snprintf_P(datestring,
  142.             countof(datestring),
  143.             PSTR("%02u:%02u"),
  144.             dt.Hour(),
  145.             dt.Minute());
  146.     //Serial.print(datestring);
  147.    
  148.     TFTscreen.text(datestring, 6, 57);
  149.    
  150.      
  151. }
  152. void printMin(const RtcDateTime& dt)
  153. {
  154.     char datestring[3];
  155.     int Idatestring=atoi(datestring);
  156.     //set the text size
  157.     TFTscreen.setTextSize(5);
  158.     snprintf_P(datestring,
  159.             countof(datestring),
  160.             PSTR("%02u"),
  161.             dt.Minute());
  162.     //Serial.print(datestring);
  163.     Serial.print(Idatestring-Min); //daca se sterge nu mai compara (Idatestring-Min)>0....ramane = cu 0 in permanenta
  164.    
  165.     //TFTscreen.text(datestring, 6, 57);
  166.  
  167.     if ((Idatestring-Min)>0)
  168.     {
  169.       Min=Idatestring;
  170.       TFTscreen.background(0,0,0);
  171.     }
  172.      
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement