Duplicating RTC time to another RTC

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);
}

What hardware are we talking about?

Which pins should be duplicated to both, and which pins need their own signal?

I guess that "RTC" is connected by SPI, so you connect the bus lines to both chips but have separate CS signals for each one.

As this was your only question I'm wondering why you not simply asked about the connections for an SPI bus.

Thanks for the reply! I didn't want to connect the MISO line to both RTCs, I only have one MISO line and it is connected to the master. I think my wiring is correct.

My other question was about my code, I wanted to know if duplicateTimeDate() will correctly copy the RTC signal. I know that the reading master part is correct:

digitalWrite(MASTER_SS, LOW);
SPI.transfer(i+0x00); 
data = SPI.transfer(0x00);        
digitalWrite(MASTER_SS, HIGH);

But I don't know if the write to slave part is correct:

digitalWrite(SLAVE_SS, LOW);
SPI.transfer(i+0x00);
SPI.transfer(data); 
digitalWrite(SLAVE_SS, HIGH);

Right now, the duplication code does nothing, the slave RTC isn't modified at all. What is wrong with this?

I didn't want to connect the MISO line to both RTCs

Why not? The whole point to the SPI bus is that it is a bus. You connect the Arduino MISO to MOSI on both RTC, and MOSI on the Arduino to MISO on both RTC, and all three SCLK are connected together. Then the CS/SS on the RTCs must connect to separate digital pins on the Arduino. Wikipedia has a diagram showing this to the right of the section called "Daisy chain configuration".
If you haven't done this, your code hasn't got a prayer of working.

Pete

I tied the MISO lines together. I thought it would be like tying two outputs together, but it works now. It only works on the first time, but that's another issue. Thanks for your help!

I tied the MISO lines together.

Which MISO lines?

Pete

and which RTCs are you using?

Pete