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 100 bits (13 bytes, at same time instant) in binary format from master device to slave device, and receive back the same data at the master side, but unable to perform the same. I'm a beginner to arduino programming. The copy of the code tried is attached below.
Master code
#include<SPI.h>
byte latch = 9;
byte mastersend[13] = {B11010011, B10110010, B10011101, B10101101, B10111001, B10110101, B10101101, B10110010, B10110010, B10110010, B10011101, B10011101, B10011101};
byte masterrecv[13];
void setup()
{
Serial.begin(9600);
pinMode(latch, OUTPUT);
digitalWrite(latch, HIGH);
digitalWrite(SS, HIGH);
SPI.begin();
}
void loop()
{
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
digitalWrite(SS, LOW);
masterrecv = SPI.transfer(mastersend,13);
Serial.println(mastersend, BIN);
Serial.println(mastersend, BIN);
SPI.endTransaction();
}
Here is the slave code which I tried for Tx and Rx 8 bits (1 byte). Kindly help me on writing 13 bytes from master to slave device and get back the same at master device.
Slave Code
#include <SPI.h>
volatile boolean datarecev;
volatile byte slaverecev;
volatile byte slavesend;
void setup (void)
{
Serial.begin (9600);
pinMode (MISO, OUTPUT);
SPCR |= _BV(SPE);
datarecev = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect)
{
slaverecev = SPDR;
datarecev = true;
}
void loop (void)
{
SPI.beginTransaction(SPISettings(8000000, MSBFIRST, SPI_MODE0));
if (datarecev)
{
Serial.println(slaverecev, BIN);
slavesend = slaverecev;
SPDR = slavesend;
}
}
Also, how do I print my data at both the Master and Slave side, in order to verify the data sent is received without any error.
Thanks.