SD shield & MCP 3204 communication problem

Hello All,
i'm using MCP 3204 12 Bit ADC (Arduino Playground - MCP3208), arduino and Sparkfun's micro SD shield (SparkFun microSD Shield - DEV-12761 - SparkFun Electronics) . Both of the SD shield and MCP 3204 are use SPI protocol. (i'm using SD library which comes with 022). However, i can not use both SPI device efficiently. I wanna log ADC data to SD card.

SD shield uses pin 8 as a chip seelct pin and MCP uses pin 10 as a chip select pin. However, when i turn it on ADC always read 4096 whatever the real value is. When i turn it on without SD shield, it reads correct values. DO you have any idea about this problem?

Thank you!

Here is the code:

#include <SD.h>
#include <Wire.h>
#include <DS1307.h>

#define SELPIN 10 //Selection Pin
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//Clock
const int chipSelect = 8;
int readvalue1=0, readvalue2=0;
int hr=0, minu=0, sec=0;
//#include <WProgram.h>
void setup(){
//set pin modes
pinMode(SELPIN, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK, OUTPUT);
//disable device to start with
digitalWrite(SELPIN,HIGH);
digitalWrite(DATAOUT,LOW);
digitalWrite(SPICLOCK,LOW);
//----------------------------CARD INIT
Serial.begin(9600);

Serial.print("Initializing SD card...");
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
Serial.println(digitalRead(chipSelect));
delay(1000);

}

void loop() {
String dataString = "";
clockread();
readvalue1 = read_adc(1);
readvalue2=read_adc(2);
dataString += String(hr);
dataString += "/";
dataString += String(minu);
dataString += "/";
dataString += String(sec);
dataString += "\t";
dataString += String(readvalue1);
dataString += "\t";
dataString += String(readvalue2);
File dataFile = SD.open("zi.log", FILE_WRITE);

if (dataFile) {
dataFile.println(dataString);
dataFile.close();

Serial.println(dataString);

}

else {
Serial.println("error opening datalog.txt");
}
}

//-----------------------------FUNCTIONS-------------------------------------

//------------- ADC READ FUNCTION----------------
int read_adc(int channel){
int adcvalue = 0;
byte commandbits = B11000000; //command bits - start, mode, chn (3), dont care (3)

//allow channel selection
commandbits|=((channel-1)<<3);
digitalWrite(chipSelect,HIGH);
digitalWrite(SELPIN,LOW); //Select adc
// setup bits to be written
for (int i=7; i>=3; i--){
digitalWrite(DATAOUT,commandbits&1<<i);
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}

digitalWrite(SPICLOCK,HIGH); //ignores 2 null bits
digitalWrite(SPICLOCK,LOW);
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);

//read bits from adc
for (int i=11; i>=0; i--){
adcvalue+=digitalRead(DATAIN)<<i;
//cycle clock
digitalWrite(SPICLOCK,HIGH);
digitalWrite(SPICLOCK,LOW);
}
digitalWrite(SELPIN, HIGH); //turn off device
digitalWrite(chipSelect,LOW);
return adcvalue;
}

void clockread ()
{
hr=RTC.get(DS1307_HR,true); //read the hour and also update all the values by pushing in true
minu=RTC.get(DS1307_MIN,false);//read minutes without update (false)
sec=RTC.get(DS1307_SEC,false);//read seconds

}

You are prevent4ed from posting links only in your first post. Since this isn't your first post, you CAN (and should have) post(ed) links.

Care to try again?

Sorry i thought that you could understand it, so i ignored it. No problem i will add necessary links.

http://www.sparkfun.com/tutorials/172

pinMode(10, OUTPUT); //Pin 10 must be set as an output for the SD
communication to work.

MCP uses pin [glow]10[/glow] as a chip select pin.

Looks like you should have checked this out BEFORE buying.

const int chipSelect = 8;
if (!SD.begin(chipSelect))

Library is flexible. 10 is default chip select pin but u can revise it.

I think i solved the problem. The SPI library expects D10 to be configured as an output (even if it's not being used!). i'll try changing CS pin different from 10

The SPI library expects D10 to be configured as an output

That's actually the ATmega's SPI hardware expecting it.

-j