How to read data with multiple SPI slaves

I’d like to use a micro SD shield and DS 3237 RTC devices together using SPI protocol with a FIO.
When I had tried the SD and RTC separately with both devices connected, the sketch runs fine,
However, when I have tried them both, it does not work.
The following code has problem to write something on SD memory. “Not OK” message is printed on the serial terminal. I attached the source code.

#include <SPI.h>
#include <SD.h>

#define ECHO_TO_SERIAL 1

#define RTC_SS 9 // slave select for RTC
#define SD_SS 8 // slave select for SD

#define MOSI 11
#define MISO 12
#define SCK 13

File logFile;

void setup() {

// pinMode(CS, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SCK, OUTPUT);
// pinMode(10, OUTPUT);

pinMode(SD_SS, OUTPUT);
digitalWrite(SD_SS, HIGH);

pinMode(RTC_SS, OUTPUT);
digitalWrite(RTC_SS, HIGH);

Serial.begin(9600);

//disable the RTC
//pinMode(RTC_SS, OUTPUT);
// digitalWrite(RTC_SS, HIGH);

SD_init();

SPI.begin();

//SPI.setClockDivider(SPI_CLOCK_DIV8);

RTC_init();
//day(1-31), month(1-12), year(0-99), hour(0-23), minute(0-59), second(0-59)
SetTimeDate(21,5,14,12,0,0);

}

void loop() {

Serial.println(ReadTimeDate());

//sd_loop();

fileTest();

delay(2000);
}

void fileTest() {
if (digitalRead(RTC_SS) == LOW) {
Serial.println(“RTC is LOW1”);
digitalWrite(RTC_SS, HIGH);
}

if (digitalRead(SD_SS) == HIGH) {
Serial.println(“SD is HIGH1”);
digitalWrite(SD_SS, LOW);
}

if (logFile) {
if (logFile.println(“Hello world 3”)) {
Serial.println(" File wirte OK");
logFile.flush();
}
else Serial.print(" Write Not OK ");
} else Serial.println(“logFile is NULL”);

digitalWrite(SD_SS, HIGH);
Serial.println();
}

int SD_init() {

if (digitalRead(SD_SS) == HIGH) {
Serial.println(“SD is HIGH2”);
digitalWrite(SD_SS, LOW);
}

if (digitalRead(RTC_SS) == LOW) {
Serial.println(“RTC is LOW”);
digitalWrite(RTC_SS, HIGH);
}

if (!SD.begin(SD_SS)) {
Serial.println(“initialization failed!”);
return -1;
} else Serial.println(“initialization done.”);

// create a new file
char filename = “LOGGER00.xls”;
for (uint8_t i = 0; i < 300; i++) {
filename[6] = i/10 + ‘0’;
filename[7] = i%10 + ‘0’;
if (! SD.exists(filename)) {
// only open a new file if it doesn’t exist
logFile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logFile) {
#if ECHO_TO_SERIAL
Serial.println(“Could not create file”);
delay(20);
#endif //ECHO_TO_SERIAL
return -1;
}

#if ECHO_TO_SERIAL
Serial.print("Logging to: ");
Serial.println(filename);
#endif //ECHO_TO_SERIAL

logFile.println(“date \t time \t\t\t temp”);

logFile.println("Hello World ");
logFile.flush();
//logFile.close();
//

logFile.println(“Hello World 2”);
logFile.flush();

#if ECHO_TO_SERIAL
Serial.println(“date \t time \t\t\t temp”);
#endif //ECHO_TO_SERIAL
}

//=====================================
int RTC_init(){
pinMode(RTC_SS,OUTPUT); // chip select
// start the SPI library:
// SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE1); // both mode 1 & 3 should work
//set control register
digitalWrite(RTC_SS, LOW);
SPI.transfer(0x8E);
SPI.transfer(0x60); //60= disable Osciallator and Battery SQ wave @1hz, temp compensation, Alarms disabled
digitalWrite(RTC_SS, HIGH);
delay(10);
}
//=====================================
int SetTimeDate(int d, int mo, int y, int h, int mi, int s){
int TimeDate [7]={s,mi,h,0,d,mo,y};
for(int i=0; i<=6;i++){
if(i==3)
i++;
int b= TimeDate*/10;*
int a= TimeDate_-b10;_
_
if(i==2){_
_
if (b==2)_
_
b=B00000010;_
_
else if (b==1)_
_
b=B00000001;_
_
} _
_ TimeDate= a+(b<<4);*_

* digitalWrite(RTC_SS, LOW);
_ SPI.transfer(i+0x80);
SPI.transfer(TimeDate); _

digitalWrite(RTC_SS, HIGH);
_ }
}
//=====================================
String ReadTimeDate(){
String temp;
int TimeDate [7]; //second,minute,hour,null,day,month,year _

if (digitalRead(SD_SS) == LOW) {
_ Serial.println(“SD is LOW”);_
digitalWrite(SD_SS, HIGH);
_ }*_

* for(int i=0; i<=6;i++){*
* if(i==3)*
* i++;*
* digitalWrite(RTC_SS, LOW);
_ SPI.transfer(i+0x00);
unsigned int n = SPI.transfer(0x00); _

digitalWrite(RTC_SS, HIGH);
_ int a=n & B00001111;
if(i==2){
int b=(n & B00110000)>>4; //24 hour mode*

* if(b==B00000010)
b=20;
else if(b==B00000001)
b=10;
TimeDate=a+b;
}
else if(i==4){
int b=(n & B00110000)>>4;
TimeDate=a+b10;

* }
else if(i==5){
int b=(n & B00010000)>>4;
TimeDate=a+b10;

* }
else if(i==6){
int b=(n & B11110000)>>4;
TimeDate=a+b10;

* }
else{
int b=(n & B01110000)>>4;
TimeDate=a+b10;

* }
}
temp.concat(TimeDate[4]);
temp.concat("/") ;
temp.concat(TimeDate[5]);
temp.concat("/") ;
temp.concat(TimeDate[6]);
temp.concat(" “) ;
temp.concat(TimeDate[2]);
temp.concat(”:") ;
temp.concat(TimeDate[1]);
temp.concat(":") ;
temp.concat(TimeDate[0]);
return(temp);
}*_

Please provide links to the hardware used and edit your post and include code tags to make the code readable.

My guess is that your two devices need the SPI bus reconfigured before every access to them because the don't use the same SPI mode. I couldn't find a datasheet for the DS3237 though, is that really the correct part number?

Sorry for my wrong part number.
My hardware consists of an Arduino FIO, DeadOn RTC -DS3234 breakout, and breakout board for micro SD transflash of Sparkfun. I tried with a Arduino Pro 3.3v 8MHz, DeadOn RTC -DS3234 breakout , and a micro SD shield . But it does not work as well. I configured the two SPI devices shares D11, D12, and D13 pins , and one’s CS is D10 pin and the other’s CS is D9.
DeadOn RTC-DS3234- https://www.sparkfun.com/products/10160
micro SD transflash - SparkFun microSD Transflash Breakout - BOB-00544 - SparkFun Electronics

Please help me.
Byungsoon

sd_rtc3234_0521.ino (4.87 KB)

Then pylon is correct. The SD card is SPI Mode0, and yout RTD is Mode1. You must switch modes when accessing each device.

  // before enabling the RTC slave select when accessing the RTC
  SPI.setDataMode(SPI_MODE1); 

  // before calling any SD function
  SPI.setDataMode(SPI_MODE0);

edit: The way I do it is change the mode before accessing the Mode 1 device, then change the mode back to Mode 0.

  SPI.setDataMode(SPI_MODE1); 
  // do your RTC stuff here, then set the mode back to 0
  SPI.setDataMode(SPI_MODE0);