Hello. I am trying to implement a slave/master relationship (Arduino Uno as master and Arduino Pro Mini as slave) where the master sends a mode command to the slave and receives data in return once the mode command is accepted by the slave. My problem is that the serial monitor of the master only prints the number 255, no matter what data it is fed by the slave. Any help would be much appreciated. Master and Slave code attached below.
// SPI Example of Sending a Byte or 2 Bytes from an Arduino Uno Board to a Pro Micro Arduino Board using the SPI interface (Slave)
//
// 10th December 2021
//
#include <SPI.h>
// GPIO pin that determines the state of the RS458 defining it as either High (transmission state) or Low (receiver state)
// setting up of the data pins for transmission and receiving of data from the master on Pro Micro Arduino Board
int dataEnablePin = 8;
int dataReceiverPin = 12; // MOSI (Master Out Slave In for SPI interface)
int dataTransmitterPin = 11; // MISO (Master In Slave Out for SPI interfacte)
int ssPin = 10;
// setting up of the clock pins for only the receiving of the clock from the master
int clockEnablePin = 9;
int clockReceiverPin = 13; // The SCLK of the SPI interface on the Pro Micro
volatile byte command = 0;
void setup (void)
{
// defining the states of the data pins
pinMode(dataEnablePin, OUTPUT);
pinMode(dataReceiverPin, INPUT);
pinMode(dataTransmitterPin, OUTPUT);
pinMode(ssPin, INPUT);
// defining the states of the clock pins
pinMode(clockEnablePin, OUTPUT);
pinMode(clockReceiverPin, INPUT);
digitalWrite(dataEnablePin, LOW); // pin 8 always LOW to receive value from Master
digitalWrite(clockEnablePin, LOW); // pin 9 is the Clock Line and it will always be LOW to receive
SPI.begin();
// turn on SPI in slave mode
SPCR |= _BV(SPE);
SPCR |= !(_BV(MSTR)); //Arduino is Slave
// turn on interrupts
SPCR |= _BV(SPIE);
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte slaveReceived = SPDR;
// byte slaveSend = 00010101; // decimal 15
int slaveSend = 15;
switch (command)
{
// no command? then this is the command
case 0:
command = slaveReceived;
SPDR = 0;
break;
// add to incoming byte, return result
case 'a':
digitalWrite(dataEnablePin, HIGH);
SPDR = slaveSend;
digitalWrite(dataEnablePin, LOW);
break;
} // end of switch
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
{
// if SPI not active, clear current command
if (digitalRead(ssPin) == HIGH)
command = 0;
} // end of loop
// SPI Example of Sending a Byte or 2 Bytes from an Arduino Uno Board to a Pro Micro Arduino Board using the SPI interface (Master)
//
// December 2021
#include <SPI.h>
int dataOut = 11; // MOSI
int dataIn = 12; // MISO
int dataEnablePin = 8;
int clockPin = 13; // SCLK
int clockEnablePin = 9;
void setup (void)
{
Serial.begin(9600);
pinMode(dataOut, OUTPUT);
pinMode(dataEnablePin, OUTPUT);
pinMode(dataIn, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
digitalWrite(dataOut, HIGH);
digitalWrite(dataEnablePin, HIGH); // for transmission using the RS485
digitalWrite(clockEnablePin, HIGH); // for the transmission using the RS485
SPI.beginTransaction(SPISettings(2000000, MSBFIRST, SPI_MODE2)); // Slow down the master a bit
SPI.begin();
Serial.begin (115200);
Serial.println ();
digitalWrite(SS, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
} // end of setup
byte transferAndWait (const byte trans)
{
byte a = SPI.transfer (trans);
digitalWrite(dataEnablePin, LOW);
delayMicroseconds (20);
return a;
} // end of transferAndWait
void loop (void)
{
byte a, b, c, d;
// enable Slave Select
digitalWrite(SS, LOW);
a = transferAndWait ('a'); // add command
digitalWrite(dataEnablePin, HIGH);
// transferAndWait (10);
// a = SPDR;
b = transferAndWait (0);
//digitalWrite(dataEnablePin, HIGH);
// b = transferAndWait (33);
// c = transferAndWait (42);
// d = transferAndWait (0);
// disable Slave Select
digitalWrite(SS, HIGH);
Serial.println ("Adding results:");
Serial.println (a, DEC);
Serial.println (b, DEC);
// Serial.println (c, DEC);
// Serial.println (d, DEC);
delay (1000); // 1 second delay
} // end of loop

