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?