hi,
i want to send 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 to send data from slave to master and then master will transmit serially to computer (salve--->Master>>>compter) , could any one help me ? i will be very thankful
In SPI, only master can initiate a transfer. Also, there seems to be no support for slave mode SPI in standard Arduino library.
In SPI, only master can initiate a transfer.
i have see many example in which master arduino receive data from slave like in Aruino library example Arduino master receive data from salve Barometric Pressure Sensor.
Also, there seems to be no support for slave mode SPI in standard Arduino library.
i use the Nick Gammon code as link is mention above ,to get this job
Well, what have you tried? In my example code the master starts a transfer, the slave gets an interrupt, finds out something, and then replies.
Well, what have you tried?
i wrote a code for master and slave which is given below but i got a garbage value from master
i aslo post this question here Arduino Forum i spend a month to comunicate arduinos but still didn't success , first i had used softserial arduino library but it was a bit slow which is not good for my project.
code for Master
#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
and for Slave
#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;
}
}
Your slave code doesn't look anything like the one I had on my SPI page you referred to.
Which was:
// Written by Nick Gammon
// April 2011
#include "pins_arduino.h"
// what to do with incoming data
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
ohhh!!! i forgot that example
now i going to do it.thanks