SPI with Arduino

Hi All,

I have a CERES PCB. And i need to communicate with the PCB using SPI bus. The problem is I very low on knowledge regarding any types of communication. Below is the command list i need to do. I have practice the SPI code with only 1 data transfered to Slave. But when i try to do on my actual project, I'm become confused with the Address, MOSI cmd+add. I dont know which data need to be send and how.

I tried to play around but i keep getting rubbish data

My code:

#include <SPI.h>

const int chipSelectPin = 10;

void setup() {
  Serial.begin(9600);
  pinMode(chipSelectPin, OUTPUT);
  SPI.begin();
  
  SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
  
  byte commandSelAdc2Vdd[] = {0x40, 0x84, 0x00, 0x00, 0x02, 0x00};
  byte commandEnAcd2[] = {0x40, 0x24, 0x00, 0x20, 0x00, 0x00};
  byte commandReadAcd2[] = {0x00, 0x14, 0x00, 0x00, 0x00, 0x00};

  byte* commands[] = {commandSelAdc2Vdd, commandEnAcd2, commandReadAcd2};
  int numCommands = sizeof(commands) / sizeof(commands[0]);

  for (int i = 0; i < numCommands; i++) {

    digitalWrite(chipSelectPin, LOW);

    SPI.transfer(commands[i], sizeof(commandSelAdc2Vdd)); 

    digitalWrite(chipSelectPin, HIGH);
    delay(100);
    digitalWrite(chipSelectPin, LOW); 

    byte response[4]; 
    SPI.transfer(response, sizeof(response)); 
    digitalWrite(chipSelectPin, HIGH); 

    Serial.print("Response for Command ");
    if (i == 0) 
    {
      Serial.print("commandSelAdc2Vdd: ");
    } else if (i == 1) 
    {
      Serial.print("commandEnAcd2: ");
    }else{
      Serial.print ("commandReadAcd2: ");
    }
    for (int j = 0; j < 4; j++) {
      Serial.print(response[j],HEX);
      Serial.print(" ");
    }
    Serial.println();
    delay(500);
  }
}

void loop() {
  
}

Look at the SPI example that comes with the IDE [File->examples->SPI->BarometricPressureSensor]

It shows you how to read/write. Bascially, you have to send your cmd+addr (0x4084) and then you have to transfer 4 more bytes while reading back the results of the transfer as your data. The examples has more details.

Hi blh64,

Those cmd+Add command need to be send byte by byte or can be send altogether? Can u show an example of it?

@hisyam94 its hard to tell from the one piece of the manual that you show but it looks like it may be you have to send the data like this

transfer --> address + MOSI cmd + MOSI data

which for SelAdc2Vdd might be

receivedVal = SPI.transfer(0x21);
receivedVal = SPI.transfer16(0x4084);
receivedVal = SPI.transfer(0x00);
receivedVal = SPI.transfer(0x00);
receivedVal = SPI.transfer(0x02);
receivedVal = SPI.transfer(0x00);

I'm not saying that is how it should be, it just looks to me like the address should be part of the transfer and the command can be sent as a 16 bit value.

Here is a link to the Arduino reference it might help you get a little further

https://docs.arduino.cc/language-reference/en/functions/communication/SPI/

Look at the example I pointed out in reply #1.