Ho un DS3231 nuovo, appena tolto dal suo involucro antistatico.
Ci carico lo sketch che uso su un'altro RTC simile ma mi visualizza sempre sul monitor seriale dei dati incomprensibili tipo:
165/165/20165 45:85:85
Lo sketch è questo:
/********************************
* DS3231_R_W.ini
* 20 marzo 2019
* Arduino NANO
* Legge e scrive data e ora
* Derivato da DS3231_R_Mod.ini
*********************************/
#include <Wire.h>
#define DS3231_I2C_ID 0x68
struct{
byte Second = 00;
byte Minute = 4;
byte Hour = 18;
byte W_day = 2;
byte M_day = 22;
byte Month = 3;
byte Year = 2021;
} now;
void setup() {
Serial.begin(57600);
Wire.begin();
// Imposta l'orario
Wire.beginTransmission(DS3231_I2C_ID);
Wire.write(0x0); // punta al registro 0
Wire.write(0x0); // azzera i secondi
Wire.write(bin2BCD(now.Minute));
Wire.write(bin2BCD(now.Hour));
Wire.endTransmission();
// Imposta la data
Wire.beginTransmission(DS3231_I2C_ID);
Wire.write(0x3); // punta al registro 3
Wire.write(now.W_day);
Wire.write(bin2BCD(now.M_day));
Wire.write(bin2BCD(now.Month));
Wire.write(bin2BCD(now.Year));
Wire.endTransmission();
}
void loop() {
readDate();
delay(5000);
}
void readDate() {
Wire.beginTransmission(DS3231_I2C_ID);
Wire.write(0x0);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ID, 7); // get 7 bytes
byte Second = bcd2dec(Wire.read() & 0b1111111);
byte Minute = bcd2dec(Wire.read() & 0b1111111);
byte Hour = bcd2dec(Wire.read() & 0b00111111);
byte WeekDay = bcd2dec(Wire.read()); // 1=Domenica
byte Day = bcd2dec(Wire.read());
byte Month = bcd2dec(Wire.read());
byte Year = bcd2dec(Wire.read());
Serial.print(Day);
Serial.print("/");
Serial.print(Month);
Serial.print("/20");
Serial.print(Year);
Serial.print(" ");
Serial.print(Hour);
Serial.print(":");
Serial.print(Minute);
Serial.print(":");
if (Second < 10) Serial.print("0");
Serial.println(Second);
}
// Convert binary coded decimal (BCD) to normal decimal numbers
byte bcd2dec(byte val) {
return ((val/16*10) + (val%16));
}
byte bin2BCD(byte n){
return ((n / 10) << 4) | (n % 10);
}
L'altro modulino dove è installato questo sketch funziona regolarmente da un paio di anni.
Mi sono dimenticato qualche reset, qualche altro passaggio di configurazione.
Anche usando un arduino Uno e adattando i pin SCL e SDA non cambia niente.
Saluti
LelloGi