0
\$\begingroup\$

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;
 } 
}
Null
7,81618 gold badges38 silver badges49 bronze badges
asked Jun 29, 2017 at 16:17
\$\endgroup\$
2
  • \$\begingroup\$ String "Hello, world!\n" is 15 bytes long not 14. \$\endgroup\$ Commented Jun 29, 2017 at 16:41
  • \$\begingroup\$ @gre_gor, I know. I was testing if that affected it. Previously also tested 15 and 16 as well to see if there was any change. No effect. \$\endgroup\$ Commented Jun 29, 2017 at 17:00

1 Answer 1

1
\$\begingroup\$

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.

answered Jun 29, 2017 at 20:59
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.