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);
}