Introduction
Serial Peripheral Interface (SPI) is a synchronous serial communication protocol used by many devices to communicate with one another. Fast and capable of full duplex communication (meaning that both devices can send and receive data at the same time), it is commonly used in cases requiring large amounts of data to be transferred, continuously at high speeds. Devices that use the SPI protocol include displays, flash memory, sensors, real-time clocks, etc.
In this guide, learn about the SPI protocol and SPI library.
Complete this guide to gain a better understanding of the SPI library, SPI modes, SPI transaction and transfer methods.
In this guide, learn about the SPI protocol and SPI library.
Complete this guide to gain a better understanding of the SPI library, SPI modes, SPI transaction and transfer methods.
-
-
-
-
-
-
-
-
-
-
#include <SPI.h>
-
#include <SPI.h> void setup() { // start the SPI library: SPI.begin(); }
-
#include <SPI.h> // using two incompatible SPI devices, A and B. Incompatible means that they need different SPI_MODE const int slaveAPin = 20; const int slaveBPin = 21; // set up the speed, data order and data mode SPISettings settingsA(1000000, MSBFIRST, SPI_MODE1); SPISettings settingsB(10000000, LSBFIRST, SPI_MODE3); void setup() { // set the Slave Select Pins as outputs: pinMode (slaveAPin, OUTPUT); pinMode (slaveBPin, OUTPUT); // initialize SPI: SPI.begin(); }
-
#include <SPI.h> // using two incompatible SPI devices, A and B. Incompatible means that they need different SPI_MODE const int slaveAPin = 20; const int slaveBPin = 21; // set up the speed, data order and data mode SPISettings settingsA(1000000, MSBFIRST, SPI_MODE1); SPISettings settingsB(10000000, LSBFIRST, SPI_MODE3); void setup() { // set the Slave Select Pins as outputs: pinMode (slaveAPin, OUTPUT); pinMode (slaveBPin, OUTPUT); // initialize SPI: SPI.begin(); } uint8_t stat, val1, val2, result; void loop() { // read three bytes from device A SPI.beginTransaction(settingsA); digitalWrite (slaveAPin, LOW); // reading only, so data sent does not matter stat = SPI.transfer(0); val1 = SPI.transfer(0); val2 = SPI.transfer(0); digitalWrite (slaveAPin, HIGH); }
-
#include <SPI.h> // using two incompatible SPI devices, A and B. Incompatible means that they need different SPI_MODE const int slaveAPin = 20; const int slaveBPin = 21; // set up the speed, data order and data mode SPISettings settingsA(1000000, MSBFIRST, SPI_MODE1); SPISettings settingsB(10000000, LSBFIRST, SPI_MODE3); void setup() { // set the Slave Select Pins as outputs: pinMode (slaveAPin, OUTPUT); pinMode (slaveBPin, OUTPUT); // initialize SPI: SPI.begin(); } uint8_t stat, val1, val2, result; void loop() { // read three bytes from device A SPI.beginTransaction(settingsA); digitalWrite (slaveAPin, LOW); // reading only, so data sent does not matter stat = SPI.transfer(0); val1 = SPI.transfer(0); val2 = SPI.transfer(0); digitalWrite (slaveAPin, HIGH); SPI.endTransaction(); // if stat is 1 or 2, send val1 or val2 else zero if (stat == 1) { result = val1; } else if (stat == 2) { result = val2; } else { result = 0; } }
-
#include <SPI.h> // using two incompatible SPI devices, A and B. Incompatible means that they need different SPI_MODE const int slaveAPin = 20; const int slaveBPin = 21; // set up the speed, data order and data mode SPISettings settingsA(1000000, MSBFIRST, SPI_MODE1); SPISettings settingsB(10000000, LSBFIRST, SPI_MODE3); void setup() { // set the Slave Select Pins as outputs: pinMode (slaveAPin, OUTPUT); pinMode (slaveBPin, OUTPUT); // initialize SPI: SPI.begin(); } uint8_t stat, val1, val2, result; void loop() { // read three bytes from device A SPI.beginTransaction(settingsA); digitalWrite (slaveAPin, LOW); // reading only, so data sent does not matter stat = SPI.transfer(0); val1 = SPI.transfer(0); val2 = SPI.transfer(0); digitalWrite (slaveAPin, HIGH); SPI.endTransaction(); // if stat is 1 or 2, send val1 or val2 else zero if (stat == 1) { result = val1; } else if (stat == 2) { result = val2; } else { result = 0; } // send result to device B SPI.beginTransaction(settingsB); digitalWrite (slaveBPin, LOW); SPI.transfer(result); digitalWrite (slaveBPin, HIGH); SPI.endTransaction(); }
-
#include <SPI.h> // using two incompatible SPI devices, A and B. Incompatible means that they need different SPI_MODE const int slaveAPin = 20; const int slaveBPin = 21; // set up the speed, data order and data mode SPISettings settingsA(1000000, MSBFIRST, SPI_MODE1); SPISettings settingsB(10000000, LSBFIRST, SPI_MODE3); void setup() { // set the Slave Select Pins as outputs: pinMode (slaveAPin, OUTPUT); pinMode (slaveBPin, OUTPUT); // initialize SPI: SPI.begin(); } uint8_t stat, val1, val2, result; void loop() { // read three bytes from device A SPI.beginTransaction(settingsA); digitalWrite (slaveAPin, LOW); // reading only, so data sent does not matter stat = SPI.transfer(0); val1 = SPI.transfer(0); val2 = SPI.transfer(0); digitalWrite (slaveAPin, HIGH); SPI.endTransaction(); // if stat is 1 or 2, send val1 or val2 else zero if (stat == 1) { result = val1; } else if (stat == 2) { result = val2; } else { result = 0; } // send result to device B SPI.beginTransaction(settingsB); digitalWrite (slaveBPin, LOW); SPI.transfer(result); digitalWrite (slaveBPin, HIGH); SPI.endTransaction(); }