I used sketches attributed mainly to Nick Gammon (with a special THANKS to him for his contributions here!) to work on two Arduino Mega’s using IDE 1.5.5. It worked, with problems. I tried the correction listed in the last part of Reply #7 by Nick Gammon above, but because I do not understand where the correction should go it is not included. I have added the sample output where it is obvious that the “Hello world“ lines print more than once on a single line on the monitor and some letters come out wrong.
The master program I used is:
/* SPI Master Send - Hello World (derived from Nick Gammon's samples)
Copied and modified from Reply #6 by Rofarley: http://forum.arduino.cc/index.php?topic=120049.0
Arduino Mega is acting as an SPI Master for
a corresponding Arduino Mega board
Circuit:
SS_PIN: pin 53
MISO: pin 50
MOSI: pin 51
SCK: pin 52
*/
#include <SPI.h>
long int count = 1;
byte config = B01010000;
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define SS_PIN 53
uint8_t buffer[128];
void setup()
{
digitalWrite(SS_PIN, HIGH); // Ensure SS stays high for now
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16);
Serial.begin(115200);
}
void loop()
{
char c;
// enable Slave Select
digitalWrite(SS_PIN, LOW);
// Send test string
for (const char *p="Hello, world!\n";c=*p;p++)
{
SPI.transfer(c);
Serial.print(c);
}
//delay(100);
// Disable slave select
digitalWrite(SS_PIN, HIGH);
// 0.1 second delay
delay(100);
}
The Slave program I used is:
// Written by Nick Gammon
// February 2011
/**
* Send arbitrary number of bits at whatever clock rate (tested at 500 KHZ and 500 HZ).
* This script will capture the SPI bytes, when a '\n' is recieved it will then output
* the captured byte stream via the serial.
*/
#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
if (c == '\n')
process_it = true;
} // end of room available
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
Serial.println (buf);
pos = 0;
process_it = false;
} // end of flag set
} // end of loop
The output irregularities occur irregularly and fairly frequently. A sample of these is depicted below:
Hello, world!
Hello, world!
Hello, world!Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, wo&ÆBHello, world!
Hello, world!
Hello, world!Hello, world!
Hello, worBHello, world!
Hello, world!
Hello, world!
Question 1: Could anyone please sprinkle some wisdom on these sketches to make them print correctly?
The Master program compiles properly on the Arduino Due, but the slave program simply does not compile.
Question 2: What changes to the Slave program should I make to make the Slave sketch work with the DUE?
Thanks for giving this attention!