User Tools

Site Tools


arduino:gammon-spi-data-peripheral

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

gammon-spi-data-peripheral.ino
// Written by Nick Gammon
// April 2011
 
// what to do with incoming data
volatile byte command = 0;
 
void setup (void)
{
 
  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
 
  // turn on SPI in slave mode
  SPCR |= _BV(SPE);
 
  // turn on interrupts
  SPCR |= _BV(SPIE);
 
}  // end of setup
 
 
// SPI interrupt routine
ISR (SPI_STC_vect)
{
  byte c = SPDR;
 
  switch (command)
  {
  // no command? then this is the command
  case 0:
    command = c;
    SPDR = 0;
    break;
 
  // add to incoming byte, return result
  case 'a':
    SPDR = c + 15;  // add 15
    break;
 
  // subtract from incoming byte, return result
  case 's':
    SPDR = c - 8;  // subtract 8
    break;
 
  } // end of switch
}  // end of interrupt service routine (ISR) SPI_STC_vect
 
void loop (void)
{
 
  // if SPI not active, clear current command
  if (digitalRead (SS) == HIGH)
    command = 0;
}  // end of loop
arduino/gammon-spi-data-peripheral.txt · Last modified: 2023/07/25 00:14 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki