Lessen 19 Real Time Clock Module

Hallo Zusammen,

da ich über google nichts gefunden habe versuch eich es mal hier.

Ich habe die Bibliothek DS3231 von der beiliegenden CD eingebunden und die datei auf den UNO R3 Hochgeladen ´.

Im Seriellen Monitor wird aber das angezeigt, ich weiss nicht wie ich die Zeit einstelle.

Long number format:
Long format with month name:
Short format witch 12h mode:
Today is: days of the year.
Actual month has: days.
Unixtime:

Hier ist der Sketch

//www.elegoo.com
//2016.12.9
#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;

void setup()
{
  Serial.begin(9600);

  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();

  // Set sketch compiling time
  clock.setDateTime(__DATE__, __TIME__);

  // Set from UNIX timestamp
  // clock.setDateTime(1397408400);

  // Manual (YYYY, MM, DD, HH, II, SS
  // clock.setDateTime(2016, 12, 9, 11, 46, 00);
}

void loop()
{
  dt = clock.getDateTime(int);

  Serial.print("Long number format:          ");
  Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));

  Serial.print("Long format with month name: ");
  Serial.println(clock.dateFormat("d F Y H:i:s",  dt));

  Serial.print("Short format witch 12h mode: ");
  Serial.println(clock.dateFormat("jS M y, h:ia", dt));

  Serial.print("Today is:                    ");
  Serial.print(clock.dateFormat("l, z", dt));
  Serial.println(" days of the year.");

  Serial.print("Actual month has:        ");
  Serial.print(clock.dateFormat("t", dt));
  Serial.println(" days.");

  Serial.print("Unixtime:                    ");
  Serial.println(clock.dateFormat("U", dt));

  Serial.println();

  delay(5000);
}

Die Kommunikation mit DS3231 funktioniert nicht, da keine Uhrzeit angezeigt wird. Der Baustein hat immer eine Zeit und Datum, auch wenn er nicht gestellt wird.

Laß mal einen I2C-Scanner laufen, ob eine Adresse angezeigt wird.

clock.setDateTime(DATE, TIME);
Setzt die RTC mit der PC Zeit/ Datum vim Kopilationszeitpunkt.

Nach dem ersten Einschalten muß diese Zeile auskomentiert werden und der Sketch nochmal Hochgeladen werden.

Grüße Uwe

Hallo Uwe, leider hat das auskomentieren nicht funktioniert
@agmue - der 12 c Scanner hat 3 adressen gefunden

0x57
0x5F
0x68

angeschlossen ist der RTC:

SCL - SCL
SDA - SDA wie im Handbuch beschrieben

Bei mir:

I2C device found at address 0x57 (87) it could be a EEPROM AT24Cxxxx
I2C device found at address 0x68 (104) it could be a Real Time Clock DS3231/DS1337/DS1307/...

Das EEPROM sitzt bei mir auf der Platine mit drauf.

Was ist aber bei Dir 0x5F?

Das kann ich dir leider nicht sagen :confused:
Ich besitze den UNO R3 seit einem Tag :slight_smile:
Ich hab mal den Schaltplan mit eingefügt, so und nichts anderes ist bei mir auch aufgebaut.

Ist eventuell der DS3231 kaputt?

agmue:
Was ist aber bei Dir 0x5F?

Wenn der RTC baugleich mit dem AZDelivery Real Time Clock RTC DS3231 ist, könnte das ein Temperatursensor sein. Laut Beschreibung:

Das RTC-Modul mit DS3231 Chipsatz zeichnet sich nicht nur durch seinen hochräzisen Uhrenquarz aus, es findet auch jede Minute eine Messung der Temperatur statt, um den Oscillator zu Korrekturzwecken anzupassen.

Ich hab das Teil aber noch nicht getestet, daher ist es nur eine Annahme.

Gruß
Jörg

wapjoe:
Wenn der RTC baugleich mit dem AZDelivery Real Time Clock RTC DS3231 ist, könnte das ein Temperatursensor sein. Laut Beschreibung:
Ich hab das Teil aber noch nicht getestet, daher ist es nur eine Annahme.

Gruß
Jörg

ja das kann sein, ich kann damit auch eine Temperatur messen

/*
  DS3231: Real-Time Clock. Temperature example
  Read more: www.jarzebski.pl/arduino/komponenty/zegar-czasu-rzeczywistego-rtc-ds3231.html
  GIT: https://github.com/jarzebski/Arduino-DS3231
  Web: http://www.jarzebski.pl
  (c) 2014 by Korneliusz Jarzebski
*/

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;

void setup()
{
  Serial.begin(9600);

  // Initialize DS3231
  Serial.println("Initialize DS3231");;
  clock.begin();
}

void loop()
{
  // The temperature registers are updated after every 64-second conversion.
  // If you want force temperature conversion use forceConversion()
  clock.forceConversion();

  Serial.print("Temperature: ");
  Serial.println(clock.readTemperature());

  delay(1000);
}

Arni89:
Das kann ich dir leider nicht sagen :confused:
Ich besitze den UNO R3 seit einem Tag :slight_smile:
Ich hab mal den Schaltplan mit eingefügt, so und nichts anderes ist bei mir auch aufgebaut.

Ist eventuell der DS3231 kaputt?

Bei meiner baugleichen Platine wird vom Händler und einigen Kunden eine Eingangsspannung von 3,3 V empfohlen. Mangels Test kann ich das aber nicht bestätigen.

Arni89:
ja das kann sein, ich kann damit auch eine Temperatur messen

Die RTC ist temperaturkompensiert, daher steht auch die Temperatur als Wert zur Verfügung.

Ich habe mal ein Programm zusammengewürfelt, um die Programmbibliothek DS3231 zu umgehen:

// http://tronixlabs.com/news/tutorial-using-ds1307-and-ds3231-realtime-clock-modules-with-arduino/
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val / 16 * 10) + (val % 16) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  // set the initial time here:
  // Wednesday November 26, 2014 and 9:42 pm and 30 seconds
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,42,21,4,26,11,14);
  // So Mo Di Mi Do Fr Sa
  //  1  2  3  4  5  6  7
  // setDS3231time(00,46,19,2,23,10,17);
  displayTime(); // display the real-time clock data on the Serial Monitor,
  RTCDump(DS3231_I2C_ADDRESS, 19);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
                   dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
                    byte *minute,
                    byte *hour,
                    byte *dayOfWeek,
                    byte *dayOfMonth,
                    byte *month,
                    byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}
void displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
                 &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  Serial.print(":");
  if (minute < 10)
  {
    Serial.print("0");
  }
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
  {
    Serial.print("0");
  }
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print("/");
  Serial.print(month, DEC);
  Serial.print("/");
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  switch (dayOfWeek) {
    case 1:
      Serial.println("Sunday");
      break;
    case 2:
      Serial.println("Monday");
      break;
    case 3:
      Serial.println("Tuesday");
      break;
    case 4:
      Serial.println("Wednesday");
      break;
    case 5:
      Serial.println("Thursday");
      break;
    case 6:
      Serial.println("Friday");
      break;
    case 7:
      Serial.println("Saturday");
      break;
  }
}
void loop() {}

void RTCDump(byte DS3231_ADDRESS, unsigned int NumberOfBytes)
{
  Serial.print(F("Dump of NVRAM content ("));
  Serial.print(NumberOfBytes);
  Serial.print(F(" bytes) from I2C device"));
  PrintAddress(DS3231_ADDRESS, 1);
  Wire.beginTransmission(DS3231_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
  if ( Wire.requestFrom(DS3231_ADDRESS, NumberOfBytes) == NumberOfBytes ) {
    while (!Wire.available())
    {
      // waiting
    }
    byte addr = 0;
    Serial.println(F("The information is apply only for chip DS3231, partially for chips DS1337 and DS1307 (and maybe for another similar chips)"));
    PrintAddress(addr++, 4); Serial.print(F("Seconds             = ")); Serial.println(bcdToDec(Wire.read() & 0x7F));
    PrintAddress(addr++, 4); Serial.print(F("Minutes             = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Hours               = ")); Serial.println(bcdToDec(Wire.read() & 0x3F));
    PrintAddress(addr++, 4); Serial.print(F("Weekday             = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Day                 = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Month/Century       = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Year                = ")); Serial.println(bcdToDec(Wire.read()) + 2000);
    PrintAddress(addr++, 4); Serial.print(F("Alarm 1 Seconds     = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Alarm 1 Minutes     = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Alarm 1 Hours       = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Alarm 1 Day&Date    = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Alarm 2 Minutes     = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Alarm 2 Hours       = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Alarm 2 Day&Date    = ")); Serial.println(bcdToDec(Wire.read()));
    PrintAddress(addr++, 4); Serial.print(F("Control             = ")); Serial.println(Wire.read(), BIN);
    PrintAddress(addr++, 4); Serial.print(F("Control/Status      = ")); Serial.println(Wire.read(), BIN);
    PrintAddress(addr++, 4); Serial.print(F("Aging Offset        = ")); Serial.println(Wire.read());
    PrintAddress(addr++, 4); Serial.print(F("Temperature MSB+LSB = "));
    //MSB of Temp
    float ttc = (float)(int)Wire.read();
    //LSB of Temp
    addr++;
    byte portion = Wire.read();
    if (portion == 0b01000000) ttc += 0.25;
    if (portion == 0b10000000) ttc += 0.5;
    if (portion == 0b11000000) ttc += 0.75;
    char buffer[16];
    String TempStr = dtostrf(ttc, 5, 2, buffer);
    TempStr = TempStr + "\xB0\x43"; //oC for Western Character Set
    Serial.println(TempStr);
  }
  else {
    Serial.println(F("Error"));
  };
  Serial.println(F("End of Dump\n"));
};

void PrintAddress(byte addr, byte details)
{
  if ( ( details == 1 ) or ( details == 5 ) )
  {
    Serial.print(F(" at address 0x"));
  };
  if ( details == 3 )
  {
    Serial.print(F("(0x"));
  };
  if ( details == 4 )
  {
    Serial.print(F("0x"));
  };
  if ( addr < 16 )
  {
    Serial.print("0");
  };
  Serial.print(addr, HEX);
  if ( details == 1 )
  {
    Serial.print(" (");
    Serial.print(addr);
    Serial.println(")");
  };
  if ( details == 2 )
  {
    Serial.print(" ");
    Serial.print(addr);
    Serial.print(" ");
    Serial.println((char)addr);
  };
  if ( details == 3 )
  {
    Serial.println(")");
  };
  if ( details == 4 )
  {
    Serial.print(" ");
  };
  if ( details == 5 )
  {
    Serial.print(" (");
    Serial.print(addr);
    Serial.print(") ");
  };
};

Ausgabe:

</sup> <sup>13:47:27 2/3/19 Day of week: Saturday Dump of NVRAM content (19 bytes) from I2C device at address 0x68 (104) The information is apply only for chip DS3231, partially for chips DS1337 and DS1307 (and maybe for another similar chips) 0x00 Seconds             = 27 0x01 Minutes             = 47 0x02 Hours               = 13 0x03 Weekday             = 7 0x04 Day                 = 2 0x05 Month/Century       = 3 0x06 Year                = 2019 0x07 Alarm 1 Seconds     = 10 0x08 Alarm 1 Minutes     = 80 0x09 Alarm 1 Hours       = 80 0x0A Alarm 1 Day&Date    = 80 0x0B Alarm 2 Minutes     = 0 0x0C Alarm 2 Hours       = 0 0x0D Alarm 2 Day&Date    = 0 0x0E Control             = 11001 0x0F Control/Status      = 10000001 0x10 Aging Offset        = 0 0x11 Temperature MSB+LSB = 22.75°C End of Dump</sup> <sup>

Wie sieht das bei Dir aus?

Hi agmue,

danke für deine Hilfe.
bei mir ist das rausgekommen

22:53:51 6/3/19 Day of week: Tuesday
Dump of NVRAM content (19 bytes) from I2C device at address 0x68 (104)
The information is apply only for chip DS3231, partially for chips DS1337 and DS1307 (and maybe for another similar chips)
0x00 Seconds = 51
0x01 Minutes = 53
0x02 Hours = 22
0x03 Weekday = 3
0x04 Day = 6
0x05 Month/Century = 3
0x06 Year = 2019
0x07 Alarm 1 Seconds = 0
0x08 Alarm 1 Minutes = 0
0x09 Alarm 1 Hours = 0
0x0A Alarm 1 Day&Date = 0
0x0B Alarm 2 Minutes = 0
0x0C Alarm 2 Hours = 0
0x0D Alarm 2 Day&Date = 0
0x0E Control = 10011
0x0F Control/Status = 10001000
0x10 Aging Offset = 0
0x11 Temperature MSB+LSB = 25.50⸮C
End of Dump

Arni89:
Ist eventuell der DS3231 kaputt?

Wohl nicht, denn die Ausgabe sieht plausibel aus.

Dann hat möglicherweise die mitgelieferte Bibliothek eine Macke oder es wird die falsche Bibliothek eingebunden. Du könntest eine andere verwenden oder direkt auf die Register zugreifen, so wie das das Programm in #9 macht. Mit setDS3231time() wird die Zeit gesetzt, das wolltest Du ja machen.

Tipp: Programm mit aktiver Funktion setDS3231time() und Zeit etwas in der Zukunft laden und dann zur Zeit den Resetknopf drücken und mit dem Zeitzeichen loslassen. Danach das Programm mit deaktivierter Funktion laden. Umstellung auf Sommerzeit per Programm, dann kann die Uhr ungestört laufen.

Ok danke für die Hilfe ich probiere es nochmal :slight_smile: