Reference.SPI History

Hide minor edits - Show changes to output

November 21, 2012, at 12:55 PM by Federico -
Changed line 52 from:
||Uno or Duemilanove || 13 or ICSP-4 || 11 or ICSP-1 || 12 or ICSP-3 || 10 || - ||
to:
||Uno or Duemilanove || 11 or ICSP-4 || 12 or ICSP-1 || 13 or ICSP-3 || 10 || - ||
October 22, 2012, at 06:22 PM by Federico -
Changed lines 72-73 from:
Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52). Pin 78 is an SS pin connected to the on-board external data flash memory, used to store variables while the board is powered off.
to:
Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52).
September 24, 2012, at 12:46 PM by Federico -
Changed lines 34-38 from:
|| 0 || 0 || 0 ||
|| 1 || 0 || 1 ||
|| 2 || 1 || 0 ||
|| 3 || 1 || 1 ||
to:
|| SPI_MODE0 || 0 || 0 ||
|| SPI_MODE1 || 0 || 1 ||
|| SPI_MODE2 || 1 || 0 ||
|| SPI_MODE3 || 1 || 1 ||
September 17, 2012, at 03:27 PM by Federico -
Added lines 76-77:
(:cell width=35%:)
Deleted lines 97-100:
(:cell width=10%:)

(:cell width=35%:)
September 17, 2012, at 02:40 PM by Cristian -Vacuum- Maglie -
Changed lines 70-71 from:
* automatic handling of different device configurations (clock speed, data mode, etc) so every device can have its own configuration that is automatically selected.
to:
* automatic handling of different device configurations (clock speed, data mode, etc) so every device can have its own configuration automatically selected.
September 17, 2012, at 02:37 PM by Cristian -Vacuum- Maglie -
Changed lines 19-20 from:
* '''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:
* '''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.
September 17, 2012, at 02:37 PM by Cristian -Vacuum- Maglie -
Changed lines 13-18 from:
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,
* Master In Slave Out (MISO) - The Slave line for sending data to the master,
* Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
* Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the master, and
* Slave Select pin - 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:
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.
Changed lines 22-29 from:
*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? This and the clock idling 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 (called the clock '''polarity'''). The four modes combine polarity and phase. The SPI.setClockDivider() allows you to change the clock speed (default is 4MHz), and the @@SPI.setDataMode()@@ function lets you set the mode to control clock polarity and phase according to this table:

to:
* 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 (called the clock '''polarity'''). The four modes combine polarity and phase according to this table:
Changed lines 37-38 from:
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.
to:
The @@SPI.setDataMode()@@ 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.
Changed lines 49-56 from:
|| || '''MOSI''' || '''MISO''' || '''SCK''' || '''SS''' ||
||Uno / Duemilanove || 13 - ICSP-4 || 11 - ICSP-1 || 12 - ICSP-3 || 10 ||
||Mega1280 - Mega2560 || 51 - ICSP-4 || 50 - ICSP-1 || 52 - ICSP-3 || 53 ||
||Leonardo || ICSP-4 || ICSP-1 || ICSP-3 || - ||
||Due || ICSP-4 || ICSP-1 ||ICSP-3 || 4, 10, 52 ||

MISO, MOSI, and SCK are also available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on the Uno and the Mega.
to:
|| '''Arduino Board''' || '''MOSI''' || '''MISO''' || '''SCK''' || '''SS (slave)''' || '''SS (master)''' ||
||Uno or Duemilanove || 13 or ICSP-4 || 11 or ICSP-1 || 12 or ICSP-3 || 10 || - ||
||Mega1280 or Mega2560 || 51 or ICSP-4 || 50 or ICSP-1 || 52 or ICSP-3 || 53 || - ||
||Leonardo || ICSP-4 || ICSP-1 || ICSP-3 || - || - ||
||Due || ICSP-4 || ICSP-1 ||ICSP-3 || - || 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.
Changed lines 59-78 from:
Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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.

!!!!Due Differences
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off.

The SPI library can be used on the Due with the standard methods available to other Arduino boards based on AVR microcontrollers, or in the extended mode. This mode extends the standard methods, by using the features of the SAM3X microcontroller. This enables automatic handling of the slave selection between multiple devices sharing the SPI bus (each device can have also different configurations such as clock speed and datamode).

(:cell width=10%:)

(:cell width=35%:)

!!!!Functions

* [[SPIBegin | begin]]()
* [[SPIEnd | end]]()
* [[SPISetBitOrder | setBitOrder]]()
* [[SPISetClockDivider | setClockDivider]]()
* [[SPISetDataMode | setDataMode]]()
* [[SPITransfer | transfer]]()
to:
!!!!Note about Slave Select (SS) pin on AVR based boards
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.
Added lines 65-82:

The Arduino Due's SPI interface works differently than any other Arduino boards. The library can be used on the Due with the same methods available to other Arduino boards or using the '''extended methods'''. The extended methods exploits the the SAM3X hardware and allows some interesting features like:
* automatic handling of the device slave selection.
* automatic handling of different device configurations (clock speed, data mode, etc) so every device can have its own configuration that is automatically selected.

Arduino Due has three exposed pins for the devices Slave Select (SS) lines (pins 4, 10, and 52). Pin 78 is an SS pin connected to the on-board external data flash memory, used to store variables while the board is powered off.

[[DueExtendedSPI | Due Extended SPI usage]]

!!!!Functions

* [[SPIBegin | begin]]()
* [[SPIEnd | end]]()
* [[SPISetBitOrder | setBitOrder]]()
* [[SPISetClockDivider | setClockDivider]]()
* [[SPISetDataMode | setDataMode]]()
* [[SPITransfer | transfer]]()
Added lines 94-97:
(:cell width=10%:)

(:cell width=35%:)
September 12, 2012, at 12:39 AM by Scott Fitzgerald -
Changed lines 25-27 from:
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. The @@SPI.setDataMode()@@ function lets you set the mode to control clock polarity and phase according to this table:

to:
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. The SPI.setClockDivider() allows you to change the clock speed (default is 4MHz), and the @@SPI.setDataMode()@@ function lets you set the mode to control clock polarity and phase according to this table:

September 11, 2012, at 07:34 PM by Scott Fitzgerald -
Changed lines 56-57 from:
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off. The available SS pins for user's purpose are three and are listed in the table above.
to:
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off.
September 11, 2012, at 07:33 PM by Scott Fitzgerald -
Changed lines 56-59 from:
The Arduino Due differently to the other Arduino boards provides only four pins for the Slave Select (SS) line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off. The available SS pins for user's purpose are three and are listed in the table above.

With the Due the SPI library can be used in two different ways, with the standard methods shared with the other Arduino boards based on AVR microcontrollers, or in the extended mode, that extend the standard methods, that consists in using the feature of the microcontroller (SAM3X) that automatically handle the slave selection between multiple devices sharing the SPI bus (each device can have also different configurations such as clock speed and datamode).
to:
The Arduino Due's SPI interface works differently than other Arduino boards. It has three exposed pins for the Slave Select (SS) line (pins 4, 10, and 52). Pin 78 is an SS pin connected to the onboard external data flash, used to store variables while the board is powered off. The available SS pins for user's purpose are three and are listed in the table above.

The SPI library can be used on the Due with the standard methods available to other Arduino boards based on AVR microcontrollers, or in the extended mode. This mode extends the standard methods, by using the features of the SAM3X microcontroller. This enables automatic handling of the slave selection between multiple devices sharing the SPI bus (each device can have also different configurations such as clock speed and datamode).
August 29, 2012, at 01:12 PM by Federico -
Changed lines 25-27 from:
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 three 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 three modes combine polarity and phase. The @@SPI.setDataMode()@@ function lets you set the mode to control clock polarity and phase according to this table:

to:
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. The @@SPI.setDataMode()@@ function lets you set the mode to control clock polarity and phase according to this table:

Changed lines 44-48 from:
||Uno / Duemilanove || 13 || 11 || 12 || 10 ||
||Mega1280 / Mega2560 || 51 || 50 || 52 || 53 ||
||Leonardo || ICSP || ICSP || ICSP || ICSP ||
||Due || ICSP || ICSP ||ICSP || 4, 10, 52 ||
to:
||Uno / Duemilanove || 13 - ICSP-4 || 11 - ICSP-1 || 12 - ICSP-3 || 10 ||
||Mega1280 - Mega2560 || 51 - ICSP-4 || 50 - ICSP-1 || 52 - ICSP-3 || 53 ||
||Leonardo || ICSP-4 || ICSP-1 || ICSP-3 || - ||
||Due || ICSP-4 || ICSP-1 ||ICSP-3 || 4, 10, 52 ||
Added lines 51-52:
Attach:ICSPHeader.jpg
August 28, 2012, at 06:00 PM by Federico -
Changed lines 51-52 from:
Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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. Differently to the other boards the Due provides only 4 pins for the Slave Select line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off.
to:
Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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.

!!!!Due Differences
The Arduino Due differently to the other Arduino boards provides only four pins for the Slave Select (SS) line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off. The available SS pins for user's purpose are three and are listed in the table above.

With the Due the SPI library can be used in two different ways, with the standard methods shared with the other Arduino boards based on AVR microcontrollers, or in the extended mode, that extend the standard methods, that consists in using the feature of the microcontroller (SAM3X) that automatically handle the slave selection between multiple devices sharing the SPI bus (each device can have also different configurations such as clock speed and datamode).
August 28, 2012, at 05:27 PM by Federico -
Changed lines 28-34 from:
|| border=1
||!Mode ||!Clock Polarity (CPOL) ||!Clock Phase (CPHA) ||
||0 ||0 ||0 ||
||1 ||0 ||1 ||
||2 ||1 ||0 ||
||3 ||1 ||1 ||
to:
|| border=1 style="border-collapse:collapse" cellpadding=5 width=90%
|| '''Mode''' || '''Clock Polarity (CPOL)''' || '''Clock Phase (CPHA)''' ||
|| 0 || 0 || 0 ||
|| 1 || 0 || 1 ||
|| 2 || 1 || 0 ||
|| 3 || 1 || 1 ||
Changed lines 40-43 from:
On the Arduino Duemilanove and other ATmega168 / 328-based boards, the SPI bus uses pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). MISO, MOSI, and SCK are also available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on the Uno and the Mega.

Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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.
to:
The following table display on which pins the SPI lines are broken out on the different Arduino boards:

||border=1 style="border-collapse:collapse" cellpadding=5 width=90%
|| || '''MOSI''' || '''MISO''' || '''SCK''' || '''SS''' ||
||Uno / Duemilanove || 13 || 11 || 12 || 10 ||
||Mega1280 / Mega2560 || 51 || 50 || 52 || 53 ||
||Leonardo || ICSP || ICSP || ICSP || ICSP ||
||Due || ICSP || ICSP ||ICSP || 4, 10, 52 ||

MISO, MOSI, and SCK are also available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on the Uno and the Mega.

Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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. Differently to the other boards the Due provides only 4 pins for the Slave Select line, and one of them (pin 78) is connected to the onboard external data flash, used to store user's variables while the board is powered off.
Changed lines 78-79 from:
(:tableend:)
to:
(:tableend:)
August 26, 2012, at 02:10 AM by Scott Fitzgerald -
Added lines 57-59:
!!!!Extended SPI functionality for the Due
* [[DueExtendedSPI | Due Extended SPI usage]]
June 29, 2012, at 05:04 AM by David A. Mellis -
Changed lines 40-43 from:
On the Arduino Duemilanove and other ATmega168 / 328-based boards, the SPI bus uses pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative.

It is possible to use a pin other than pin 10 as the slave select (SS) pin. 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.
to:
On the Arduino Duemilanove and other ATmega168 / 328-based boards, the SPI bus uses pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). MISO, MOSI, and SCK are also available in a consistent physical location on the ICSP header; this is useful, for example, in designing a shield that works on the Uno and the Mega.

Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative. It is, however, possible to use a pin other than pin 10 as the slave select (SS) pin. 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.
December 16, 2010, at 04:08 AM by David A. Mellis -
Changed lines 63-64 from:
* [[ShiftOut | shiftOut]]()
to:
* [[ShiftOut | shiftOut()]]
* [[ShiftIn | shiftIn()]]
November 21, 2010, at 07:44 PM by David A. Mellis -
Changed lines 28-29 from:
|| border = 1
||!Mode ||!Clock Polarity (CPOL) ||!Clock Phase (CPHA)''' ||
to:
|| border=1
||!Mode ||!Clock Polarity (CPOL) ||!Clock Phase (CPHA) ||
November 21, 2010, at 07:43 PM by David A. Mellis -
Changed lines 3-6 from:
to:
(:table:)

(:cell width=55%:)
Deleted lines 8-17:
!!!!Functions

* [[SPIBegin | begin]]()
* [[SPIEnd | end]]()
* [[SPISetBitOrder | setBitOrder]]()
* [[SPISetClockDivider | setClockDivider]]()
* [[SPISetDataMode | setDataMode]]()
* [[SPITransfer | transfer]]()

Changed lines 28-48 from:
(:table border=1 cellpadding=0 cellspacing=0:)
(:cellnr:)
'''Mode'''
(:cell:)
'''Clock Polarity (CPOL)'''
(:cell:)
'''Clock Phase (CPHA)'''
(:cellnr:)0
(:cell:)0
(:cell:)0
(:cellnr:)1
(:cell:)0
(:cell:)1
(:cellnr:)2
(:cell:)1
(:cell:)0
(:cellnr:)3
(:cell:)1
(:cell:)1
(:tableend:)
to:
|| border = 1
||!Mode ||!Clock Polarity (CPOL) ||!Clock Phase (CPHA)''' ||
||0 ||0 ||0 ||
||1 ||0 ||1 ||
||2 ||1 ||0 ||
||3 ||1 ||1 ||
Changed lines 44-56 from:
to:
(:cell width=10%:)

(:cell width=35%:)

!!!!Functions

* [[SPIBegin | begin]]()
* [[SPIEnd | end]]()
* [[SPISetBitOrder | setBitOrder]]()
* [[SPISetClockDivider | setClockDivider]]()
* [[SPISetDataMode | setDataMode]]()
* [[SPITransfer | transfer]]()
Deleted line 61:
Added lines 64-66:

(:tableend:)
August 15, 2010, at 03:45 PM by Tom Igoe -
Added lines 1-73:
!!SPI library


This library allows you to communicate with SPI devices, with the Arduino as the master device.

!!!!Functions

* [[SPIBegin | begin]]()
* [[SPIEnd | end]]()
* [[SPISetBitOrder | setBitOrder]]()
* [[SPISetClockDivider | setClockDivider]]()
* [[SPISetDataMode | setDataMode]]()
* [[SPITransfer | transfer]]()


!!!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,
* Master In Slave Out (MISO) - The Slave line for sending data to the master,
* Master Out Slave In (MOSI) - The Master line for sending data to the peripherals,
* Serial Clock (SCK) - The clock pulses which synchronize data transmission generated by the master, and
* Slave Select pin - 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? This and the clock idling 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 three 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 three modes combine polarity and phase. The @@SPI.setDataMode()@@ function lets you set the mode to control clock polarity and phase according to this table:


(:table border=1 cellpadding=0 cellspacing=0:)
(:cellnr:)
'''Mode'''
(:cell:)
'''Clock Polarity (CPOL)'''
(:cell:)
'''Clock Phase (CPHA)'''
(:cellnr:)0
(:cell:)0
(:cell:)0
(:cellnr:)1
(:cell:)0
(:cell:)1
(:cellnr:)2
(:cell:)1
(:cell:)0
(:cellnr:)3
(:cell:)1
(:cell:)1
(:tableend:)

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 [[http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_Numbers|Wikipedia's page on SPI]].

!!! Connections
On the Arduino Duemilanove and other ATmega168 / 328-based boards, the SPI bus uses pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative.

It is possible to use a pin other than pin 10 as the slave select (SS) pin. 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.


!!!!Examples

* [[Tutorial/BarometricPressureSensor]]: Read air pressure and temperature from a sensor using SPI
* [[Tutorial/SPIDigitalPot]]: Control a digital potentiometer using SPI


!!!!See also
* [[ShiftOut | shiftOut]]()

Share