SPI slave repeats MOSI input on MISO

Hi,

I've set up my Uno as an SPI slave to another microcontroller (uC2). I am able to send a byte from the uC2 to the Uno, and forward it to the serial monitor.

I have set up an oscilloscope to monitor the slave select (yellow), MOSI (green), MISO (blue), and clock (red) and I discover that with each subsequent transmission, the data transmitted from uC2 to Uno on MOSI in the previous transmission is returned on MISO at the point of the new transmission.

I have not set the Arduino up to do this. Is this the default behaviour? Is there an explanation which would be obvious to someone more advanced than me?

My Arduino code is as follows:

// --------------------------------------------------------------------------
// Arduino SPI Slave
// --------------------------------------------------------------------------

#include <SPI.h>

char buff[8];
volatile boolean process_it = false;

// --------------------------------------------------------------------------
void setup(void)
{
  Serial.begin(115200);

  // Reset data I/O pins
  pinMode(MISO, OUTPUT);
  pinMode(MOSI, INPUT);

  // Set clock speed to 1MHz
  SPI.setClockDivider(SPI_CLOCK_DIV16);

  // Turn on in slave mode
  SPCR |= 1 << SPE;

  // Enable interrupt
  SPI.attachInterrupt();
}

// --------------------------------------------------------------------------
ISR (SPI_STC_vect)
{
  buff[0] = SPDR; // Get byte from SPI Data Reg
  process_it = true;
}

// --------------------------------------------------------------------------
void loop(void)
{
  static word my_tick = 0;
  
//  if (my_tick == 50000)
//  {
//    Serial.println("Tick");    
//    my_tick = 0;
//  }
//  else
//  {
//    my_tick++;
//  }
  
  if (true == process_it)
  {
    Serial.println((int) buff[0], HEX);
    process_it = false;
  }  
}

This sounds like normal behaviour. When you receive a byte over SPI, it goes into the SPDR (SPI Data Register). You then read the contents. However, the byte is still in the SPDR - it doesn't get zero'd.

When the Arduino as an SPI slave receives the next set of clock pulses, it clocks out the contents of SPDR - which just happen to be the byte previously received.

Thanks!

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