SPI send data from slave to master

Hi. I need to receive and integrate data from multi slave. So I use SPI to send the data from slave to master. And when I tried using one slave to send the data I was so confused because the slave doesn't send any data. Almost all the references I found use the master to send data to the slave.
Here is my code. Could any one help me? I will be very thankful.

//SLAVE-SENDER

#include <SPI.h>


void setup (void) {
  Serial.begin (9600);
  pinMode(MISO, OUTPUT);
  digitalWrite(MISO, HIGH);
  SPCR |= _BV(SPE); // turn on SPI in slave mode
  SPI.setClockDivider(SPI_CLOCK_DIV64);//divide the clock by 8
}

int l = 0;
void loop (void) {
  int hasil[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  char data[13][11];
  char c;
  int i;
  for (i = 0; i < 13; i++) {
    hasil[i] = l;
    l++;
    sprintf(data[i], "%d, ", hasil[i]);
  }

  for (i = 0; i < 13; i++) {
    int len = strlen(data[i]);
    data[i][len - 1] = '\r';
    data[i][len] = '\0';

    for (int j = 0; j < len; j++) {
      SPI.transfer(data[i][j]);
      Serial.print(data[i][j]);
      delayMicroseconds(10);
    }

  }
  Serial.println("");
}
#include <SPI.h>

char receivedData[200];
volatile byte indx;
volatile boolean process;

void setup() {
  pinMode(8, OUTPUT);
  Serial.begin(9600);
  digitalWrite(8, HIGH); // Disable Slave Select
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV64); // divide the clock by 8
  SPI.attachInterrupt();
  indx = 0;
  process = false;
}

ISR(SPI_STC_vect) // SPI interrupt routine
{
  byte d = SPDR;
  if (indx < sizeof(receivedData) - 1) {
    if (d != '\r') {
      receivedData[indx++] = d;
    } else {
      receivedData[indx] = '\0';
      process = true;
    }
  }
}

void loop() {
  digitalWrite(8, LOW);
  SPI.transfer(0); // Send dummy byte to initiate data transfer
  while (!process) {
    // Wait for data to be received
  }
  process = false;
  Serial.println(receivedData);
  indx = 0;
  digitalWrite(8, HIGH);
  delay(500);
}

master must select the device on the bus and then request the data.
workround is to wire an extra pin for signaling from slave to master

How to request data from the slave in master?

Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino

Regards

Please, try to understand the diagram of Fig-1 to figure out data exchange mechanism between two back-to-back connected SPDR Registers of SPI Ports.


Figure-1:

select the slave on the bus (by setting CS pin LOW for only that one slave). I guess it would be enough if the slave checks the CS pin, but usual is to send some message to which the slave replays

The clock is controlled by the master. There is no way for a slave to initiate a transfer via the SPI protocol.

I got an update, I can send data from slave to master using this code

// master

#include <SPI.h>

#define SS_1 7
#define SS_2 8
#define SS_3 6
//#define SS_4 5
//#define SS_5 4

void setup (void)
{
  Serial.begin (115200);
  Serial.println ("Starting");

  pinMode(SS_1, OUTPUT);
  pinMode(SS_2, OUTPUT);
//  pinMode(SS_3, OUTPUT);
//  pinMode(SS_4, OUTPUT);
//  pinMode(SS_5, OUTPUT);

  digitalWrite(SS_1, LOW);  // ensure SS stays high for now
  digitalWrite(SS_2, LOW);  // ensure SS stays high for now
 digitalWrite(SS_3, LOW);  // ensure SS stays high for now
//  digitalWrite(SS_4, LOW);  // ensure SS stays high for now
//  digitalWrite(SS_5, LOW);  // ensure SS stays high for now

  SPI.begin ();

  SPI.setClockDivider(SPI_CLOCK_DIV128);
}

void loop (void)
{
  char buf_1[54]; //54
  char buf_2[54]; //54
//  char buf_3[54]; //54
//  char buf_4[54]; //54
//  char buf_5[54]; //54

  digitalWrite(SS_1, HIGH );

  SPI.transfer (1);
  for (int pos = 0; pos < sizeof (buf_1) - 1; pos++)
  {
    delayMicroseconds (20);
    buf_1 [pos] = SPI.transfer (0);
    if (buf_1 [pos] == 0)
    {
      break;
    }
  }

  buf_1 [sizeof (buf_1) - 1] = 0;

  Serial.print("Slave 1:");
  Serial.print(buf_1);
  memset(buf_1, 0, sizeof(buf_1)); //clear array
  delay(10);
  digitalWrite(SS_1, LOW);
  delay(10);
  Serial.print("\t");
  //================SLAVE 2===================//
  digitalWrite(SS_2, HIGH);

  SPI.transfer (2);
  for (int pos = 0; pos < sizeof (buf_2) - 1; pos++)
  {
    delayMicroseconds (20);
    buf_2 [pos] = SPI.transfer (0);
    if (buf_2 [pos] == 0)
    {
      break;
    }
  }

  buf_2 [sizeof (buf_2) - 1] = 0;

  Serial.print("Slave 2:");
  Serial.print(buf_2);

  memset(buf_2, 0, sizeof(buf_2)); //clear array
  delay(10);

  digitalWrite(SS_2, LOW);
  Serial.print("\t");
  //================SLAVE 3===================//
//  digitalWrite(SS_3, HIGH);
//
//  SPI.transfer (3);
//  for (int pos = 0; pos < sizeof (buf_3) - 1; pos++)
//  {
//    delayMicroseconds (20);
//    buf_3 [pos] = SPI.transfer (0);
//    if (buf_3 [pos] == 0)
//    {
//      break;
//    }
//  }
//
//  buf_3 [sizeof (buf_3) - 1] = 0;
//
//  Serial.print("Slave 3:");
//  Serial.print(buf_3);
//
//  memset(buf_3, 0, sizeof(buf_3)); //clear array
//  delay(10);
//
//  digitalWrite(SS_3, LOW);
//  Serial.print("\t");
  //================SLAVE 4===================//
//  digitalWrite(SS_4, HIGH);
//
//  SPI.transfer (4);
//  for (int pos = 0; pos < sizeof (buf_4) - 1; pos++)
//  {
//    delayMicroseconds (20);
//    buf_4 [pos] = SPI.transfer (0);
//    if (buf_4 [pos] == 0)
//    {
//      break;
//    }
//  }
//
//  buf_4 [sizeof (buf_4) - 1] = 0;
//
//  Serial.print("Slave 4:");
//  Serial.print(buf_4);
//
//  memset(buf_4, 0, sizeof(buf_4)); //clear array
//  delay(10);
//
//  digitalWrite(SS_4, LOW);
//  Serial.print("\t");
//  //================SLAVE 5===================//
//  digitalWrite(SS_5, HIGH);
//
//  SPI.transfer (5);
//  for (int pos = 0; pos < sizeof (buf_5) - 1; pos++)
//  {
//    delayMicroseconds (20);
//    buf_5 [pos] = SPI.transfer (0);
//    if (buf_5 [pos] == 0)
//    {
//      break;
//    }
//  }
//
//  buf_5 [sizeof (buf_5) - 1] = 0;
//
//  Serial.print("Slave 5:");
//  Serial.print(buf_5);
//
//  memset(buf_5, 0, sizeof(buf_5)); //clear array
//  delay(10);
//
//  digitalWrite(SS_5, LOW);
  Serial.println("");
  delay(500);

}  // end of loop
// Slave

#include <Wire.h>
#include <VL53L1X.h>

//#define max_range 1000
//#define min_range 20
#define XSHUT_1 3

VL53L1X sensor1;

//uint8_t value1;

volatile char buf[54];
volatile int pos;
volatile bool active;

void setup (void)
{
  int x;
  int i;
  int j;
  int jaraktotal;

  unsigned long t;
  unsigned long tx;
  unsigned long ty;


  while (!Serial) {}
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  pinMode(MISO, OUTPUT);

  pinMode(XSHUT_1, OUTPUT);

  digitalWrite(XSHUT_1, LOW);

  SPCR |= bit(SPE);

  SPCR |= bit(SPIE);

  //================================================
  //Conf. Sensor1
  //================================================
  delay(50);
  digitalWrite(XSHUT_1, HIGH);
  delay(50);

  sensor1.setTimeout(500);
  if (!sensor1.init())
  {
    Serial.println("Failed to detect and initialize sensor1!");
  }
  else {
    sensor1.setAddress(24);
    sensor1.setDistanceMode(VL53L1X::Long);
    sensor1.setMeasurementTimingBudget(23500); //microsecond => 10 milisecond = 10ms
    sensor1.startContinuous(45);
    sensor1.setROISize(1, 8);

  }

}

ISR (SPI_STC_vect)
{
  byte c = SPDR;
  if (c == 3)    { //different value for every string
    active = true;
    pos = 0;
    SPDR = buf [pos++];

    return;
  }

  if (!active)
  {
    SPDR = 0;
    return;
  }

  SPDR = buf [pos];
  if (buf [pos] == 0 || ++pos >= sizeof(buf))
    active = false;

}

void loop (void)
{
  int x = 0;
//  int hitung = 0;
  int hasil[13] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  for (int i = 151; i <= 247;) {
    sensor1.setROICenter(i);
    sensor1.startContinuous(10);
    hasil[x] = sensor1.read();
    Serial.print(hasil[x]);
    Serial.print(",");
    x++;
    i += 8;

  }
  Serial.println("");
  sprintf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,", hasil[0],hasil[1],hasil[2],hasil[3],hasil[4],hasil[5],hasil[6],hasil[7],hasil[8],hasil[9],hasil[10],hasil[11],hasil[12]);
}

but the new problem is I can not read the data from the slave 3. I succeed to read data from slave 1 and 2, but when I activate slave 3 I got problem and the output is unwanted characters. I wonder what happened and whats wrong with my code?

Custom dashboards, smartphone remote control, data sharing between boards, remote uploads. Get them (for free) using your forum account!

SPI send data from slave to master

SPI Multi Slaves sends to a Master

Why did you double post? I do not mind answering a post but not the same one with different titles.

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