Pages: [1]   Go Down
Author Topic: Arduino with multiple slaves using spi  (Read 351 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: September 17, 2013, 12:53:47 pm by ashritha » Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1185
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

so i need to change here?
Code:
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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1185
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Pages: [1]   Go Up
Jump to: