Pages: [1]   Go Down
Author Topic: SPI troubles - updated  (Read 277 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 117
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
« Last Edit: February 13, 2012, 09:04:12 pm by Verdris » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Quote
And if so, what's the point of the SPI library at all?
See above answer.

Quote
It seems like I'd much rather use the SPI library for ease of data transfer.
So, do that.

Quote
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.

Quote
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.

Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 117
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

Quote
Quote
And if so, what's the point of the SPI library at all?
See above answer.
Okay.

Quote
Quote
It seems like I'd much rather use the SPI library for ease of data transfer.
So, do that.
I'm gonna.

Quote
Quote
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?

Quote
Quote
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.

Code:
#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);
}
« Last Edit: February 13, 2012, 08:07:04 pm by Verdris » Logged

Pages: [1]   Go Up
Jump to: