Created
July 10, 2015 15:30
-
-
Save michaelsarduino/6f7b9193988a2cee73cb to your computer and use it in GitHub Desktop.
Arduino Sketch, um Daten über eine SPI Verbindung zu empfangen.
This file contains hidden or 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 buf [100]; | |
volatile byte pos; | |
volatile boolean senden; | |
void setup (void) | |
{ | |
Serial.begin (9600); //starten der seriellen Verbindung | |
SPCR |= bit (SPE); //SPI im Slave Modus starten | |
pinMode(MISO, OUTPUT); | |
pos = 0; | |
senden = false; //Variablen fuer Buffer vorbereiten | |
SPI.attachInterrupt(); //SPI Interrupt schalten | |
} | |
ISR (SPI_STC_vect) //SPI Interrupt | |
{ | |
byte c = SPDR; //kopieren der Daten von SPI Buffer | |
if (pos < sizeof buf) | |
{ | |
buf [pos++] = c; //hinzufuegen des naechsten Zeichens | |
if (c == '\n') | |
senden = true; | |
} //wenn Zeilenumbruch an PC senden | |
} | |
void loop (void) | |
{ | |
if (senden) //warten auf Ende der Zeilenumbruch (\n) | |
{ | |
buf [pos] = 0; | |
Serial.println (buf); | |
pos = 0; //senden der Daten ueber serielle Verbindung | |
senden = false; //senden -> falsch | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment