Trouble with two way SPI communication

Hi,

I’m having some trouble getting two Arduinos (Uno-Uno or Uno-Mega) to communicate over SPI.
I would like the Slave to to send one or more bytes to the Master. The bytes will eventually contain the status of sensors connected to the digital ports, possibly chaining a serie of arduinos. But for now I’m just content with sending a predefined byte back to the master.

I’ve set up two Arduino’s using the guide from Nick Gammon here. All 6 pins from the iscp port are connected one-on-one. The Master is powered of USB and connected to the serial.

Master code is this:

#include <SPI.h>

void setup (void) {
    Serial.begin(9600);
    Serial.println();

    digitalWrite(SS, HIGH);  // ensure SS stays high for now
    SPI.begin ();

}

void loop (void)
    {

    SPI.beginTransaction (SPISettings (30000, MSBFIRST, SPI_MODE0));
    digitalWrite(SS, LOW);
    
    Serial.println(SPI.transfer(0), BIN);
     
    digitalWrite(SS, HIGH);
    SPI.endTransaction();  

    delay(2000);  // 2 second delay
}

Slave code is this:

byte clr;

void setup (void) {

    pinMode(MISO, OUTPUT);      // have to send on master in, *slave out*
    SPCR |= _BV(SPE);           // turn on SPI in slave mode
    SPCR |= _BV(SPIE);          // turn on interrupts
    
    clr = SPDR;
    SPDR = 0xd0;
}


// SPI interrupt routine
ISR (SPI_STC_vect) {
    byte c = SPDR;
    SPDR = 0xd0;
}  

void loop (void) {
    
}

I’m expecting only lines of 11010000 bytes.
But I’m getting this:

0
0
0
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
0
0
0
11010000
11010000
11010000
11010000
0
0
0
0
1111111
11111111
11111111
11111111
11111111

Anyone knows what’s happening?

pvandegeer:
Hi,

I’m having some trouble getting two Arduinos (Uno-Uno or Uno-Mega) to communicate over SPI.
I would like the Slave to to send one or more bytes to the Master. The bytes will eventually contain the status of sensors connected to the digital ports, possibly chaining a serie of arduinos. But for now I’m just content with sending a predefined byte back to the master.

I’ve set up two Arduino’s using the guide from Nick Gammon here. All 6 pins from the iscp port are connected one-on-one. The Master is powered of USB and connected to the serial.

Master code is this:

#include <SPI.h>

void setup (void) {
    Serial.begin(9600);
    Serial.println();

digitalWrite(SS, HIGH);  // ensure SS stays high for now
    SPI.begin ();

}

void loop (void)
    {

SPI.beginTransaction (SPISettings (30000, MSBFIRST, SPI_MODE0));
    digitalWrite(SS, LOW);
   
    Serial.println(SPI.transfer(0), BIN);
   
    digitalWrite(SS, HIGH);
    SPI.endTransaction();

delay(2000);  // 2 second delay
}




Slave code is this:


byte clr;

void setup (void) {

pinMode(MISO, OUTPUT);      // have to send on master in, slave out
    SPCR |= _BV(SPE);          // turn on SPI in slave mode
    SPCR |= _BV(SPIE);          // turn on interrupts
   
    clr = SPDR;
    SPDR = 0xd0;
}

// SPI interrupt routine
ISR (SPI_STC_vect) {
    byte c = SPDR;
    SPDR = 0xd0;
}

void loop (void) {
   
}




I'm expecting only lines of 11010000 bytes.
But I'm getting this:


0
0
0
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
0
0
0
11010000
11010000
11010000
11010000
0
0
0
0
1111111
11111111
11111111
11111111
11111111




Anyone knows what's happening?

I would add a SPI.begin(); in the Slave’s setup, before you reconfigure it for ‘slave’ mode. I am unsure what the bit order, clock divisor are after a hardware reset. You are using the default SPI.begin() in the master, I recommend you use the same configs on the slave.

Chuck.

Nevermind... I'm a fool.

I connected all 6 iscp pins, thinking this covered all pins. But there's a reset pin there and not a SS pin.
Connected the SS pin as wel and everything is working beautifully. Even 3 arduino's in a row passing on their bytes in a chain.

Thanks for your reply anyway!

Pepijn