Se me resetea la fecha y hora del modulo del reloj RTC1302

tengo un problema con el RTC1302 al momento de ejecutar el programa al principio si me dice la fecha y hora actual pero ya despues de unos segundos se me resetea y me marca la fecha hora del 01/01/2000 00:00:00 y la verdad ya he visto tutoriales pero no se me soluciona nada en el monitor serial. Al principio me dice la fecha y la hora actual y despues me arroja el siguiente mensaje "RTC is older than compile time! (Updating DateTime)"
y se me resetea. Utilizo la libreria de <RtcDS1302.h> y <ThreeWire.h> tambien he utilizando la libreria de virtualbox pero igual se me resetea este es el codigo y espero que me puedan ayudar.

// CONNECTIONS:
// DS1302 CLK/SCLK --> 5
// DS1302 DAT/IO --> 4
// DS1302 RST/CE --> 2
// DS1302 VCC --> 3.3v - 5v
// DS1302 GND --> GND

#include <ThreeWire.h>  
#include <RtcDS1302.h>

ThreeWire myWire(4,5,2); // IO, SCLK, CE
RtcDS1302<ThreeWire> Rtc(myWire);

#define countof(a) (sizeof(a) / sizeof(a[0]))

const char data[] = "what time is it";

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

    Serial.print("compiled: ");
    Serial.print(__DATE__);
    Serial.println(__TIME__);
    
    Rtc.Begin();

    RtcDateTime compiled = RtcDateTime(__DATE__,__TIME__);
    printDateTime(compiled);
      Serial.println();
    if (!Rtc.IsDateTimeValid()) 
    {
        Serial.println("RTC lost confidence in the DateTime!");
        Rtc.SetDateTime(compiled);
    }

    if (Rtc.GetIsWriteProtected())
    {
        Serial.println("RTC was write protected, enabling writing now");
        Rtc.SetIsWriteProtected(false);
    }

    if (!Rtc.GetIsRunning())
    {
        Serial.println("RTC was not actively running, starting now");
        Rtc.SetIsRunning(true);
    }

    RtcDateTime now = Rtc.GetDateTime();
    if (now < compiled) 
    {
        Serial.println("RTC is older than compile time!  (Updating DateTime)");
        Rtc.SetDateTime(compiled);
    }


/* comment out on a second run to see that the info is stored long term */
    // Store something in memory on the RTC
    uint8_t count = sizeof(data);
    uint8_t written = Rtc.SetMemory((const uint8_t*)data, count); // this includes a null terminator for the string
    if (written != count) 
    {
        Serial.print("something didn't match, count = ");
        Serial.print(count, DEC);
        Serial.print(", written = ");
        Serial.print(written, DEC);
        Serial.println();
    }
/* end of comment out section */
}

void loop () 
{
RtcDateTime now =Rtc.GetDateTime();
     printDateTime(now);
    Serial.println(" +");

    if (!now.IsValid())
    {
        // Common Causes:
        //    1) the battery on the device is low or even missing and the power line was disconnected
        Serial.println("RTC lost confidence in the DateTime!");
    }

    delay(5000);

    // read data
    uint8_t buff[20];
    const uint8_t count = sizeof(buff);
    // get our data
    uint8_t gotten = Rtc.GetMemory(buff, count);

    if (gotten != count) 
    {
        Serial.print("something didn't match, count = ");
        Serial.print(count, DEC);
        Serial.print(", gotten = ");
        Serial.print(gotten, DEC);
        Serial.println();
    }

    Serial.print("data read (");
    Serial.print(gotten);
    Serial.print(") = \"");
    // print the string, but terminate if we get a null
    for (uint8_t ch = 0; ch < gotten && buff[ch]; ch++)
    {
        Serial.print((char)buff[ch]);
    }
    Serial.println("\"");
    
    delay(5000);
}



void printDateTime(const RtcDateTime& dt)
{
    char datestring[20];

    snprintf_P(datestring, 
            countof(datestring),
            PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
            dt.Month(),
            dt.Day(),
            dt.Year(),
            dt.Hour(),
            dt.Minute(),
            dt.Second() );
    Serial.print(datestring);
}

Moderador:
Por favor, lee las Normas del foro y edita tu código/error usando etiquetas de código.
Ve a edición, luego selecciona todo el código que has publicado, lo cortas y click en </>


No puedes ponerle una y otra vez la hora y fecha de compilación!!
Eso es para 1 sola vez y con eso pones en hora el RTC
Luego comentas algo como esta linea

RtcDateTime compiled = RtcDateTime(__DATE__,__TIME__);

para que no te vuelva a setear el RTC y lo dejas funcionar con la hora y fechas que correspondan.

pero si esa linea ya la habia puesto en el void setup, segun a lo que investige el Rtc.GetDateTime(); es el que se encarga de actualizar la fecha y hora pero hay un error de libreria que aveces esa linea es la que resetea la fecha y hora y estuve buscando soluciones y la linea Rtc.SetDateTime(compiled); es la que tengo que poner en el void setup para solucionar ese error y la puse tal cual y comoquiera se me resetea. El codigo para programar el modulo lo saque de la libreria. Cuando descargue la libreria venian ejemplos de como poner el codigo y agarre el 1302 simple.

Estimado @epocramex
Cuando tu corres por primera vez un código para un RTC la forma de pasarle la fecha y hora es a través del la fecha y hora de compilación. Luego de lo cual, debes comantarlas.
Busca qué comentar y deja que el RTC provea la hora por su cuenta sin llamar de nuevo a fecha y hora VIEJAS porque si por alguna razón apagas y prendes el Arduino y se vuelve a ejectuar el setup y fija nuevamente los datos con la hora y fecha de compilación que crees que hará? Pues tendras fechas y hora de aquel momento. Y es porque las volviste a configurar.
Por eso te digo que las comentes.
No conozco tu librería por eso no doy mas detalles pero el ejemplo de la misma debe tener un comentario para que dejes de usar las lineas que no sirven una vez configurado el RTC.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.