1.1 Data Transmission Between Master and Slave
The interaction between a master and a slave AVR is shown in Figure 1-1. Two identical SPI units are displayed. The left unit is configured as master while the right unit is configured as slave. The MISO, MOSI, and SCK lines are connected with the corresponding lines of the other part. The mode in which a part is running determines if they are input or output signal lines. Because a bit is shifted from the master to the slave and from the slave to the master simultaneously in one clock cycle both 8-bit shift registers can be considered as one 16-bit circular shift register. This means that after eight SCK clock pulses the data between master and slave will be exchanged.
- New bytes to be sent cannot be written to the data register (SPDR) / shift register before the entire shift cycle is completed.
- Received bytes are written to the Receive Buffer immediately after the transmission is completed.
- The Receive Buffer has to be read before the next transmission is completed or data will be lost.
- Reading the SPDR will return the data of the Receive Buffer.
After a transfer is completed the SPI Interrupt Flag (SPIF) will be set in the SPI Status Register (SPSR). This will cause the corresponding interrupt to be executed if this interrupt and the global interrupts are enabled. Setting the SPI Interrupt Enable (SPIE) bit in the SPCR enables the interrupt of the SPI while setting the I bit in the SREG enables the global interrupts.