Hi all,
The various tutorials and example sketches for SPI are very sparse at best, and I've been banging rocks together over this problem for some days.
I need a good tutorial or information for communicating with an external IC that has various registers. The chip in particular is Analog Devices AD7714Y Analog to Digital converter.
http://www.analog.com/static/imported-files/data_sheets/AD7714.pdf
In general, I'd like to know more about using the Arduino's SPI library to communicate. How data is transmitted, received, and interpreted. I'm fairly certain I have a handle on transmitting from Arduino to the ADC, but receiving information isn't really working.
My problem is basically in reading information. The way I THINK this works is that inByte[j] stores each byte received into inByte[0]-inByte[2]. Then they're added up using a fancy line of bit-shifting, based on the resolution I choose for the ADC (can be either 16- or 24-bit). Then, for debugging purposes, I Serial.print() each value stored in inByte[j] as well as their total.
The problem is I get an infinite stream of
92 92 92 6052956
regardless of the value on the ADC input pins. I should mention I have a function generator feeding it A*sin(t) + A, where A is 250mv.
The funny thing I notice is that the command for reading from the data register is SPI.transfer(0x5C); and 0x5C is 92 in binary.
So, if there's a general resource for learning about communicating with ICs via SPI, could someone point me to it? Otherwise, maybe someone here could help me with my particular code - it would be appreciated. My project can't really move forward until I get this working.
#include <SPI.h>
const int SSP = 10;
const int dataReady = 7;
const int resetPin = 3;
unsigned long advalue;
const int bytes = 3;
unsigned long inByte[bytes];
void setup()
{
pinMode(SSP, OUTPUT);
pinMode(dataReady,INPUT);
pinMode(resetPin,OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE1);
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);
Serial.begin(115200);
digitalWrite(resetPin,LOW);
digitalWrite(resetPin,HIGH);
digitalWrite(SSP,LOW);
SPI.transfer(0x27); // setup and calibration
SPI.transfer(0x4F);
SPI.transfer(0x37);
SPI.transfer(0xA0);
SPI.transfer(0x14);
SPI.transfer(0x20);
digitalWrite(SSP,HIGH);
delay(100);
}
void loop()
{
if(digitalRead(dataReady) == LOW)
{
advalue = readChannel();
Serial.println(advalue);
}
}
unsigned long readChannel()
{
memset(inByte,0,sizeof(inByte));
unsigned long result = 0;
digitalWrite(SSP,LOW);
for(int i = 0; i<bytes; i++)
{
inByte[i] = SPI.transfer(0x5C);
result += (inByte[i] << (((bytes - 1) * 8)-(8*i)));
Serial.print(inByte[i]); Serial.print(" ");
}
digitalWrite(SSP,HIGH);
return(result);
}