SPI interface with other board_AVR_ESK1100 or among Arduino itself

ranjeetray:

Hi...!!!

Can anybody share the working code on SPI.
[/quote]

Any Luck....!!!

I think I'll close this thread if you don't answer my question. It's no use ignoring the questions and keep posting about wanting people to share code.

Hi..!!!

Yes.

I have read this again and again, but still problem is there.
From master I am sending following data (int array[8] = {0x04, 0x00, 0x06, 0x00, 0x08, 0x00, 0x0A, 0x00}:wink: and on slave side I am receiving the same data. But same data are available on both MOSI and MISO.
I can see on oscilloscope that even MISO line has the same data which are ( int array[8] = {0x04, 0x00, 0x06, 0x00, 0x08, 0x00, 0x0A, 0x00}:wink: on MOSI also.
But from slave side I am not sending any data then why data are available on line(MISO), is there any way to clear buffer or SPDR.

//Master sending data

//Master sending data
#include <SPI.h>// include the SPI library:
#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int array[8] = {0x04, 0x00, 0x06, 0x00, 0x08, 0x00, 0x0A, 0x00};
int chr;

void setup() {
  
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
  
  SPI.begin();// initialize SPI:
  Serial.begin(115200);
  SPI.setDataMode(SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;
  SPI.setBitOrder(LSBFIRST);
 
}

void loop() {
  int data[10];
  char ch;
 

    
   digitalWrite(spidata,LOW);
   SPI.transfer(array[0]); 
   SPI.transfer(array[1]);
   digitalWrite(spidata,HIGH);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[2]); 
   SPI.transfer(array[3]);
   digitalWrite(spidata,HIGH);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[4]); 
   SPI.transfer(array[5]);
   digitalWrite(spidata,HIGH);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[6]); 
   SPI.transfer(array[7]);
   digitalWrite(spidata,HIGH);
   

    }

//Slave receiving data

//Slave receiving data
#include "pins_arduino.h"
#include <SPI.h>

// what to do with incoming data
byte command = 0;

void setup (void)
{
  Serial.begin(115200);
  // 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);

}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;
  Serial.println(c, HEX);
}


void loop (void)
{
  
  // if SPI not active, clear current command
  if (digitalRead (SS) == HIGH)
    command = 0;
}  // end of loop

If you aren't sending any data back, what do you expect? It will happen to return what is in SPDR at the time. You have to change it in the ISR. Also you can't do Serial.print in an ISR, it takes too long.

Modified sketch, returns 0x42:

//Slave receiving data
#include <SPI.h>

void setup (void)
{
  Serial.begin(115200);
  // 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);

}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // what we received from the master
  SPDR = 0x42;    // what to return to the master
}  // end of ISR SPI_STC_vect


void loop (void)
{
 
}  // end of loop

Logic analyzer confirms we got the 0x42 returned each time:

Thanks a lot Sir.

Thank you Sir.

Can we do like this, after receiving data can we check(if condition) data and then send back data to master like this.

#include <SPI.h>

void setup (void)
{
  Serial.begin(115200);
  // 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);

}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // what we received from the master
  //delay(15);
 if(c == 0x04)
 {
   digitalWrite(SS, LOW);
    SPDR = 0x45;
  SPI.transfer(0xFF);
digitalWrite(SS, HIGH);
 }
  else
 SPDR = 0x00;    // what to return to the master
}  // end of ISR SPI_STC_vect


void loop (void)
{
 
}  // end of loop

It is not working, I tried.

Hi...!!

This code works fine with making SPDR = 0x00; at ISR but I have observed on Oscilloscope that sometime few data goes back to MISO, Slave is not able to clear SPDR perfectly, what could be the reason.

//Slave receiving data
#include <SPI.h>

void setup (void)
{
  Serial.begin(115200);
  // 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);

}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // what we received from the master
  SPDR = 0x00;    // what to return to the master
}  // end of ISR SPI_STC_vect


void loop (void)
{
 
}  // end of loop

Just keeping bandwidth of site in mind I am attaching one oscilloscope photo for your reference.

TEK0000.BMP (76.1 KB)

ranjeetray:
Can we do like this, after receiving data can we check(if condition) data and then send back data to master like this.

I really don't know what you are trying to do. You are doing an SPI.transfer in the middle of the ISR?

You send back data to the master by assigning to SPDR in the interrupt service routine. No other way. What are you thinking?

ranjeetray:
This code works fine with making SPDR = 0x00; at ISR but I have observed on Oscilloscope that sometime few data goes back to MISO, Slave is not able to clear SPDR perfectly, what could be the reason.
...
Just keeping bandwidth of site in mind I am attaching one oscilloscope photo for your reference.

I can reproduce that. I am guessing that the Timer interrupt (used by millis() and delay() ) is slowing it down enough that it cannot respond quickly (this would happen every few milliseconds, because if the timer interrupt happens to fire when the SPI interrupt needs to fire, the SPI interrupt won't change SPDR fast enough).

By adding this line to setup the problem goes away:

  // disable timer interrupts
  TIMSK0 = 0;

Modified sketch:

//Slave receiving data
#include <SPI.h>

void setup (void)
{
  Serial.begin(115200);
  // 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)
{
  byte c = SPDR;  // what we received from the master
  SPDR = 0;    // what to return to the master
}  // end of ISR SPI_STC_vect


void loop (void)
{
 
}  // end of loop

This is why you cannot use delay inside the ISR (as you had, commented out). In any case, delay() doesn't work inside an ISR.

The ISR has to be fast, because if you don't change SPDR fast enough, the incorrect data will be sent on the next transfer.

Of course, if you turn off timer interrupts you won't know how much time has elapsed.

What you could do is have an interrupt on the SS (slave select) line. When that goes low, turn off timer interrupts, wait for the incoming data, and when SS goes high again, turn timer interrupts back on.

ranjeetray:
This code works fine with making SPDR = 0x00; at ISR but I have observed on Oscilloscope that sometime few data goes back to MISO, Slave is not able to clear SPDR perfectly, what could be the reason.

I'm not convinced you read and understood my page here:

In that I mention this exact problem. To get a response you need to allow time for the slave to do whatever it needs to do, and set up a response (ie. set SPDR). See this diagram from that page:

In the code on that page I built in a delay on the master end, to slow down communications long enough to give the slave time to respond. It's all on the page. Please read it again, and again.

... what could be the reason.

You need to read my page and understand it.

Is channel 1 on the scope trace really set to 50mV/div? If so you are in deep trouble.

At the other extreme the top trace seems to pulse well over 7v. Is that right?


Rob

He's probably got the 10x switch flicked on the probe.

Yes , It is 10X probe of Tektronix TDS2024C(200MHz/100MHz, 10MOhm/<12pF 300V CAT II).

Hi...!!

I am reading http://www.gammon.com.au/spi again and again and trying to grasp SPI, but not getting expected results.

Can we write the codes like this, are these correct codes for mater and slave

//Master sending data
#include <SPI.h>// include the SPI library:
#define SCK_PIN   13
#define MISO_PIN  12
#define MOSI_PIN  11

const int spidata = 10;//Pin 11 is data(MOSI) and pin 13 SCK ,set pin 10(SS) as the slave select for the digital pot:
int array[30] = {0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00,0x00, 0x00, 0x00, 0x00, 0x08,0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,0x00};
int chr;

void setup() {
 
  pinMode (spidata, OUTPUT);// set the spi_data_pin as an output:
 
  SPI.begin();// initialize SPI:
  Serial.begin(115200);
  SPI.setDataMode(SPI_MODE2);
  SPI.setClockDivider(SPI_CLOCK_DIV64) ;
  SPI.setBitOrder(LSBFIRST);
  digitalWrite(spidata,HIGH);
 
}

void loop() {
  int data[10];
  char ch;
 

    //delay(14);
   digitalWrite(spidata,LOW);
   SPI.transfer(array[0]);
   SPI.transfer(array[1]);
   digitalWrite(spidata,HIGH);
    //delay(14);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[2]);
   SPI.transfer(array[3]);
   digitalWrite(spidata,HIGH);
   // delay(14);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[4]);
   SPI.transfer(array[5]);
   digitalWrite(spidata,HIGH);
    delay(1);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[6]);
   SPI.transfer(array[7]);
   digitalWrite(spidata,HIGH);
   
     digitalWrite(spidata,LOW);
   SPI.transfer(array[8]);
   SPI.transfer(array[9]);
   digitalWrite(spidata,HIGH);
   
      digitalWrite(spidata,LOW);
   SPI.transfer(array[10]);
   SPI.transfer(array[11]);
   digitalWrite(spidata,HIGH);
  delay(1);
   
     digitalWrite(spidata,LOW);
   SPI.transfer(array[12]);
   SPI.transfer(array[13]);
   digitalWrite(spidata,HIGH);
   // delay(14);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[14]);
   SPI.transfer(array[15]);
   digitalWrite(spidata,HIGH);
   // delay(14);
   
   digitalWrite(spidata,LOW);
   SPI.transfer(array[16]);
   SPI.transfer(array[17]);
   digitalWrite(spidata,HIGH);
   delay(1);
   
     digitalWrite(spidata,LOW);
   SPI.transfer(array[18]);
   SPI.transfer(array[19]);
   digitalWrite(spidata,HIGH);
   
      digitalWrite(spidata,LOW);
   SPI.transfer(array[20]);
   SPI.transfer(array[21]);
   digitalWrite(spidata,HIGH);
    digitalWrite(spidata,LOW);
   SPI.transfer(array[22]);
   SPI.transfer(array[23]);
   digitalWrite(spidata,HIGH);
  //  delay(14);
   

    }

Slave receiving data from master then sending the data back to master, is it right.

//Slave receiving data from master and sending back data
#include "pins_arduino.h"
#include <SPI.h>
#define SS 10
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
byte c;
void ss_falling()
{
  c = 0;
}

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;
  // interrupt for SS falling edge
  attachInterrupt (0, ss_falling, FALLING);
   // disable timer interrupts
  TIMSK0 = 0;
  
}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  c = SPDR;
  
 if(c == 0x04)
  {
    digitalWrite(SS, LOW);
  SPI.transfer(dat[0]);
  SPI.transfer(dat[1]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[2]);
  SPI.transfer(dat[3]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[4]);
  SPI.transfer(dat[5]);
   digitalWrite(SS, HIGH);
 }
 
  else if(c == 0x06)
  {
   digitalWrite(SS, LOW);
  SPI.transfer(dat[6]);
  SPI.transfer(dat[7]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[8]);
  SPI.transfer(dat[9]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[10]);
  SPI.transfer(dat[11]);
   digitalWrite(SS, HIGH);
 }
  else
 SPDR = 0x00;    // what to return to the master
 


}  // end of interrupt service routine (ISR) SPI_STC_vect

void loop (void)
{
 
}  // end of loop

TEK0000.BMP (76.1 KB)

Nick had this

// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;  // what we received from the master
  SPDR = 0;    // what to return to the master
}  // end of ISR SPI_STC_vect

You have this

ISR (SPI_STC_vect)
{
  c = SPDR;
  
 if(c == 0x04)
  {
    digitalWrite(SS, LOW);
  SPI.transfer(dat[0]);
  SPI.transfer(dat[1]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[2]);
  SPI.transfer(dat[3]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[4]);
  SPI.transfer(dat[5]);
   digitalWrite(SS, HIGH);
 }
 
  else if(c == 0x06)
  {
   digitalWrite(SS, LOW);
  SPI.transfer(dat[6]);
  SPI.transfer(dat[7]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[8]);
  SPI.transfer(dat[9]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[10]);
  SPI.transfer(dat[11]);
   digitalWrite(SS, HIGH);
 }
  else
 SPDR = 0x00;    // what to return to the master
 


}  // end of interrupt service routine (ISR) SPI_STC_vect

Do you see the difference?

Nick said this

The ISR has to be fast, because if you don't change SPDR fast enough, the incorrect data will be sent on the next transfer.

You have an ISR as long as your arm that will take about three weeks to execute.

Nick said this

you need to allow time for the slave to do whatever it needs to do, and set up a response (ie. set SPDR).

Your code has the odd delay but not after every transfer.

On the master you are toggling the for every two bytes, why?

On the slave you do similar on the SS pin. You DON'T touch the SS pin on the slave. You DON'T call SPI.transfer() on the slave(). You DON'T do nothing on the slave except put data in the SPDR register. If you need to parse the first byte as a command you'd better do it real quick and use the results to point to a different array of bytes to return to the master.


Rob

I know I'm going to regret this but here's my quick version of one way to do the slave

#define MAX_BYTES 4  // is there 4 bytes? if not change this and the arrays.

byte cmd_04_data[MAX_BYTES] = {1,2,3,4}; // you fill in the numbers 
byte cmd_06_data[MAX_BYTES] = {5,6,7,8};
byte * data_ptr;
int byte_count = 0;

ISR (SPI_STC_vect) {
  byte c = SPDR;
  if (byte_count == 0) {
      // set a pointer to one or other array based on the byte just received
      data_ptr = (c == 0x04) ? cmd_04_data : cmd_06_data; 
  }
  SPDR = *(data_ptr + byte_count);
  byte_count++;   
  if (byte_count = MAX_BYTES) byte_count = 0;
}

It compiles but I can't test it.


Rob

Graynomad:
Nick had this

// SPI interrupt routine

ISR (SPI_STC_vect)
{
  byte c = SPDR;  // what we received from the master
  SPDR = 0;    // what to return to the master
}  // end of ISR SPI_STC_vect




You have this



ISR (SPI_STC_vect)
{
  c = SPDR;
 
if(c == 0x04)
  {
    digitalWrite(SS, LOW);
  SPI.transfer(dat[0]);
  SPI.transfer(dat[1]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[2]);
  SPI.transfer(dat[3]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[4]);
  SPI.transfer(dat[5]);
   digitalWrite(SS, HIGH);
}

else if(c == 0x06)
  {
   digitalWrite(SS, LOW);
  SPI.transfer(dat[6]);
  SPI.transfer(dat[7]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[8]);
  SPI.transfer(dat[9]);
   digitalWrite(SS, HIGH);
   
   digitalWrite(SS, LOW);
  SPI.transfer(dat[10]);
  SPI.transfer(dat[11]);
   digitalWrite(SS, HIGH);
}
  else
SPDR = 0x00;    // what to return to the master

}  // end of interrupt service routine (ISR) SPI_STC_vect




Do you see the difference?

Nick said this



> The ISR has to be fast, because if you don't change SPDR fast enough, the incorrect data will be sent on the next transfer.



You have an ISR as long as your arm that will take about three weeks to execute.

Nick said this



> you need to allow time for the slave to do whatever it needs to do, and set up a response (ie. set SPDR).



Your code has the odd delay but not after every transfer.

On the master you are toggling the for every two bytes, why?

On the slave you do similar on the SS pin. You DON'T touch the SS pin on the slave. You DON'T call SPI.transfer() on the slave(). You DON'T do nothing on the slave except put data in the SPDR register. If you need to parse the first byte as a command you'd better do it real quick and use the results to point to a different array of bytes to return to the master.


______
Rob

Hi...!!!

Thanks. More than two SPI.transfer(); does not work fine and 2 bytes of data transfer gives continuous 16clock pulse.
Following code does not work fine with SPDR = data; and I want to send back more than 10 data. And I am not able to give proper delay.
Can you please tell me how much delay I should give at Master side or Slave side.

#include "pins_arduino.h"
#include <SPI.h>
#define SS 10
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
byte c;
void ss_falling()
{
  c = 0;
}

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;
  // interrupt for SS falling edge
  attachInterrupt (0, ss_falling, FALLING);
   // disable timer interrupts
  TIMSK0 = 0;
  
}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  c = SPDR;
  
  if(c == 0x04)
  {
    
    SPDR = dat[0];
   
 }
 
   else if(c == 0x06)
  {
    SPDR = dat[1];
   
 }
  else
 SPDR = 0x00;    // what to return to the master
 


}  // end of interrupt service routine (ISR) SPI_STC_vect

void loop (void)
{
 
}  // end of loop

Graynomad:
I know I'm going to regret this but here's my quick version of one way to do the slave

#define MAX_BYTES 4  // is there 4 bytes? if not change this and the arrays.

byte cmd_04_data[MAX_BYTES] = {1,2,3,4}; // you fill in the numbers
byte cmd_06_data[MAX_BYTES] = {5,6,7,8};
byte * data_ptr;
int byte_count = 0;

ISR (SPI_STC_vect) {
  byte c = SPDR;
  if (byte_count == 0) {
      // set a pointer to one or other array based on the byte just received
      data_ptr = (c == 0x04) ? cmd_04_data : cmd_06_data;
  }
  SPDR = *(data_ptr + byte_count);
  byte_count++;   
  if (byte_count = MAX_BYTES) byte_count = 0;
}




It compiles but I can't test it. 

_____
Rob

Hi..!!

This code gives following result, which is attached here, blue line is MISO yellow MOSi and Pink line is SCK.

#define MAX_BYTES 4  // is there 4 bytes? if not change this and the arrays.

byte cmd_04_data[MAX_BYTES] = {1,2,3,4}; // you fill in the numbers 
byte cmd_06_data[MAX_BYTES] = {5,6,7,8};
byte * data_ptr;
int byte_count = 0;
void setup (void)
{
}
ISR (SPI_STC_vect) {
  byte c = SPDR;
  if (byte_count == 0) {
      // set a pointer to one or other array based on the byte just received
      data_ptr = (c == 0x04) ? cmd_04_data : cmd_06_data; 
  }
  SPDR = *(data_ptr + byte_count);
  byte_count++;   
  if (byte_count = MAX_BYTES) byte_count = 0;
} 
void loop (void)
{
 
}  // end of loop

TEK0000.BMP (76.1 KB)

TEK0001.BMP (76.1 KB)

That wasn't a stand-alone program, what happened to all your setup() code (most or which you don't need anyway I think).


Rob

[code][quote author=Nick Gammon link=topic=120454.msg999023#msg999023 date=1353098493]
[quote author=ranjeetray link=topic=120454.msg998331#msg998331 date=1353063009]
Can we do like this, after receiving data can we check(if condition) data and then send back data to master like this.
[/quote]

I really don't know what you are trying to do. You are doing an SPI.transfer in the middle of the ISR?

You send back data to the master [b]by assigning to SPDR in the interrupt service routine[/b]. No other way. What are you thinking?
[/quote]

Hi...!!

This way(SPDR = dat[0]) also it is not able to send back data to Master, and I am not able to give delay properly and synchronize with master. Slave is not getting chance to send back data, I think so.


[code]

#include "pins_arduino.h"
#include <SPI.h>
#define SS 10
int dat[24] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14};
byte c;
void ss_falling()
{
  c = 0;
}

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;
  // interrupt for SS falling edge
  attachInterrupt (0, ss_falling, FALLING);
   // disable timer interrupts
  TIMSK0 = 0;
  
}  // end of setup


// SPI interrupt routine
ISR (SPI_STC_vect)
{
  c = SPDR;
  
  if(c == 0x04)
  {
    
    SPDR = dat[0];
   
 }
 
   else if(c == 0x06)
  {
    SPDR = dat[1];
   
 }
  else
 SPDR = 0x00;    // what to return to the master
 


}  // end of interrupt service routine (ISR) SPI_STC_vect

void loop (void)
{
 
}  // end of loop

[/code][/code]