icearrow:
Goodevening everybody.
I'm having an issue with an Arduino Due compatible board (TaijiUino Due) and the onboard RTC with DUE_RTC library.
I use this bouard since the VBU pin (backup battery pin) of the SAM3X is available to be connected to an external 3V battery in order to keep the RTC alive and keep counting time even with no power.
The RTC itself works properly and fine, I've programmed the board using the Due RTC_Simple_Sample Sketch and actually the RTC keeps time even when no USB or DC cable is connected.
Probles starts as soon as I press the onboard reset button, once I restart the board the internal date and time are resetted to 01/01/2007 00:00:00.
Do You know if there is a way to avoid that? It's not to comfortable to update the RTC date and time after every reset.
Thank You
Hello icearrow,
I don't know what code you are using but I use to use two codes. One for the initial settings and the other one to read the timestamp. Here the codes:
Code to set the day/time:
#include "Wire.h"
#define DS1307_ADDRESS 0x68
#define DS1307_I2C_ADDRESS 0x68
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
void setup(){
Wire.begin();
Serial.begin(9600);
second = 0;
minute = 43;
hour = 16;
dayOfWeek = 2;
dayOfMonth = 14;
month = 9;
year = 15;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
void loop(){
printDate();
delay(1000);
}
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
void printDate(){
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(B00010000); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
Here the code to read the date/time:
#include "Wire.h"
#define DS1307_ADDRESS 0x68
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
printDate();
delay(1000);
}
byte bcdToDec(byte val) {
return ( (val/16*10) + (val%16) );
}
void printDate(){
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
int second = bcdToDec(Wire.read());
int minute = bcdToDec(Wire.read());
int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
int monthDay = bcdToDec(Wire.read());
int month = bcdToDec(Wire.read());
int year = bcdToDec(Wire.read());
Serial.print(month);
Serial.print("/");
Serial.print(monthDay);
Serial.print("/");
Serial.print(year);
Serial.print(" ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
}
-p