Arduino - SPI
Arduino - SPI
SPI library
This library allows you to communicate with SPI devices, with the Arduino as the master device.
Functions
- begin (http://arduino.cc/en/Reference/SPIBegin)() - end (http://arduino.cc/en/Reference/SPIEnd)() - setBitOrder (http://arduino.cc/en/Reference/SPISetBitOrder)() - setClockDivider (http://arduino.cc/en/Reference/SPISetClockDivider)() - setDataMode (http://arduino.cc/en/Reference/SPISetDataMode)() - transfer (http://arduino.cc/en/Reference/SPITransfer)() - Due Extended SPI usage (http://arduino.cc/en/Reference/DueExtendedSPI)
Examples
- BarometricPressureSensor (http://arduino.cc/en/Tutorial/BarometricPressureSensor): Read air pressure and temperature from a sensor using SPI - SPIDigitalPot (http://arduino.cc/en/Tutorial/SPIDigitalPot): Control a digital potentiometer using SPI
See also
- shiftOut() (http://arduino.cc/en/Reference/ShiftOut) - shiftIn() (http://arduino.cc/en/Reference/ShiftIn)
The SPI standard is loose and each device implements it a little differently. This means you have to pay special attention to the device's datasheet when writing your code. Generally speaking, there are four modes of transmission. These modes control whether data is shifted in and out on the rising or falling edge of the data clock signal (called the clock phase), and whether the clock is idle when high or low (called the clock polarity). The four modes combine polarity and phase according to this table: Mode SPI_MODE0 SPI_MODE1 SPI_MODE2 SPI_MODE3 Clock Polarity (CPOL) 0 0 1 1 Clock Phase (CPHA) 0 1 0 1
The S P I . s e t D a t a M o d e ( )function lets you set the mode to control clock polarity and phase. Every SPI device has a maximum allowed speed for SPI Bus. The SPI.setClockDivider() allows you to change the clock speed to make your device working properly (default is 4MHz). Once you have your SPI parameters set correctly you just need to figure which registers in your device control which functions, and you're good to go. This will be explained in the data sheet for your device. For more on SPI, see Wikipedia's page on SPI (http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_Numbers).
Connections
The following table display on which pins the SPI lines are broken out on the different Arduino boards: Arduino Board Uno or Duemilanove Mega1280 or Mega2560 Leonardo Due MOSI 11 or ICSP4 51 or ICSP4 ICSP4 ICSP4 MISO 12 or ICSP1 50 or ICSP1 ICSP1 ICSP1 SCK 13 or ICSP3 52 or ICSP3 ICSP3 ICSP3 SS (slave) SS (master)
10
53 -
4, 10, 52
Note that MISO, MOSI, and SCK are available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on every board.
All AVR based boards have an SS pin that is useful when they act as a slave controlled by an external master. Since this library supports only master mode, this pin should be set always as OUTPUT otherwise the SPI interface could be put automatically into slave mode by hardware, rendering the library inoperative. It is, however, possible to use any pin as the Slave Select (SS) for the devices. For example, the Arduino Ethernet shield uses pin 4 to control the SPI connection to the on-board SD card, and pin 10 to control the connection to the Ethernet controller.
Reference Home (http://arduino.cc/en/Reference/HomePage) Corrections, suggestions, and new documentation should be posted to the Forum (http://arduino.cc/forum/index.php/board,23.0.html). The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License (http://creativecommons.org/licenses/by-sa/3.0/). Code samples in the reference are released into the public domain.
Share
NEWSLETTER
Enter your email to sign up
2014 Arduino
(http://www.facebook.com/official.arduino) (http://www.flickr.com/photos/arduino_cc)