view history edit print login register

Spi Library

This library provides functions for transferring information using the Serial Peripheral Interface (SPI). The SPI interface is automatically initialized when the Spi library is included in a sketch. It sets the following digital I/O pins:

pin 13	SCK	SPI clock	
pin 12	MISO	SPI master in, slave out
pin 11	MOSI	SPI master out, slave in
pin 10	SS	SPI slave select

The default SPI configuation is as follows:

SPI Master enabled
MSB of the data byte transmitted first
SPI mode 0 (CPOL = 0, CPHA = 0)
SPI clock frequency = system clock / 4

Download

The Spi download contains the Spi library.

Functions

mode(byte config)
Sets the SPI configuration register. Only required if the default configuration described above must be modified. The SPE (SPI enabled) and MSTR (SPI master) bits are always set. If there are multiple SPI devices on the bus which require different SPI configurations, this function can be called before accessing each different device type to set the appropriate configuration.
Example:
Spi.mode((1<<CPOL) | (1 << CPHA)); // set SPI mode 3
or
Spi.mode((<<SPR0));                // set SPI clock to system clock / 16

byte transfer(byte b)
Sends and receives a byte from the SPI bus.
Example:
n = Spi.transfer(0x2A);            // sends the byte 0x2A
	                           // and returns the byte received

byte transfer(byte b, byte delay)
Delays for a number of microseconds, then sends and receives a byte from the SPI bus. This function is used if there are timing considerations associated with the data transfer.
Example:
n = Spi.transfer(0x2A, 2);         // waits 2 usec, then sends the byte 0x2A
                                   // and returns the byte received