SPI Notes - Hirokuzu/EECE281_project2 GitHub Wiki

SPI is used to synchronize data transfer between microcontrollers. Each microcontroller uses 4 pins, 3 of which are shared, the last being the connection between one slave and the master. They are as follows:

Pin name Purpose Pin Number (Uno)
MOSI (Master out Slave in) Transfer data from Master to Slave 11
MISO (Master in Slave out) Transfer data from Slave to Master 12
SCK (serial clock) Clock generated by master to synchronize data transmission 13
SS (Slave select) 10 (Slave mode only)

As listed above, the Slave select pin is only used in slave mode. To avoid the Arduino from switching into slave mode, make sure that pin 10 is set to OUTPUT.

Transferring Over SPI

Make sure you SPI.begin() to start using the SPI pins in SPI mode! The pin values will be initialized for you. If you need to switch back, use SPI.end().

It's just the SPI.transfer(val)(http://arduino.cc/en/Reference/SPITransfer) function! This sends a one-byte val out and returns one byte read from SPI.

Things to be aware of:

Because SPI is a fairly loose standard, different devices might implement different methods of data transfer. The below SPI functions are here to help you out:

  • SPI.setBitOrder(order) - set the order in which we receive the bits. Is it least significant bit first? Or Most significant?
  • SPI.setDataMode(mode) - Whether clocks are idle high or low, and whether the sampling starts on the rising or falling clock. There are predefined values.
  • SPI.setClockDivider(divider) - sets the SPI clock to a fraction of the system clock. There are predefined values (multiples of 2)

References

  1. Arduino SPI Library