Problem with Tx and Rx 100 bits in binary from master to slave using SPI

Hi!

I'm using two arduino uno boards for SPI communication, one acting as a master and other to be a slave. My aim is to transmit (via MOSI line) 100 bits in binary format from master device to slave device, and receive back the same data at the master side (via MISO line), but unable to perform the same. I'm a beginner to arduino programming, can anyone help me out with the code? Here is a copy attached below.

Code for Master

#include <SPI.h>

void setup(void)
{
  
  Serial.begin(9600); 
  
  digitalWrite(SS, HIGH);
  
  SPI.begin();
    
}

void loop()
{

  SPI.beginTransaction(SPISettings(7000000, MSBFIRST, SPI_MODE0)); 
    
  byte mastersend[13] = {B11010011,B10110010,B10011101,B10101101,B10111001,B10110101,B10101101,B10110010,B10110010,B10110010,B10011101,B10011101,B10011101};
  byte masterrecev;
  unsigned int i;

  digitalWrite(SS, LOW);
  
  for(i = 0; i <= 13; i++)
  {
    masterrecev = SPI.transfer(mastersend[i]);
  }
  
  Serial.println(mastersend[i], BIN);
  delay(2000);
   
  Serial.println(masterrecev, BIN);
  delay(2000);
    
  digitalWrite(SS, HIGH);
  delay(2000);
  
}

Code for Slave

#include <SPI.h>

volatile boolean datarecev;
volatile int slaverecev;
volatile int slavesend;

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

  SPI.beginTransaction(SPISettings(7000000, MSBFIRST, SPI_MODE0));
  
  pinMode (MISO, OUTPUT);
  SPCR |= _BV(SPE);
  datarecev = false;
  
  SPI.attachInterrupt();

}

ISR (SPI_STC_vect)
{

  slaverecev = SPDR;
  datarecev = true;

}  

void loop (void)
{
  
  if (datarecev)
    {
       
    Serial.println(slaverecev, BIN);
    
    slavesend = slaverecev;
    SPDR = slavesend;

    delay(2000);
    
    }

}

Thanks in advance!

Hi,
Welcome to the forum.
Thanks for using code tags, added a Karma.
Have you got a basic SPI system working before trying to expand to what you want?

This may help.

Tom... :slight_smile:

BINDU_S_M:
I'm using two arduino uno boards for SPI communication, one acting as a master and other to be a slave. My aim is to transmit (via MOSI line) 100 bits in binary format from master device to slave device, and receive back the same data at the master side (via MISO line), but unable to perform the same.

Try these sketches and then optimize the Master codes using for() loop.

SPI-Master Codes: UNO

#include<SPI.h> //header file that contains the meanings of SS, MOSI, MISO

byte mastersend[13] =
{
  B11010011, B10110010, B10011101, B10101101,
  B10111001, B10110101, B10101101, B10110010,
  B10110010, B10110010, B10011101, B10011101,
  B10011101
};

byte receiveBytes[14] = {0};
void setup()
{
  Serial.begin(9600);
  SPI.begin(); //SPI Port is created with with default settings ;SS = LH
  digitalWrite(SS, LOW);   //slave is selected
}

void loop()
{
  SPI.transfer(mastersend[0]);
  //-------------------------------------
  byte x0 =  SPI.transfer(mastersend[1]);
  Serial.println(x0, BIN);
  //------------------------
  byte x1 = SPI.transfer(mastersend[2]);
  Serial.println(x1, BIN);
  //-----------------------------
  byte x2 = SPI.transfer(mastersend[3]);
  Serial.println(x2, BIN);
  //------------------------
  byte x3 = SPI.transfer(mastersend[4]);
  Serial.println(x3, BIN);
  //-----------------------------
  byte x4 = SPI.transfer(mastersend[5]);
  Serial.println(x4, BIN);
  //------------------------
  byte x5 = SPI.transfer(mastersend[6]);
  Serial.println(x5, BIN);
  //-----------------------------
  byte x6 = SPI.transfer(mastersend[7]);
  Serial.println(x6, BIN);
  //------------------------
  byte x7 =  SPI.transfer(mastersend[8]);
  Serial.println(x0, BIN);
  //------------------------
  byte x8 = SPI.transfer(mastersend[9]);
  Serial.println(x1, BIN);
  //-----------------------------
  byte x9 = SPI.transfer(mastersend[10]);
  Serial.println(x2, BIN);
  //------------------------
  byte x10 = SPI.transfer(mastersend[11]);
  Serial.println(x3, BIN);
  //-----------------------------
  byte x11 = SPI.transfer(mastersend[12]);
  Serial.println(x4, BIN);
  //------------------------
  byte x12 = SPI.transfer(0x00);
  Serial.println(x12, BIN);
  //-----------------------------
  
  while (1);
}

SPI-Slave Codes: NANO

#include<SPI.h> //header file that contains the meanings of SS, MOSI, MISO
volatile bool flag1 = false;
byte x;

void setup()
{
  Serial.begin(9600);
  SPCR |= _BV(SPE);   //SPI Port is enabled
  SPCR |= !(_BV(MSTR)); //NANO is slave
 // SPCR |= _BV(SPIE);     //interrupt is enabled
  pinMode(SS, INPUT_PULLUP);
  pinMode(MISO, OUTPUT);
  SPDR = 0x67;
}

void loop()
{
  
}

Screen shot: (Master)
smRe.png

smRe.png

Somebody needs to step AWAY from the "report to moderator" button.
Not the best way to answer a post LOL

Bob.

Yes, basic SPI for a single byte transfer is working fine. @Tom.
I apologize for the late response.

Thanks for the guidance. @ GolamMostafa

The codes worked well!
But, what if my slave is an IC with 100 bit shift register and i will have to load 100 binary bits to the slave shift register at the same instant over SPI.