Using SD card with another SPI device

Hello! I got an external ADC device that communicates to my ArduinoMega over SPI, and I would like to log the data from the ADC into a SD card.

The SD card reader is a standard xBee shield with SD card, where ChipSelect=4. The ADC is a LTC1864, very easy to use. See the attachment for its timing diagram, where CONV=chipSelect_ACD=11 and SD0=MISO.
ADC datasheet: http://cds.linear.com/docs/en/datasheet/18645fb.pdf

Without the SD cad, I am able to get reading from the ADC as follows:

#include <SPI.h>

const int  chipSelect_ACD=11;
void setup() {
  SPI.begin();
  Serial.begin(9600);
}
void loop() {
  String dataString = "";
  dataString+=String(adc());
  Serial.println(dataString);
  delay(5000);
}

unsigned int adc(){
  byte inByte=0;
  unsigned int result=0;
  digitalWrite(chipSelect_ACD, HIGH);
  delay(1);
  digitalWrite(chipSelect_ACD, LOW);
  result = SPI.transfer(0x00);
  result = result << 8;
  inByte = SPI.transfer(0x00);
  result = result | inByte;
  return result;
}

However, when I try to store the dataString into SD card, my Arduino freezes whenever it reaches adc().

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

const int chipSelect_SD=4, chipSelect_ACD=11;
File myFile;

void setup() {
  SPI.begin();
  Serial.begin(9600);
  if (!SD.begin(chipSelect_SD)){
    Serial.println("sd card initialization failed.");
  }
}
void loop() {
  String dataString = "";
  dataString+=String(adc());
  Serial.println(dataString);
  File dataFile=SD.open("DATA.txt", FILE_WRITE);
  if(dataFile){
    Serial.println(dataString);
    dataFile.println(dataString);
    dataFile.close();
  }
  else{
    Serial.println("error opening DATA.txt");
  }
  delay(5000);
}

unsigned int adc(){
  byte inByte=0;
  unsigned int result=0;
  digitalWrite(chipSelect_ACD, HIGH);
  delay(1);
  digitalWrite(chipSelect_ACD, LOW);
  result = SPI.transfer(0x00);
  result = result << 8;
  inByte = SPI.transfer(0x00);
  result = result | inByte;
  return result;
}

I am wondering if you guys are able to spot what is wrong with my code? CS for SD is 4 and CS for ADC is 11. Thank you!

(deleted)

Thanks for your response. In your code, I see that you pull chipSelect_ACD to HIGH when trying to access the SD card. I tried your code, but it doesn't seem to work. Is the SPI chip select bus for the Arduino active low? Thanks.

I don't understand what can cause the Arduino to froze. I am assuming the SPI.transfer conflicts with something in SD.h library? Am I not suppose to use both SD card and SPI device together?

  String dataString = "";
  dataString+=String(adc());
  Serial.println(dataString);

Why are you pissing away resources uselessly? The println() method KNOWS how to print the value that adc() returns. It doesn't need you to wrap it in a String.

Thanks for your reply. I am using a MUX to take samples from 16 sensors and I just thought it would be nicer to pack all the data into one string and print only that string to both the serial monitor and SD card.

But I don't believe it has to do with the SPI problem I am encountering. Thank you though.

I am assuming the SPI.transfer conflicts with something in SD.h library?

How can it? SPI.transfer() blocks until it is done. All the SD functions are blocking, too.

More likely, SPI.begin() is setting up SPI one way, and the SD card needs it set up another way. The SD class changes the SPI parameters the way it needs them. The adc() function does not.

Thanks. So basically, SD.h and SPI.h cannot exist together, am I right? Is there a solution to the problem? SPI uses MISO, MOSI, SCK and CS, where SD card is default to be 4 and my ADC is 11. I don't quite understand what kind of "parameters" changes may have cause the conflict. Can you be a little bit more specific?

Does anyone had experience using SD card with another SPI device? Thanks.

So basically, SD.h and SPI.h cannot exist together, am I right?

No, you aren't. What you can't do is mix SPI protocols without knowing what each SPI device expects. The SD class handles the protocol for the SD device. Your other code does NOT.

SPI uses MISO, MOSI, SCK and CS, where SD card is default to be 4 and my ADC is 11.

So? At what speed? With which endianness? There is a lot more to SPI than "let the hardware handle it".

Thanks. So I think the best approach is to buy another arduino to handle the ADC and send the data to my old Mega to store in SD card? I also want to setup a ethernet server at some point which also use SPI. Do I need a third arduino?

Do you have an idea of how to transform the output of your code to voltage?