Llevo mucho tiempo analizando y estudiando el foro y todas las indicaciones que se hacen.
Estoy inmerso en un proyecto, para controlar un acuario, y tengo un problema que no se como resolver después de darle muchas muchas vueltas y cambiar mucho el código, os comento para ver si me podeis ayudar.
Tengo un Arduino Mega
Modulo Tiny RTC como este
Conexiones:
SQ -->
DS --> Digital 2
SCL --> 21
SDA --> 20
VCC--> 5V
GND --> GND
BAT -->
Consigo inicializar la hora y la fecha perfectamente y cuando inicializo el código con el botón del reset se respeta la hora perfectamente y continua desde donde estaba. El problema viene cuando lo desconecto de la alimentación se inicializa a [01/01/2000 00:00:00] y ya no avanza nada.
Os dejo dos código diferente en los dos tengo el mismo problema.
#include <Wire.h>
//////////////////////////////////////////////////////////////////////////////////////// RELOJ
//-------------------------------------- Libreria que se usa para fijar y grabar la hora en la memoria
#include <DS1307new.h>
uint16_t startAddr = 0x0000; // Start address to store in the NV-RAM
uint16_t lastAddr; // new address for storing in NV-RAM
uint16_t TimeIsSet = 0xaa55; // Helper that time must not set again
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
digitalWrite( 2, HIGH); // Test of the SQW pin, D2 = Pullup on
Wire.begin(); // Inicia el puerto I2C
RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08
//Descomentar estas DOS lineas para fijar la hora inicialmente.
//TimeIsSet = 0xffff;
//RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet != 0xaa55)
{ RTC.stopClock();
//Hora que se quiere fijar, si no tiene ya una fijada.
RTC.fillByYMD(2015,8,5); //aaaa,m,d
RTC.fillByHMS(17,14,0); //hh,m,s
RTC.setTime();
TimeIsSet = 0xaa55;
RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.startClock();
}
else
{ RTC.getTime();
}
RTC.ctrl = 0x00;
RTC.setCTRL();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// put your main code here, to run repeatedly:
String Cadena;
RTC.getTime();
Cadena = "";
if( RTC.day<10 ) Cadena += String("0");
Cadena += String(RTC.day) + "/";
if( RTC.month<10) Cadena += String("0");
Cadena += String(RTC.month) + "/" + String(RTC.year);
Cadena += " ";
if( RTC.hour<10) Cadena += String("0");
Cadena += String(RTC.hour) + ":";
if( RTC.minute<10) Cadena += String("0");
Cadena += String(RTC.minute) + ":";
if( RTC.second<10 ) Cadena += String("0");
Cadena += String(RTC.second);
Serial.println( Cadena );
Grabar_Hora();
delay(1000);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------- Graba la hora en el dispositivo del reloj ----------------------------------
void Grabar_Hora(){
RTC.getRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
RTC.setRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet == 0xaa55) // check if the clock was set or not
{ Serial.println(" Correcto la grabacion de la hora del reloj");
}
else
{ Serial.println("Fallo en la grabacion de la hora del reloj ( ERR-03 Reloj )");
}
}
#include <Wire.h>
//////////////////////////////////////////////////////////////////////////////////////// RELOJ
//-------------------------------------- Libreria que se usa para fijar y grabar la hora en la memoria
#include <DS1307new.h>
uint16_t startAddr = 0x0000; // Start address to store in the NV-RAM
uint16_t lastAddr; // new address for storing in NV-RAM
uint16_t TimeIsSet = 0xaa55; // Helper that time must not set again
//-------------------------------------- Otra libreria que se usa para leer las horas
#include <RTClib.h>
RTC_DS1307 RTCLeer;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
digitalWrite( 2, HIGH); // Test of the SQW pin, D2 = Pullup on
Wire.begin(); // Inicia el puerto I2C
RTC.setRAM(0, (uint8_t *)&startAddr, sizeof(uint16_t));// Store startAddr in NV-RAM address 0x08
//Descomentar estas DOS lineas para fijar la hora inicialmente.
//TimeIsSet = 0xffff;
//RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet != 0xaa55)
{ RTC.stopClock();
//Hora que se quiere fijar, si no tiene ya una fijada.
RTC.fillByYMD(2015,8,5); //aaaa,m,d
RTC.fillByHMS(17,14,0); //hh,m,s
RTC.setTime();
TimeIsSet = 0xaa55;
RTC.setRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
RTC.startClock();
}
else
{ RTC.getTime();
}
RTC.ctrl = 0x00;
RTC.setCTRL();
RTCLeer.begin(); // Inicia la comunicación con el RTC
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
// put your main code here, to run repeatedly:
String Cadena;
DateTime now;
now = RTCLeer.now(); // Obtiene la fecha y hora del RTC
Cadena = "";
if( now.day()<10 ) Cadena += String("0");
Cadena += String(now.day()) + "/";
if( now.month()<10) Cadena += String("0");
Cadena += String(now.month()) + "/" + String(now.year());
Cadena += " ";
if( now.hour()<10) Cadena += String("0");
Cadena += String(now.hour()) + ":";
if( now.minute()<10) Cadena += String("0");
Cadena += String(now.minute()) + ":";
if( now.second()<10 ) Cadena += String("0");
Cadena += String(now.second());
Serial.println( Cadena );
Grabar_Hora();
delay(1000);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------- Graba la hora en el dispositivo del reloj ----------------------------------
void Grabar_Hora(){
RTC.getRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
RTC.setRAM(0, (uint8_t *)&lastAddr, sizeof(uint16_t));
RTC.getRAM(54, (uint8_t *)&TimeIsSet, sizeof(uint16_t));
if (TimeIsSet == 0xaa55) // check if the clock was set or not
{ Serial.println(" Correcto la grabacion de la hora del reloj");
}
else
{ Serial.println("Fallo en la grabacion de la hora del reloj ( ERR-03 Reloj )");
}
}
Estoy perdido si me podéis ayudar os lo agradecería
Un saludo

