From 9a73e484d72c6e4edab542964aa59caa96e4e1b6 Mon Sep 17 00:00:00 2001 From: Raghav <62105787+MythicalCow@users.noreply.github.com> Date: Sun, 18 Jul 2021 16:13:20 -0400 Subject: [PATCH] Add files via upload --- communication/MasterDuinoTest.ino | 35 ++++++++++++++++++++ communication/SlaveDuinoTest.ino | 55 +++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 communication/MasterDuinoTest.ino create mode 100644 communication/SlaveDuinoTest.ino diff --git a/communication/MasterDuinoTest.ino b/communication/MasterDuinoTest.ino new file mode 100644 index 0000000..aa65168 --- /dev/null +++ b/communication/MasterDuinoTest.ino @@ -0,0 +1,35 @@ +#include + +void setup (void) +{ + + digitalWrite(SS, HIGH); // ensure SS stays high for now + + // Put SCK, MOSI, SS pins into output mode + // also put SCK, MOSI into LOW state, and SS into HIGH state. + // Then put SPI hardware into Master mode and turn SPI on + SPI.begin (); + + // Slow down the master a bit + SPI.setClockDivider(SPI_CLOCK_DIV8); + +} // end of setup + + +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); + + // disable Slave Select + digitalWrite(SS, HIGH); + + delay (1000); // 1 seconds delay +} // end of loop diff --git a/communication/SlaveDuinoTest.ino b/communication/SlaveDuinoTest.ino new file mode 100644 index 0000000..e458975 --- /dev/null +++ b/communication/SlaveDuinoTest.ino @@ -0,0 +1,55 @@ +#include + +char buf [100]; +volatile byte pos; +volatile bool process_it; + +void setup (void) +{ + Serial.begin (115200); // debugging + + // turn on SPI in slave mode + SPCR |= bit (SPE); + + // have to send on master in, *slave out* + pinMode (MISO, OUTPUT); + + // 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