Search the Arduino Website
Reference Language (http://arduino.cc/en/Reference/HomePage) | Libraries (http://arduino.cc/en/Reference/Libraries) | Comparison
(http://arduino.cc/en/Reference/Comparison) | Changes (http://arduino.cc/en/Reference/Changes)
SPI library
This library allows you to communicate with SPI devices, with the Arduino as the
master device.
A Brief Introduction to the Serial Peripheral
Interface (SPI)
Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by
microcontrollers for communicating with one or more peripheral devices quickly
over short distances. It can also be used for communication between two
microcontrollers.
With an SPI connection there is always one master device (usually a
microcontroller) which controls the peripheral devices. Typically there are three
lines common to all the devices:
MISO (Master In Slave Out) - The Slave line for sending data to the master,
MOSI (Master Out Slave In) - The Master line for sending data to the
peripherals,
SCK (Serial Clock) - The clock pulses which synchronize data transmission
generated by the master
and one line specific for every device:
SS (Slave Select) - the pin on each device that the master can use to enable and
disable specific devices.
When a device's Slave Select pin is low, it communicates with the master. When
it's high, it ignores the master. This allows you to have multiple SPI devices sharing
the same MISO, MOSI, and CLK lines.
To write code for a new SPI device you need to note a few things:
Is data shifted in Most Significant Bit (MSB) or Least Significant Bit (LSB) first?
This is controlled by the SPI.setBitOrder() function.
Is the data clock idle when high or low? Are samples on the rising or falling edge
of clock pulses? These modes are controlled by the SPI.setDataMode()
function.
What speed is the SPI running at? This is controlled by the SPI.setClockDivider()
function.
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
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)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-