Problem interfacing ADS8341 SPI

Hello,

Since this is my first time working with SPI I have a few questions.

My problem is that I don’t know how to read and write data to the SPI device (in this case ADS841, 16-bit ADC). The code I have now is just setting up the SPI communication:

#include <SPI.h>

int spi_SS = 10;
int spi_MOSI = 11;
int spi_MISO = 12;
int spi_SCK = 13;

void setup() {
  SPI.begin();

  digitalWrite(spi_SS, HIGH);
}

void loop() {
  SPI.beginTransaction(SPISettings(2400000, MSBFIRST, SPI_MODE0));
  digitalWrite(spi_SS, LOW);

  //Other SPI code 

  digitalWrite(spi_SS, HIGH);
  SPI.endTransaction();
}

I got the frequency/MSB/Mode from the datasheet.

This is the timing diagram (The ADC can also be used with an internal clock mode or a 24-clock conversion rate, see datasheet):

So from what I understand, the DIN/MOSI has to send 8 bits of data on the rising edge. After that there is 1 clock cylce where no the ADC is processing the send data. Then (in the next 17 to 25 bits) on every clk cycle the DOUT is sending data that the MISO has to read. After that only the clock is cycling to the 32th bit and the protocol is done.

The only problem is that I don’t know what to do now after the SPI.beginTransaction(); in code.

If someone could explain how this process relates to code in arduino I would be happy.

Thanks.

EDIT:
After some more reading on the SPI.transfer(data); function, I see that you can send the data (1 byte) in the ‘data’ variable and then the function returns the received data. How does the function know when to stop reading the data / does it only read the 16 bits that I need from the ADC?

In your code you should insert a pinMode(spi_SS, OUTPUT) in the setup routine to ensure that the SPI hardware is in master mode.

After some more reading on the SPI.transfer(data); function, I see that you can send the data (1 byte) in the 'data' variable and then the function returns the received data. How does the function know when to stop reading the data / does it only read the 16 bits that I need from the ADC

Each call of SPI.transfer(); does transmit exactly one byte (in both directions). The method does not know when to stop, it does just send 8 clock cycles. In each clock cycle it sends one bit of the byte supplied as a parameter to the method and it reads one bit from the MISO pin. The resulting byte from that read is returned by the method. It doesn't care if the connected chip is providing data or not it just reads what the pin says. It's the problem of the connected chip to provide the data in a timely manner.

So if you want to read 16 bits of data from the ADC you have to call SPI.transfer() twice.