Guest User

Arduino

a guest
Jul 21st, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <SPI.h>
  2.  
  3. const int CS = 9;
  4.  
  5. void setup() {
  6.   Serial.begin(9600);
  7.   while (!Serial);
  8.  
  9.   pinMode(CS, OUTPUT);
  10.   digitalWrite(CS, HIGH);
  11.  
  12.   SPI.begin();
  13.   SPI.beginTransaction(SPISettings(400000, MSBFIRST, SPI_MODE0));
  14.  
  15.   // Wake up
  16.   for (int i = 0; i < 10; i++) SPI.transfer(0xFF);
  17.  
  18.   // Send CMD0
  19.   digitalWrite(CS, LOW);
  20.   SPI.transfer(0x40);  
  21.   for (int i = 0; i < 4; i++) SPI.transfer(0);
  22.   SPI.transfer(0x95);
  23.  
  24.   // Read response
  25.   byte r = 0xFF;
  26.   for (int i = 0; i < 10; i++) {
  27.     r = SPI.transfer(0xFF);
  28.     if (r != 0xFF) break;
  29.   }
  30.   digitalWrite(CS, HIGH);
  31.   SPI.endTransaction();
  32.  
  33.   Serial.print("CMD0 response = 0x");
  34.   Serial.println(r, HEX);
  35.  
  36.   if (r == 0xFF) {
  37.     Serial.println("→ No response at all (wiring/power issue).");
  38.   } else if (r == 0x01) {
  39.     Serial.println("→ Heard you (idle state). Good link!");
  40.   } else if (r == 0x00) {
  41.     Serial.println("→ Heard you and ready! Breakout & card OK!");
  42.   } else {
  43.     Serial.println("→ Got unexpected response but card is talking.");
  44.   }
  45. }
  46.  
  47. void loop() {}
  48.  
Advertisement
Add Comment
Please, Sign In to add comment