I've been trying to follow the posts on Nick Gammon's page which are admittedly a bit old, but seem to be everyone's recommendation for trying out Arduino to Arduino SPI. I'm able to get communications working, but there's a small issue I can't seem to pinpoint. Every once in a while there are three characters that show up in my output that are completely unexpected. Where are these characters coming from?
Here is what they are:
Hello, world!
Hello, wo&⸮BHello, world!
Hello, world!
Hello, world!
Hello, world!
In the second row, you'll see &⸮B
in place of what should be rld!\n
.
I have two Name-brand Arduino UNOs both being powered via USB to my computer, and they each have pins 13,12,11, and 10 directly wired to each other. Here's the code I'm using:
The unexpected characters are the same every time, and seemingly show up as UTF-8...the decimal values for those characters amounts to 38, 226, 66, 10. I'm not sure if that has any significance.
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
const char p [14] = "Hello, world!\n";
for (int i = 0; i < 14; i++) {
SPI.transfer(p[i]);
Serial.print(p[i]);
}
// disable Slave Select
digitalWrite(SS, HIGH);
delay (2000);
}
Slave:
#include <SPI.h>
volatile 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);
// turn on interrupts
SPCR |= _BV(SPIE);
// 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)
{
if (c == '\n') {
buf [pos++] = '0円';
process_it = true;
}
else{
buf [pos++] = c;
}
}
}
void loop (void)
{
if (process_it){
char * bufp = buf;
Serial.println (bufp);
pos = 0;
process_it = false;
}
}
1 Answer 1
Despite the fact that both units were being powered from the same USB Hub, there was a difference in voltage on the GNDs of each Arduino. Jumping the GND pins between the two got rid of the stray characters and strange behavior. D'oh.
"Hello, world!\n"
is 15 bytes long not 14. \$\endgroup\$