I'm trying unsuccessfully to make two Arduinos talk using SPI.
I found this great tutorial. The SPI Master send a constant string each second to the SPI Slave, which resends to the USART for depuration purpose. The problem is that nothing is printed on the Arduino's Serial Monitor.
The SPI Master is connected on a second computer and also sends the string to USART. The string arrives at the computer.
I tested all of Arduinos pins that I use and they're working (on both Arduinos).
My setup
- Arduino Leonardo: SPI Master
Seeeduino: SPI Slave
Leonardo_________Seeeduino
(10) SS--------->(10) SS
(11) MOSI------->(11) MOSI
(12) MISO<-------(12) MISO
(13) SCLK------->(13) SCLK
The code used on the SPI MASTER:
#include <SPI.h>
void setup (void)
{
Serial.begin(115200);
digitalWrite(SS, HIGH);
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8);
}
void loop (void)
{
char c;
// enable Slave Select
digitalWrite(SS, LOW); // SS is pin 10
// send test string
for (const char * p = "Hello, world!\n" ; c = *p; p++) {
SPI.transfer (c);
Serial.print(c);
}
// disable Slave Select
digitalWrite(SS, HIGH);
delay (1000);
}
And the code used on the SPI SLAVE:
#include <SPI.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
Serial.begin (115200);
// 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();
}
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
if (pos < sizeof buf)
{
buf [pos++] = c;
if (c == '\n')
process_it = true;
}
}
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
Serial.println (buf);
pos = 0;
process_it = false;
}
}
Is there something wrong on this project? How can I make it work?