-
-
Save NicoHood/b33c68bf6c68d7983509 to your computer and use it in GitHub Desktop.
SPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written by Nick Gammon | |
// February 2011 | |
#include <SPI.h> | |
void setup (void) | |
{ | |
pinMode(SS,OUTPUT); | |
pinMode(10,OUTPUT); | |
digitalWrite(SS, HIGH); // ensure SS stays high for now | |
digitalWrite(10, 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); | |
Serial.begin (115200); // debugging | |
Serial.println("Start"); | |
} // end of setup | |
void loop (void) | |
{ | |
char c; | |
// enable Slave Select | |
digitalWrite(10, LOW); // SS is pin 10 | |
Serial.println("Print"); | |
// send test string | |
for (const char * p = "Hello, world!\n" ; c = *p; p++) | |
SPI.transfer (c); | |
// disable Slave Select | |
digitalWrite(10, HIGH); | |
delay (1000); // 1 seconds delay | |
} // end of loop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written by Nick Gammon | |
// February 2011 | |
#include <SPI.h> | |
char buf [100]; | |
volatile byte pos; | |
volatile boolean process_it; | |
void setup (void) | |
{ | |
Serial.begin (115200); // debugging | |
Serial.println("Start"); | |
// have to send on master in, *slave out* | |
pinMode(MISO, OUTPUT); | |
//pinMode(SS, OUTPUT); | |
//digitalWrite(SS, LOW); | |
// 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment