I have been working off the SPI slave example at Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino and am trying to implement similar logic (read MOSI and print to Serial), but with HEX numbers, specifically 2 byte HEX values that I am receiving via Bluetooth.
I have checked my SPI master out using a logic analyzer and am positive I am sending out correct values, however when I try running my arduino code, I get random numbers instead of my expected hex values.
Any help would be greatly appreciated.
#include <SPI.h>
byte 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 (pos==1)
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.write (buf);
pos = 0;
process_it = false;
} // end of flag set
} // end o