Hello, I'm trying to get 2 arduinos talking over SPI. I want the master to send something (anything really, doesn't matter what it is) to the slave, and then have the slave echo that same byte straight back to the master. Can anybody give me a super simple, bare bones example of this? Heres what I have at the moment, which doesn't work (also from 2 seperate sketches I downloaded so probably messy...)
MASTER:
#include <SPI.h>
void setup (void)
{
digitalWrite(SS, HIGH); // ensure SS stays high for now
// 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);
Serial.begin(115200);
} // end of setup
void loop (void)
{
int c = 0x0FF;
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
// send test string
SPI.transfer (c);
Serial.println("Sent");
// disable Slave Select
digitalWrite(SS, HIGH);
delay (1000); // 1 seconds delay
} //
SLAVE:
#include <SPI.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
Serial.begin (115200); // debugging
// have to send on master in, slave out
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
// add to buffer if room
if (pos < sizeof buf)
{
buf [pos++] = c;
// example: newline means time to process buffer
} // end of room available
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
Serial.println (buf);
pos = 0;
} // end of flag set