spi_readwrite.ino (932B)
1 #include <Adafruit_SPIDevice.h> 2 3 #define SPIDEVICE_CS 10 4 Adafruit_SPIDevice spi_dev = Adafruit_SPIDevice(SPIDEVICE_CS); 5 6 7 void setup() { 8 while (!Serial) { delay(10); } 9 Serial.begin(115200); 10 Serial.println("SPI device read and write test"); 11 12 if (!spi_dev.begin()) { 13 Serial.println("Could not initialize SPI device"); 14 while (1); 15 } 16 17 uint8_t buffer[32]; 18 19 // Try to read 32 bytes 20 spi_dev.read(buffer, 32); 21 Serial.print("Read: "); 22 for (uint8_t i=0; i<32; i++) { 23 Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", "); 24 } 25 Serial.println(); 26 27 // read a register by writing first, then reading 28 buffer[0] = 0x8F; // we'll reuse the same buffer 29 spi_dev.write_then_read(buffer, 1, buffer, 2, false); 30 Serial.print("Write then Read: "); 31 for (uint8_t i=0; i<2; i++) { 32 Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", "); 33 } 34 Serial.println(); 35 } 36 37 void loop() { 38 39 }