Uno, official Ethernet shield and RTC1306 using SPI not working togheter

Hi ,
I'm experiencing similar situation similar to the one here: DUE + Ethernet Shield + RTC DS3234 = SPI Problem? - Networking, Protocols, and Devices - Arduino Forum using an Arduino Uno, an official Ethernet shield R3 and a DS1306 RTC. I put it in a different post since is based on different hardware/configuration.
I wrote a sketch to read the time from RTC and send it to the Net (using MQTT protocol but this wouldn't have any effect, anyway).

Now:

  1. Network part works well but I'm unable to read/write RTC registries;
  2. the RTC part works as expected if I use the UNO without the Ethernet shield, keeping the same pin connections;

My understanding is that on the Uno MOSI, MISO ans SCK are respectively on pin 11, 12, 13. Pin 10 is used as SS (or CE, Chip Enable, as sometimes is called) for the Ethernet shield.
My circuit (connected to the Ethernet shield pins) is the same as this one: Arduino, SPI and the DS1306 RTC - Page 1/4 with only one exception: the RTC CE is connected on pin AO, since 10 is already used to control the Ethernet shield.

Now:

  1. Network part works well but I'm unable to read/write RTC registries;
  2. the RTC part works as expected if I use the UNO without the Ethernet shield, keeping the same pin connections;

After some further experiments seems to me that:

  1. starting the SPI interface explicitly in the sketch as if I were working with RTC alone enables RTC but somehow prevents the Ethernet shield to do its job;
  2. not starting the SPI explicitly allows the Ethernet shield tho operate properly but RTC seems to be completely unavailable: no read or write operations can be done.

The code i used to set up SPI is the following, put in the setup():

  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE1);

I'm not posting the whole sketch since it is a bit messy, I have to clean it up a bit..

Does anybody have a clue on what is happening?
Is there an issue with SPI? I found some (old) post where people says that the Ethernet chip doesn't handle SPI properly, but is it still true?

Thank you

This is the problem.

 SPI.setDataMode(SPI_MODE1);

You will need to switch the data mode when using the RTC and ethernet shield together. The devices on the ethernet shield are both SPI_MODE0. I would use something like this:

 // change to mode 1 for RTC
 SPI.setDataMode(SPI_MODE1);  

 // do your RTC SPI stuff

 // then change back to mode 0 for w5100 and SD
 SPI.setDataMode(SPI_MODE0);

OOOK, thank you SurferTim, now it works :slight_smile: : following your suggestion I just added a couple of line of codes switching to mode 1 whenever I need to do RTC SPI stuff and then reverting to mode 0 when finished.

Thanks, SurferTim - You are the man!!