Hola.
Con este código realizado al vuelo y pila instalada en mi rtc, la hora se introduce enviando una s por serial.
Después se mantiene independientemente de si se apaga.
Prueba a ver.
Saludos.
#include <DS1302.h>
/* Set the appropriate digital I/O pin connections */
uint8_t CE_PIN = 5;
uint8_t IO_PIN = 6;
uint8_t SCLK_PIN = 7;
/* Create buffers */
char buf[50];
char day[10];
/* Create a DS1302 object */
DS1302 rtc(CE_PIN, IO_PIN, SCLK_PIN);
void print_time()
{
/* Get the current time and date from the chip */
Time t = rtc.time();
/* Name the day of the week */
memset(day, 0, sizeof(day)); /* clear day buffer */
switch (t.day) {
case 1:
strcpy(day, "Sunday");
break;
case 2:
strcpy(day, "Monday");
break;
case 3:
strcpy(day, "Tuesday");
break;
case 4:
strcpy(day, "Wednesday");
break;
case 5:
strcpy(day, "Thursday");
break;
case 6:
strcpy(day, "Friday");
break;
case 7:
strcpy(day, "Saturday");
break;
}
/* Format the time and date and insert into the temporary buffer */
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day,
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
/* Print the formatted string to serial so we can see the time */
Serial.println(buf);
}
void setup()
{
Serial.begin(9600);
}
/* Loop and print the time every second */
void loop()
{
if(Serial.available())
gestionaComandos();
print_time();
delay(1000);
}
void gestionaComandos()
{
Time t;
if(Serial.read()=='s'){
Serial.print("Year?: ");
while(!Serial.available());
t.yr=Serial.parseInt();
Serial.println(t.yr);
Serial.print("Month?: ");
while(!Serial.available());
t.mon=Serial.parseInt();
Serial.println(t.mon);
Serial.print("Day?: ");
while(!Serial.available());
t.date=Serial.parseInt();
Serial.println(t.date);
Serial.print("Day of week? (1=Sunday-7=Saturday");
while(!Serial.available());
t.day=Serial.parseInt();
Serial.println(t.day);
Serial.print("Hour?: ");
while(!Serial.available());
t.hr=Serial.parseInt();
Serial.println(t.hr);
Serial.print("Minute?: ");
while(!Serial.available());
t.min=Serial.parseInt();
Serial.println(t.min);
t.sec=0;
}
rtc.write_protect(false);
rtc.halt(false);
rtc.time(t);
}