SPI communication between master arduino uno and slave arduino uno

Hi, i have written a code for master arduino uno and slave arduino uno comunication according to @nickgammon ,it works well ,but the challenge is one master arduino Sending 3 chipselects using i/o pins 5,6,7 and one slave arduino with 3 chipselects as input accordly,it has to work ,i didnt get a idea using isr my slave code as below
SLAVE CODE:

volatile byte command = 0;

void setup (void)
{

// have to send on master in, slave out
pinMode(MISO, OUTPUT);

// 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;

switch (command)
{
// no command? then this is the command
case 0:
command = c;
SPDR = 0;
break;

// add to incoming byte, return result
case 'a':
SPDR = c + 15; // add 15
break;

// subtract from incoming byte, return result
case 's':
SPDR = c - 8; // subtract 8
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 (SS) == HIGH)
command = 0;
} // end of loop

I didn't understand, why do you need a three chipselects? Standard SPI protocol uses the one chipselect for each spi slave.

yeah , spi need only 1 chipselect ,but here one master has to control 3 slaves arduinos that the task, ,but i am using one slave arduino and passing 3 cs pins i.e : 5,6,7 i/o pins as chipselect for one slave itself.
master code is like cs1 go low and high then cs2 go low and high and then cs3 go low and high .but i dont have a idea ,is it use 3 or 1 interrupts and what should be used as interrupt and interrupt subroutine vector in above slave code..

Again, if you have only one slave - you need a single chipselect.

If you try to emulate a three slave on the single arduino board - this way bring you a more troubles than benefits. If you need to test three slave codes - take a three arduino boards.
if you just need to perform three different tasks on slave board, you don’t need three SPI slaves for this. use only one.

yeah its crct, while using 3 slave arduino's with one master, spi require 3 chipselect but spi has only one pin as chipselect i.e 10th pin , first time we can connect with one slave ,later without removing that chipselect (i.E 10th pin )of first slave and connecting to slave 2 and again removing cs 10th pin connection and connecting to slave 3 , just i want to use i/o pins as chip selects is it possible .
one more is like we just want to do different functionality on one slave board by giving 3 cs1,cs2,cs3 on one slave board is it possible..

No, you have a misunderstanding with it. Yes, the SPI has only one predefined SS pin, but you can assign any other pin (and more than one) as chipselect if you need.
On the SPI master you have to use a different chipselects for every spi slave:

I think a using a different spi slaves(on the same board) for different task is senseless.
If you want to do this, then get ready, at a minimum, rewrite most of the SPI library, because the standard SPI code does not support the existence of more than one slave on the board.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.