User Tools

Site Tools


arduino:gammon-spi-peripheral

From SPI - Serial Peripheral Interface - for Arduino by Nick Gammon.

gammon-spi-peripheral.ino
// Written by Nick Gammon (with Mithat formatting mods).
// February 2011
 
#include <SPI.h>
 
char buf[100];
volatile byte pos;
volatile boolean 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) - 1))
    buf[pos++] = c;
 
  // example: newline means time to process buffer
  if (c == '\n')
    process_it = true;
 
}  // 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
arduino/gammon-spi-peripheral.txt · Last modified: 2024/12/13 03:46 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki