Hi All
Hope your well,
Im working on a project that requires Full Duplex communication over SPI, initially i wrote the code for half duplex I2C however the project became more complex the more boards i added and achieving two way communication between 6 boards. bus clashes became an issue.
I have run a test on Full Duplex transmitting and receiving a singe byte simultaneously which has been successful.
The goal is send a Struct packet of data containing 8 bit, 16 bit and 32bit values which i have been successful with using I2C protocol i.e. breaking down the 16bit and 32bit values to 8 bits and reassembling them.
See below the code i started with which sends a single byte Full Duplex.
Master:-
#include<SPI.h>
byte x = 10;
void setup (void){
Serial.begin(115200);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
digitalWrite(SS,HIGH);
}
void loop(void){
byte Mastersend,Mastereceive;
digitalWrite(SS, LOW);
Mastersend = x;
Mastereceive = SPI.transfer(Mastersend);
Serial.print(Mastersend);
Serial.print(" ");
Serial.println(Mastereceive);
digitalWrite(SS,HIGH);
delay(100);
}
Slave:-
#include<SPI.h>
volatile boolean received;
volatile byte Slavereceived,Slavesend;
byte x =20;
void setup(){
Serial.begin(115200);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect)
{
Slavereceived = SPDR;
received = true;
}
void loop(){
if(received){
Serial.print(Slavesend);
Serial.print(" ");
Serial.println(Slavereceived);
Slavesend=x;
SPDR = Slavesend; //Sends the x value to master via SPDR
delay(100);
}
}
Now he is the test code i have been playing with to try issue a Struct to the slave , its very important that i remain full duplex, i.e. sending and receiving data simultaneously between boards.
i have been trying to follow [Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino (Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino) without success.
Master Struct:-
#include<SPI.h>
byte data1;
int data2;
long data3;
typedef struct myStruct{ // Struct data to transmit
byte val_1;
int val_2;
long val_3;
};
myStruct DataOut;
void setup (void){
Serial.begin(115200);
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
digitalWrite(SS,HIGH);
}
void loop(void){
DataOut.val_1 = 42;
DataOut.val_2 = 32000;
DataOut.val_3 = 100000;
data1 = DataOut.val_1;
data2 = DataOut.val_2;
data3 = DataOut.val_3;
byte DataOut,Mastereceive;
digitalWrite(SS, LOW);
Mastereceive = SPI.transfer(DataOut);
Serial.print(data1);
Serial.print(" ");
Serial.print(data2);
Serial.print(" ");
Serial.print(data3);
Serial.print(" ");
Serial.println(Mastereceive);
digitalWrite(SS,HIGH);
delay(1000);
}
Slave Struct:-
#include<SPI.h>
volatile boolean received;
volatile byte Slavesend;
byte x =20;
byte val_1;
int val_2;
long val_3;
typedef struct myStruct{ // Struct data received from master
byte val_1;
int val_2;
long val_3;
};
volatile myStruct DataIn;
void setup(){
Serial.begin(115200);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect)
{
//Slavereceived = SPDR;
received = true;
}
void loop(){
if(received){
Serial.print((int) DataIn.val_1);
Serial.print(" ");
Serial.print(DataIn.val_2);
Serial.print(" ");
Serial.print(DataIn.val_3);
Serial.print(" ");
Serial.println(Slavesend);
Slavesend=x;
SPDR = Slavesend; //Sends the x value to master via SPDR
delay(1000);
}
}
Look forward to your responses.
Thanks