Go Down

Topic: Arduino with multiple slaves using spi (Read 520 times) previous topic - next topic

ashritha

Sep 17, 2013, 07:38 pm Last Edit: Sep 17, 2013, 07:53 pm by ashritha Reason: 1
Hi,
    I have done my project using
    http://www.gammon.com.au/forum/?id=10892

    it helped me a lot.
  now i have to connect two arduinos as slaves
 can you help me with this?

Thank you.

Code: [Select]
#include <SPI.h>
const int SS = 10;
onst int SS1 = 8;
const int SS2 = 9;
byte slave = 0;
void setup (void)
{
digitalWrite(SS1,LOW);
 digitalWrite(SS2,HIGH);
 pinMode(10, OUTPUT);
  // ensure SS stays high for now

pinMode (SS1, OUTPUT);
 pinMode (SS2, OUTPUT);
 // 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
 SPI.begin ();

 // Slow down the master a bit
 SPI.setClockDivider(SPI_CLOCK_DIV8);
 
}  // end of setup


void loop (void)
{

 char c;
switch (slave) {
     case 0:
       digitalWrite(SS2,HIGH);
for (const char * p = "Hello, world!\n" ; c = *p; p++)
   SPI.transfer (c);  
     delay(1000);
       digitalWrite(SS1,LOW);
       break;
     case 1:
       digitalWrite(SS1,HIGH);
for (const char * p = "Hello, world!\n" ; c = *p; p++)
   SPI.transfer (c);
       delay(1000);
       digitalWrite(SS2,LOW);
       break;
 

 
}  // end of loop

I am connecting arduinos to pins 8 and 9.
is this code correct?
Thank you.

marco_c

No, it is not correct. The point of the SS pin is that it signals to the device that it is enabled when the pin is high. So if you have 2 devices, then when you want to communicate to one device, you put the SS1 or SS2 pin HIGH, do the comms, and then set the SAME SS pin LOW at the end.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

ashritha

so i need to change here?
Code: [Select]

case 0:
        digitalWrite(SS2,HIGH); /*this line?*/
for (const char * p = "Hello, world!\n" ; c = *p; p++)
    SPI.transfer (c); 
      delay(1000);
        digitalWrite(SS1,LOW);
        break;


i have least knowledge on programming..please help me

Thank you.

marco_c

The digitalWrite (SSx, HIGH) and the digitalWrite(SSx, LOW) need to be exactly the same (ie, x=1 or x=2, but the same x in both cases. I can't tell you if the first case should be all SS1 or SS2 as it is not clear, but they need to reference the same pin. One command puts it HIGH, the other puts it LOW. It is like lifting the handset on a telephone - you lift and put back the same one, you don't pick up one and then put back another one.
Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

Go Up