This is my first post here, I have worked with other boards but this is my first time using an Arduino.
My goal is to have a setup so that I can plug in an RTC, flip a switch, and the Arduino will load the time from a "master" RTC to the one that was plugged in ("slave" RTC). So far, I have a single RTC plugged in to the Arduino, and I can successfully load the compile time when the switch is flipped. I am working on getting the time from the master RTC and copying it to the slave.
I am not sure how to wire the Arduino to the master RTC and the slave RTC. Which pins should be duplicated to both, and which pins need their own signal? I am also confused on how to control which RTC is being read from, and how to choose which to write to.
Some assumptions:
-Both RTCs will have batteries, but the Arduino will not always have power
-I am using an Arduino Ethernet
Below is my code. I commented out the sections that format the time. This code only correctly reads the master RTC, the duplication code seems to be failing.
#include <SPI.h>
const int MASTER_SS=8; //chip select
const int SLAVE_SS=2;
const int BUTTON=7;
bool timeIsLoaded = false;
void setup() {
Serial.begin(9600);
RTC_init(MASTER_SS);
//day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
}
void loop() {
if(digitalRead(BUTTON) == 1) //RIGHT
{
//Serial.println('1');
Serial.println(ReadTimeDate()); //print the time every second
delay(1000);
timeIsLoaded = false;
}
else //LEFT
{
//Serial.println('0');
if(timeIsLoaded == false)
{
RTC_init(SLAVE_SS);
DuplicateTimeDate();
timeIsLoaded = true;
Serial.println("Uploaded.");
}
}
}
//=====================================
int RTC_init(int SS){
pinMode(BUTTON,INPUT);
pinMode(SS,OUTPUT); // chip select
// start the SPI library:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3); // both mode 1 & 3 should work
//set control register
digitalWrite(SS, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x04); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(SS, HIGH);
delay(10);
}
//=====================================
struct day_and_time{
int s,mi,h,d,mo,y;
}; //used to bundle time into one object
int DuplicateTimeDate()
{
unsigned int data;
for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(MASTER_SS, LOW);
SPI.transfer(i+0x00);
data = SPI.transfer(0x00);
digitalWrite(MASTER_SS, HIGH);
delay(10);
digitalWrite(SLAVE_SS, LOW);
SPI.transfer(i+0x00);
SPI.transfer(data);
digitalWrite(SLAVE_SS, HIGH);
}
}
//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year
for(int i=0; i<=6;i++){
if(i==3)
i++;
digitalWrite(MASTER_SS, LOW);
SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00);
digitalWrite(MASTER_SS, HIGH);
//this just converts back to decimal from BCD
int a=n & B00001111; //ones digit
//get tens digit
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode
if(b==B00000010)
b=20;
else if(b==B00000001)
b=10;
TimeDate[i]=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate[i]=a+b*10;
}
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate[i]=a+b*10;
}
else{
int b=(n & B01110000)>>4;
TimeDate[i]=a+b*10;
}
}
//now generate string from the TimeDate array
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" "); //doesn't like '\t'
temp.concat(TimeDate[2]);
temp.concat(":") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}