Advertisement
Lukashk

terárium/DS3231_casocislo

Mar 9th, 2020
2,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void ds3231ts(void){
  2.     Wire.begin();
  3.     Serial.begin(9600);
  4.     // set the initial time here:
  5.     // DS3231 seconds, minutes, hours, day, date, month, year
  6.     // setDS3231time(30,42,21,4,26,11,14);
  7. }
  8. /*void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year){
  9.     // sets time and date data to DS3231
  10.     Wire.beginTransmission(DS3231_I2C_ADDRESS);
  11.     Wire.write(0); // set next input to start at the seconds register
  12.     Wire.write(decToBcd(second)); // set seconds
  13.     Wire.write(decToBcd(minute)); // set minutes
  14.     Wire.write(decToBcd(hour)); // set hours
  15.     Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  16.     Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  17.     Wire.write(decToBcd(month)); // set month
  18.     Wire.write(decToBcd(year)); // set year (0 to 99)
  19.     Wire.endTransmission();
  20. }*/
  21. void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year){
  22.     Wire.beginTransmission(DS3231_I2C_ADDRESS);
  23.     Wire.write(0); // set DS3231 register pointer to 00h
  24.     Wire.endTransmission();
  25.     Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  26.     // request seven bytes of data from DS3231 starting from register 00h
  27.     *second = bcdToDec(Wire.read() & 0x7f);
  28.     *minute = bcdToDec(Wire.read());
  29.     *hour = bcdToDec(Wire.read() & 0x3f);
  30.     *dayOfWeek = bcdToDec(Wire.read());
  31.     *dayOfMonth = bcdToDec(Wire.read());
  32.     *month = bcdToDec(Wire.read());
  33.     *year = bcdToDec(Wire.read());
  34. }
  35.  
  36. void displayTime(void){
  37.     byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  38.     // retrieve data from DS3231
  39.     readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  40.     // send it to the serial monitor
  41.     Serial.print(hour, DEC);
  42.     // convert the byte variable to a decimal number when displayed
  43.     Serial.print(":");
  44.     if (minute<10){
  45.         Serial.print("0");
  46.     }
  47.     Serial.print(minute, DEC);
  48.     Serial.print(":");
  49.     if (second<10){
  50.         Serial.print("0");
  51.     }
  52.     Serial.print(second, DEC);
  53.     Serial.print(" ");
  54.     Serial.print(dayOfMonth, DEC);
  55.     Serial.print("/");
  56.     Serial.print(month, DEC);
  57.     Serial.print("/");
  58.     Serial.print(year, DEC);
  59.     Serial.print(" Den: ");
  60.     switch(dayOfWeek){
  61.         case 1:
  62.             Serial.println("Neděle");
  63.             break;
  64.         case 2:
  65.             Serial.println("Pondělí");
  66.             break;
  67.         case 3:
  68.             Serial.println("Úterý");
  69.             break;
  70.         case 4:
  71.             Serial.println("Středa");
  72.             break;
  73.         case 5:
  74.             Serial.println("Čtvrtek");
  75.             break;
  76.         case 6:
  77.             Serial.println("Pátek");
  78.             break;
  79.         case 7:
  80.             Serial.println("Sobota");
  81.             break;
  82.                }
  83.            
  84.              lcd.setCursor(3, 3);
  85.              lcd.print("Cas : ");
  86.              lcd.print(hour +1);
  87.              lcd.print(":");
  88.                 if (minute<10){
  89.                   lcd.print("0");
  90.                    }
  91.              lcd.print(minute);
  92.              lcd.print(":");
  93.                 if (second<10){
  94.                   lcd.print("0");
  95.                     }      
  96.              lcd.print(second);
  97.               //delay(1000);
  98.      
  99. }
  100.  
  101.  
  102.  
  103. void casocislo(void)
  104. {
  105.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  106.   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  107.   int timestamp = (hour + 1)*60  + minute ;
  108.   Serial.println(timestamp);
  109. }
  110.  
  111.  void zobraz(void)
  112.  {
  113.     displayTime(); // display the real-time clock data on the Serial Monitor,
  114.     casocislo();
  115.     //delay(1000); // every second
  116.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement