SPI

Hi All,

This bit of code is what I am doing to interface with a Analog Devices LTC1325. I need to send 6 Bytes of code to have it perform a function. My issue is that the 4, 5, and 6 bytes need to be stored in a variable so that I can extract the ADC reading and status of the chip. If I use ADCByte1 = SPI.trasnfer(0) it seems to send that byte over MOSI, at least that is what I see with my LA. I thought that command would simply place the byte in ADCByte1...

Any insight appreciated

if (LCDML.FUNC_setup())         // ****** SETUP *********
  {
    //LCD Print
    lcd.clear();
    lcd.setCursor(1, 0);
    lcd.print("Charging");
    lcd.setCursor(1, 1);
    lcd.print("Vbat=");
    lcd.setCursor(1, 2);
    lcd.print(LTC1325Status);
    
    //SPI for LTC1325
    pinMode(CS_Pin, OUTPUT); // set the slaveSelectPin as an output:
    pinMode(53, OUTPUT); //Insure Mega is Master
    digitalWrite(CS_Pin, HIGH); // Activate the CS line (CS is active Low)
    SPI.begin();
    SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE0));
    delay(100); //Let things stabalize

    //LTC1325 ADC and Status
    byte ADCByte1=0;
    byte ADCByte2=0;
    byte LTC1325Status=0;

    // remmove compiler warnings when the param variable is not used:
    LCDML_UNUSED(param);
    // setup
    // is called only if it is started

    // starts a trigger event for the loop function every 100 milliseconds
    LCDML.FUNC_setLoopInterval(5000);

    // uncomment this line when the menu should go back to the last called position
    // this could be a cursor position or the an active menu function
    // GBA means => go back advanced
    LCDML.FUNC_setGBA();

    //
  }

  if (LCDML.FUNC_loop())          // ****** LOOP *********
  {
    // Set CS Pin LOW
    digitalWrite(CS_Pin, LOW); // Activate the CS line (CS is active LOW)
    delay(1);

    //Send 6 Bytes to ltc1325
    SPI.transfer(0b00000010); // Send First byte
    SPI.transfer(0b00100100); // Send Second byte
    SPI.transfer(0b00000011); // Send Third byte
    SPI.transfer(0b11000000); // Send Forth byte
    ADCByte1 = SPI.transfer(0); // get RXbyte4 result and place in ADCByte1
    SPI.transfer(0b00000000); // Send Fifth byte
    ADCByte2 = SPI.transfer(0); // get RXbyte5 result and place in ADCByte2
    SPI.transfer(0b00000000); // Send Sixte byte
    LTC1325Status = SPI.transfer(0); // get RXbyte6 result and place in LTC1325Status
    delay(1);
    //Set CS_Pin High
    digitalWrite(CS_Pin, HIGH); // Enable internal pull-up

ADCByte1 = SPI.transfer(0) is basically a command to send 0 over the bus. In addition, the ADCByte1 variable will (should) now contain an answer from the device you are communicating with.

The reason nobody answered is probably because they didn't want to bother when you can find the answer here:

//Send 6 Bytes to ltc1325
    SPI.transfer(0b00000010); // Send First byte
    SPI.transfer(0b00100100); // Send Second byte
    SPI.transfer(0b00000011); // Send Third byte
    //SPI.transfer(0b11000000); // Send Forth byte
    ADCByte1 = SPI.transfer(0b11000000); // get RXbyte4 result and place in ADCByte1
    //SPI.transfer(0b00000000); // Send Fifth byte
    ADCByte2 = SPI.transfer(0b00000000); // get RXbyte5 result and place in ADCByte2
    //SPI.transfer(0b00000000); // Send Sixte byte
    LTC1325Status = SPI.transfer(0b00000000); // get RXbyte6 result and place in LTC1325Status
    //Set CS_Pin High
    digitalWrite(CS_Pin, HIGH); // Enable internal pull-up

That works...I didn't realize I could send and receive simultaneously with one command...duh
Thks for posting and setting me straight. I'm not a coder so sometimes it just gets lost...I did read that over many times

lucatz:
That works...I didn't realize I could send and receive simultaneously with one command...duh

Look at the following figure for the circulating type buffer/register structure of SPI Protocol.

spi328x.png

spi328x.png

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.