1 // Interrupt-driven SPI SLAVE example
2 // Recieves bytes over SPI and stuffs them in
3 // a sort-of-circular-buffer that is printed to Serial.
4 // Uses a mix of Arduino and plain AVR code,
5 // but does not use the Arduino SPI library.
7 #include <avr/interrupt.h>
9 volatile uint8_t buffer[16];
10 volatile uint8_t bptr;
16 Serial.println("Hello from SLAVE")
18 // A nice little debug/status pin, set as OUTPUT using the Arduino-way ;)
21 // Make sure our buffer-pointer starts at the first buffer byte (pos 0)
24 // Set MISO as OUTPUT using the AVR-way ;)
26 // Enable SPI (default Slave) and enable SPI interrupts
27 SPCR=(1<<SPE)|(1<<SPIE);
29 // Activate interrupts
35 // In the main loop, simply output the buffer contents, over and over again..
37 for ( uint8_t i = 0; i <= 15; i++ )
41 Serial.print(buffer[i]);
44 Serial.println(" ... ");
48 // The ISR for SPI Serial Transfer Complete gets triggered
49 // on each byte transferred.
52 // Put data in buffer, increment buffer pointer, wrap around on end.
53 buffer[bptr] = spi_read();
55 if (bptr > 15) bptr = 0;
59 // Getting the transferred byte is as imple as reading the Data Register
60 // each time a byte has completed transfer. This and the ISR, needs to be
61 // quick, so that the next byte does not overwrite the current one.
63 // Sending is done by setting the data register before reading ...
67 // Let's add some fun "things happen because of transfers"...
68 if ( d == 45 ) digitalWrite(7, ( !digitalRead(7) ) );