Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- const int CS = 9;
- void setup() {
- Serial.begin(9600);
- while (!Serial);
- pinMode(CS, OUTPUT);
- digitalWrite(CS, HIGH);
- SPI.begin();
- SPI.beginTransaction(SPISettings(400000, MSBFIRST, SPI_MODE0));
- // Wake up
- for (int i = 0; i < 10; i++) SPI.transfer(0xFF);
- // Send CMD0
- digitalWrite(CS, LOW);
- SPI.transfer(0x40);
- for (int i = 0; i < 4; i++) SPI.transfer(0);
- SPI.transfer(0x95);
- // Read response
- byte r = 0xFF;
- for (int i = 0; i < 10; i++) {
- r = SPI.transfer(0xFF);
- if (r != 0xFF) break;
- }
- digitalWrite(CS, HIGH);
- SPI.endTransaction();
- Serial.print("CMD0 response = 0x");
- Serial.println(r, HEX);
- if (r == 0xFF) {
- Serial.println("→ No response at all (wiring/power issue).");
- } else if (r == 0x01) {
- Serial.println("→ Heard you (idle state). Good link!");
- } else if (r == 0x00) {
- Serial.println("→ Heard you and ready! Breakout & card OK!");
- } else {
- Serial.println("→ Got unexpected response but card is talking.");
- }
- }
- void loop() {}
Advertisement
Add Comment
Please, Sign In to add comment