Created
October 29, 2019 21:14
-
-
Save ArsenioDev/19887dc89a37ff6245ff857dd84b4fa9 to your computer and use it in GitHub Desktop.
Arduino AVR SPI master/slave
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
#include <SPI.h> | |
void setup () { | |
Serial.begin(115200); //set baud rate to 115200 for usart | |
digitalWrite(SS, HIGH); // disable Slave Select | |
SPI.begin (); | |
SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8 | |
} | |
void loop () { | |
char c; | |
digitalWrite(SS, LOW); // enable Slave Select | |
// send test string | |
for (const char * p = "Hello, world!\r" ; c = *p; p++) | |
{ | |
SPI.transfer (c); | |
Serial.print(c); | |
} | |
digitalWrite(SS, HIGH); // disable Slave Select | |
delay(2000); | |
} |
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
#include <SPI.h> | |
char buff [50]; | |
volatile byte indx; | |
volatile boolean process; | |
void setup () { | |
Serial.begin (115200); | |
pinMode(MISO, OUTPUT); // have to send on master in so it set as output | |
SPCR |= _BV(SPE); // turn on SPI in slave mode | |
indx = 0; // buffer empty | |
process = false; | |
SPI.attachInterrupt(); // turn on interrupt | |
} | |
ISR (SPI_STC_vect) // SPI interrupt routine | |
{ | |
byte c = SPDR; // read byte from SPI Data Register | |
if (indx < sizeof buff) { | |
buff [indx++] = c; // save data in the next index in the array buff | |
if (c == '\r') //check for the end of the word | |
process = true; | |
} | |
} | |
void loop () { | |
if (process) { | |
process = false; //reset the process | |
Serial.println (buff); //print the array on serial monitor | |
indx= 0; //reset button to zero | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment