Ok. I apologize for this.
//Master sending data
#include <SPI.h>// include the SPI library:
#define SS_PIN 10
int command_array[] = {0x04, 0x06, 0x08, 0x0A};
int returned_data[4];
void setup() {
pinMode (SS_PIN, OUTPUT);// set the spi_data_pin as an output:
Serial.begin(115200);
SPI.begin();
SPI.setDataMode(SPI_MODE2);
SPI.setClockDivider(SPI_CLOCK_DIV64) ;
SPI.setBitOrder(LSBFIRST);
}
void loop() {
digitalWrite(SS_PIN,LOW);
SPI.transfer(command_array[0]); // send command
//delay(1); // give the slave some time
returned_data[0] = SPI.transfer(0); // get response
digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW);
//delay(1);
SPI.transfer(command_array[1]);
returned_data[1] = SPI.transfer(0);
digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW);
//delay(1);
SPI.transfer(command_array[2]);
returned_data[2] = SPI.transfer(0);
digitalWrite(SS_PIN,HIGH);
digitalWrite(SS_PIN,LOW);
//delay(1);
SPI.transfer(command_array[3]);
returned_data[3] = SPI.transfer(0);
digitalWrite(SS_PIN,HIGH);
Serial.print (returned_data[0]);
Serial.print (',');
Serial.print (returned_data[1]);
Serial.print (',');
Serial.print (returned_data[2]);
Serial.print (',');
Serial.println (returned_data[3]);
}
Slave code for sending back data:
//Slave Code
#include "pins_arduino.h"
#include <SPI.h>
#define SS 10
byte c;
int m, n ;
void setup (void)
{
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode (SPI_MODE2);
SPI.setClockDivider(SPI_CLOCK_DIV64) ;
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// turn on interrupts
SPCR |= _BV(SPIE);
// disable timer interrupts
TIMSK0 = 0;
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
c = SPDR;
if(c == 0x04)
{
SPDR = 0x01;
}
else if(c == 0x06)
{
SPDR = 0x02;
}
else if(c == 0x08)
{
SPDR = 0x03;
}
else if(c == 0x0A)
{
SPDR = 0x07;
}
else
SPDR = 0; // what to return to the master
} // end of interrupt service routine (ISR) SPI_STC_vect
void loop (void)
{
} // end of loop
This combination of Master and Slave code I have tried but Slave is not able to send back data to Master, I have observed in Oscilloscope MISO is not having data. What could be the reason.