spi_readwrite.ino 932 B

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