/// SPI_On_Uno_Example_With_Interrupt.ino /// Send a short message over SPI as master or slave on AVR-based Arduino. /// /// Note: This program does not use the Arduino library. The Arduino library /// does not SPI slave mode on AVR processors. For simplicity, we use the /// hardware registers for both. /// /// When the program starts, it prompts the user, using the console, to select /// master or slave. /// /// When the user selects master, the program sets the _SS low and exchanges /// data with the slave. When it's done, it sets the _SS high and reports. /// /// In the slave mode, the program waits for a message to be selected. /// When the slave is selected, it waits for a message from the master and replies to /// each byte with its. When it is deselected, it reports the message to the console. /// /// This sketch /// /// Pins /// /// The SPI hardware on the AVR chip is connected to these ICSP pins. /// On the Uno, these pins are also connected to D11, D12, D13. /// /// The ICSP connector is used in all models of Ardunio for SPI. /// /// ICSP pins: /// 1 MISO 2 +VCC /// 3 SCK 4 MOSI /// 5 Reset 6 GND /// /// The _SS line is pin D10 /// #define _SS 10 #define BUFFER_SIZE 32 uint8_t masterMessage[BUFFER_SIZE+1] = "|--- Master Uno / interrupt ---|"; uint8_t slaveMessage[BUFFER_SIZE+1] = "|---- Slave Uno / interrupt ---|"; uint8_t inputBuffer[BUFFER_SIZE]; uint8_t outputBuffer[BUFFER_SIZE]; uint16_t inputBufferCounter; uint16_t outputBufferCounter; bool transmissionActiveFlag; /// true during a transmission bool slaveEnabledFlag; /// true when user has started slave mode bool oldSS; bool iAmMaster; bool iAmSlave; void setup() { transmissionActiveFlag = false; slaveEnabledFlag = false; iAmMaster = false; iAmSlave = false; Serial.begin ( 115200 ); while ( !Serial ) ; // wait for port to open showMenu(); } void loop() { loopSerial(); loopSPI(); } void loopSerial() { if ( Serial.available() ) { uint8_t clu = Serial.read(); switch ( clu ) { case 'S': if ( ! iAmMaster ) { if ( iAmSlave ) { SPCR = 0; Serial.println ( "stop listening" ); iAmSlave = false; } else { startSlave(); } } break; case 'M': masterSend(); break; } showMenu(); } } void loopSPI() { if ( iAmSlave && 0 ) { bool newSS = digitalRead ( _SS ) == LOW; if ( newSS != oldSS ) { if ( newSS ) Serial.println ( F("_SS became active" ) ); else Serial.println ( F("_SS became inactive" ) ); oldSS = newSS; } } if ( inputBufferCounter >= BUFFER_SIZE ) { reportInputBuffer(); outputBufferCounter = 0; } } void masterSend() { Serial.println (); Serial.println ( F("Sending messge as master" ) ); outputBufferCounter = 0; inputBufferCounter = 0; SPCR = 0; pinMode ( _SS, OUTPUT ); pinMode ( MISO, INPUT ); pinMode ( MOSI, OUTPUT ); pinMode ( SCK, OUTPUT ); // Enable the SPI in master mode. digitalWrite ( _SS, LOW ); SPCR = 1 << SPE | 1 << MSTR | 1 << SPR0; for ( outputBufferCounter = 0; outputBufferCounter < BUFFER_SIZE; outputBufferCounter++ ) { delay ( 50 ); // inputBuffer[inputBufferCounter++] = SPDR; SPDR = masterMessage[outputBufferCounter]; while(!(SPSR & (1<