Hardware Reference
In-Depth Information
NOTE The Extended SPI library for the Due is only available on Arduino 1.5 and
greater.
Most SPI devices are compatible, but as you have seen previously, there are
different modes, and sometimes you will have two SPI devices on your system
that use different modes. This can complicate designs greatly, forcing you to
reconi gure the SPI controller each time you change peripherals. The Arduino
Due has a way around this.
The Arduino Due can use pins 4, 10, and 52 as slave select. These pins must
be specii ed on each call, including the setup with SPI.begin() :
void setup(){
// Initialize the bus for a device on pin 4
SPI.begin(4);
// Initialize the bus for a device on pin 10
SPI.begin(10);
// Initialize the bus for a device on pin 52
SPI.begin(52);
}
begin() is written in a different way:
SPI.begin(slaveSelectPin);
It takes one parameter, the slave select pin, to use. So why is this required?
This becomes obvious when coni guring the SPI bus:
// Set clock divider on pin 4 to 21
SPI.setClockDivider(4, 21);
// Set clock divider on pin 10 to 42
SPI.setClockDivider(10, 42);
// Set clock divider on pin 52 to 84
SPI.setClockDivider(52, 84);
Each SS pin can have its own clock frequency, and the Arduino automatically
changes the clock frequency when talking to a particular slave. This also applies
to any coni guration made:
// Set mode on pin 4 to MODE0
SPI.setDataMode(4, SPI_MODE0);
// Set mode on pin 10 to MODE2
SPI.setDataMode(10, SPI_MODE2);
The SPI system now automatically changes modes when talking to a particular
slave. To initiate communications, use transfer() , specifying the pin:
result = SPI.transfer(slaveSelectPin, val);
result = SPI.transfer(slaveSelectPin, val, transferMode);
Search WWH ::




Custom Search