SPI troubles - updated

Hi All,

In the past, the SPI parts i've used in projects had ready-made libraries so I could focus on doing science and not worry about spending too much time getting the equipment to work. But now, I've added a new ADC to my setup. I have the circuit completed properly. Datasheet:

http://www.analog.com/static/imported-files/data_sheets/AD7714.pdf

Now, I know it can interface via SPI. I also know how to send commands via such as SPI.transfer(0x5F), which for this chip sets up the next operation to be a read from the data register. However, I'm not too sure about how to read data using SPI.transfer().

So, I have questions. I've seen a few SPI device codes that communicate with sequences of digitalWrite() and digitalRead() rather than the actual SPI commands. Is this preferable? And if so, what's the point of the SPI library at all? It seems like I'd much rather use the SPI library for ease of data transfer.

How do I read data using SPI.transfer()? I've seen some code that looks like dataincoming=SPI.transfer(0); so does that mean passing 0 as SPI.transfer()'s argument means "I want to read this data now"? The data register from this IC is 24 bits long, transferred MSB first. I've read that the SPI library only likes 8 bits at a time, so should I set up a loop such as

for(int i=0; i<3;i++)
{
  datareceived[i]=SPI.transfer(0);
  data = datareceived[i] << (16-(i*8));
}

I'm not in the lab until later today, and once I get in I can post my test code. So far I'm able to write to all the calibration and mode registers, and correctly poll the DRDY pin and only execute a read when appropriate, but after that I get a constant stream of 0 for my output, even when I have a function generator giving the input appropriate signals.

I've seen a few SPI device codes that communicate with sequences of digitalWrite() and digitalRead() rather than the actual SPI commands. Is this preferable?

You are asking if software SPI is better than hardware SPI. If it was as good, do you think hardware SPI would be a feature of the chip?

And if so, what's the point of the SPI library at all?

See above answer.

It seems like I'd much rather use the SPI library for ease of data transfer.

So, do that.

How do I read data using SPI.transfer()?

A byte is read in for every byte sent out. SPI.transfer() returns a value. You would discard the first value returned, since it is not in response to anything sent. You would send bogus data at the end, to get the last value.

I've seen some code that looks like dataincoming=SPI.transfer(0); so does that mean passing 0 as SPI.transfer()'s argument means "I want to read this data now"?

See above.

PaulS:

I've seen a few SPI device codes that communicate with sequences of digitalWrite() and digitalRead() rather than the actual SPI commands. Is this preferable?

You are asking if software SPI is better than hardware SPI. If it was as good, do you think hardware SPI would be a feature of the chip?

I am new to SPI. I didn't know the difference.

And if so, what's the point of the SPI library at all?

See above answer.

Okay.

It seems like I'd much rather use the SPI library for ease of data transfer.

So, do that.

I'm gonna.

How do I read data using SPI.transfer()?

A byte is read in for every byte sent out. SPI.transfer() returns a value. You would discard the first value returned, since it is not in response to anything sent. You would send bogus data at the end, to get the last value.

So if I call SPI.transfer(0) three times, it sends out 24 bits of 0 (which is ignored by the ADC) and returns all the bits that the ADC sends back?

I've seen some code that looks like dataincoming=SPI.transfer(0); so does that mean passing 0 as SPI.transfer()'s argument means "I want to read this data now"?

See above.

Okay then.

Here's my current test code that returns nothing but 0. EDIT: This is the updated code, based on what channel I want to read, etc. Still returning all 0's.

#include <SPI.h>

const int SSP = 10;
const int dataReady = 7;
const int reset = 3;

unsigned long advalue;

void setup()
{
  pinMode(SSP, OUTPUT);
  pinMode(dataReady,INPUT);
  pinMode(reset,OUTPUT);
  digitalWrite(reset,LOW);
  digitalWrite(reset,HIGH);
  SPI.begin();
  SPI.setDataMode(SPI_MODE1);
  SPI.setBitOrder(MSBFIRST);
  Serial.begin(115200);
  
  digitalWrite(SSP,LOW);
  SPI.transfer(0x24); // setup and calibration
  SPI.transfer(0xCF);
  SPI.transfer(0x34);
  SPI.transfer(0xA0);
  SPI.transfer(0x14);
  SPI.transfer(0x20);
  digitalWrite(SSP,HIGH);
  
  delay(100);
}

void loop()
{
  if(digitalRead(dataReady) == LOW)
  {
    Serial.print("reading "); Serial.println(analogRead(1));
    digitalWrite(SSP,LOW);
    advalue = readChannel();
    //Serial.println(advalue);
    delay(500);
  }
}

unsigned long readChannel()
{
  byte inByte[3] = {0x01};
  unsigned long result;
  SPI.transfer(0x5C);
  byte junk = SPI.transfer(0x00);
  for(int i=0; i<3; i++)
  {
    inByte[0] = SPI.transfer(0x00);
  }
  digitalWrite(SSP,HIGH);
  result = (inByte[0] << 16) + (inByte[1] << 8) + inByte[2];
  Serial.print((int)junk); Serial.print(" "); Serial.print((int)inByte[0]); Serial.print(" "); Serial.print((int)inByte[1]); Serial.print(" "); Serial.println((int)inByte[2]);
  return(result);
}