Go Down

Topic: how to transmit data from SPI Slave Arduino to SPI Master Arduino (Read 1 time) previous topic - next topic

hi,
    i want to transmit data between two Arduino through SPI communication , i find this link http://www.gammon.com.au/forum/?id=10892 by Nick Gammon in which SPI Master Arduino transmit data to Slave Arduino and then Slave Arduino transmit the data serially to computer but in my case i want transmit data from slave to master and then master will transmit serially to computer, could any one help me ? i will be very thankful

Graynomad

An SPI slave cannot initiate the transfer of data, that has to be done by the master.

You should be able to use Nick's example and just have the slave to the USB stuff instead of the master.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

but the problem is that i am using 8 Arduino in my project each one has specific task the Master Arduino will collect all the data from Slave Arduinos and send it to computer through Xbee , i use Softserial Arduino library but that was a bit slow which not good for my proect  :~

Graynomad

Quote
i want to transmit data between two Arduino through SPI communication

So it's 8 then?

You need 8 digital lines to select each slave one at a time. You select the slave, transfer data, deselect the slave, move to the next slave etc etc until all 8 are done.

Connect all 9 MISOs, MOSIs and SCKs together, run 8 signals from the master, 1 to each slave, then follow Nick's example.

To get a single byte from each slave you would do something like this on the master.

Code: [Select]
byte SSpins [] = {3,4,5,6,7,8,9,10};
byte SlaveData [8];

setup () {
    for (int i - 0; i < 8; i++)
        pinMode (SSpins[i], OUTPUT);
}

loop () {
    for (int i - 0; i < 8; i++) {
        digitalWrite (SSpins[i], LOW);
        delay(1);   // give the slave some time to respond, may not be required, depends on the slave code
        SlaveData[i] = SPI.transfer();
        digitalWrite (SSpins[i], HIGH);
    }
    for (int i - 0; i < 8; i++)
       Serial.print(SlaveData[i], HEX);
}


This is quite rough and you still have to do the slave part.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

thanks for help ,
Quote
So it's 8 then?

if i become able to communicate between two then it is easy for 8.
Quote
You need 8 digital lines to select each slave one at a time. You select the slave, transfer data, deselect the slave, move to the next slave etc etc until all 8 are done.

Connect all 9 MISOs, MOSIs and SCKs together, run 8 signals from the master, 1 to each slave, then follow Nick's example.

i will do it latter for 8 arduino frist i want to do it between two .
regarding you code i wrote the following code but i receive garbage data, here is my code

for Master
Code: [Select]
#include <SPI.h>
char buf [14];
volatile byte pos=0;
void setup (void)
{
Serial.begin (115200);
  digitalWrite(SS, HIGH);  // ensure SS stays high for now
  SPI.begin ();

  SPI.setClockDivider(SPI_CLOCK_DIV128);
 
}  // end of setup
void loop (void)
{
  digitalWrite(SS, LOW);    // SS is pin 10
delay (1);
  for (int i=0;i<14;i++)
    buf[i]=SPI.transfer(0);
  // disable Slave Select
  digitalWrite(SS, HIGH);
  Serial.println(buf);

}  // end of loop



for Slave

Code: [Select]
#include <SPI.h>
char data [14]="Hello world!\n";
volatile boolean process_it;

void setup (void)
{
  pinMode(MISO, OUTPUT);
  // turn on SPI in slave mode
  SPCR |= _BV(SPE);
  // get ready for an interrupt
  process_it = false;
  // now turn on interrupts
  SPI.attachInterrupt();
}  // end of setup

// SPI interrupt routine
ISR (SPI_STC_vect)
{
process_it = true;
     

// main loop - wait for flag set in interrupt routine
void loop (void)
{
  if (process_it)
    {
    for(int i=0;i<sizeof data ; i++)
    SPI.transfer(data[i]);
    process_it = false;
    } 
}


here is the output from Master Arduino


terryking228

So, what you REALLY need is a communications interface that is MADE for multiple Arduinos.  Best answer is probably RS-485.

Nick also has this on RS-485: http://www.gammon.com.au/forum/?id=11428

Some example interface modules: http://goo.gl/S1lFR



DISCLAIMER: Mentioned stuff from my own shop... as usual... that's what I know about.
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

By RS-485 i could not distinguished that which data stream are coming from which slave Arduino   :(

terryking228

Quote
By RS-485 i could not distinguished that which data stream are coming from which slave Arduino


You need to look more closely at nick's example. Each node has it's own identification and the relationship does not need to be master-slave.

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Graynomad

I haven't checked lately but I doubt that slave code is anything like Nick's example. It it right?

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com


Go Up