Created Fri, 13 Dec 2013 11:00:50 +0000 by ajitnayak
Fri, 13 Dec 2013 11:00:50 +0000
Dear all,
The output values of RTC wire connected and disconnected as below. I can check when RTC is faulted . SO if rtc disconnected i want to run arduino on TIMER time. I know if arduino get off it set to default values, For that i kept backup such away that it never shutdown.
Arduino uno r3 board i am using. SO help me in coding.
I am trying to do this :
Pseudo code: check(if rtc failed or not) if failed: Read arduino time/self timer else :read RTC time and sync for every 5 min
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
void setup(){
Wire.begin();
Serial.begin(9600);
setDateTime(); //MUST CONFIGURE IN FUNCTION
}
void loop(){
printDate();
delay(1000);
}
void setDateTime(){
byte second =25; //0-59
byte minute =30; //0-59
byte hour = 6; //0-23
byte weekDay =1; //1-7
byte monthDay =5; //1-31
byte month = 12; //1-12
byte year =13; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
void printDate(){
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
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());
/* int second = 11;
int minute =18;
int hour = 18;
int weekDay = 1;
int monthDay = 2;
int month = 11;
int year = 12;
*/
//print the date EG 3/1/11 23:59:59
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);
}
Thu, 19 Dec 2013 23:31:51 +0000
You have already pretty much answered your own question by creating a flowchart. That is always a good place to start when you aren't sure how you should implement something.
This not being an Arduino forum, but a chipKIT forum (yes, they are similar, but not the same) some of the things you will be doing are beyond our scope (ask those questions on the Arduino forum).
I would tackle this problem by having a simple single interface to the time. A "getTime()" function, which first tries to get the time from the RTC. If that fails it then falls back to getting the time from wherever else it is you want to get it from. As soon as one of your time sources succeeds it can return. You could have each method of getting the time in its own function, so your pseudocode could look something like:
1. Get time from RTC
2. Is time valid? If so, set global time from obtained time and return.
3. Get time from internal clock
4. Set global time from obtained time and return.