Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SdCardTest.ino 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. SD card test
  3. This example shows how use the utility libraries on which the'
  4. SD library is based in order to get info about your SD card.
  5. Very useful for testing a card when you're not sure whether its working or not.
  6. The circuit:
  7. * SD card attached to SPI bus as follows:
  8. ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
  9. ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
  10. ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
  11. ** CS - depends on your SD card shield or module.
  12. Pin 4 used here for consistency with other Arduino examples
  13. created 28 Mar 2011
  14. by Limor Fried
  15. modified 9 Apr 2012
  16. by Tom Igoe
  17. */
  18. // include the SD library:
  19. #include <SD.h>
  20. #include <SPI.h>
  21. // set up variables using the SD utility library functions:
  22. Sd2Card card;
  23. SdVolume volume;
  24. SdFile root;
  25. // change this to match your SD shield or module;
  26. // Arduino Ethernet shield: pin 4
  27. // Adafruit SD shields and modules: pin 10
  28. // Sparkfun SD shield: pin 8
  29. // Teensy 2.0: pin 0
  30. // Teensy++ 2.0: pin 20
  31. // Teensy 3.0: pin 10
  32. // Audio Shield for Teensy 3.0: pin 10
  33. const int chipSelect = 10;
  34. void setup()
  35. {
  36. SPI.setMOSI(7); // Audio shield has MOSI on pin 7
  37. SPI.setSCK(14); // Audio shield has SCK on pin 14
  38. // Open serial communications and wait for port to open:
  39. Serial.begin(9600);
  40. while (!Serial) {
  41. ; // wait for serial port to connect. Needed for Leonardo only
  42. }
  43. delay(250);
  44. Serial.print("\nInitializing SD card...");
  45. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  46. // Note that even if it's not used as the CS pin, the hardware SS pin
  47. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  48. // or the SD library functions will not work.
  49. pinMode(10, OUTPUT); // change this to 53 on a mega
  50. // we'll use the initialization code from the utility libraries
  51. // since we're just testing if the card is working!
  52. if (!card.init(SPI_HALF_SPEED, chipSelect)) {
  53. Serial.println("initialization failed. Things to check:");
  54. Serial.println("* is a card is inserted?");
  55. Serial.println("* Is your wiring correct?");
  56. Serial.println("* did you change the chipSelect pin to match your shield or module?");
  57. return;
  58. } else {
  59. Serial.println("Wiring is correct and a card is present.");
  60. }
  61. // print the type of card
  62. Serial.print("\nCard type: ");
  63. switch(card.type()) {
  64. case SD_CARD_TYPE_SD1:
  65. Serial.println("SD1");
  66. break;
  67. case SD_CARD_TYPE_SD2:
  68. Serial.println("SD2");
  69. break;
  70. case SD_CARD_TYPE_SDHC:
  71. Serial.println("SDHC");
  72. break;
  73. default:
  74. Serial.println("Unknown");
  75. }
  76. // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  77. if (!volume.init(card)) {
  78. Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  79. return;
  80. }
  81. // print the type and size of the first FAT-type volume
  82. uint32_t volumesize;
  83. Serial.print("\nVolume type is FAT");
  84. Serial.println(volume.fatType(), DEC);
  85. Serial.println();
  86. volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
  87. volumesize *= volume.clusterCount(); // we'll have a lot of clusters
  88. volumesize *= 512; // SD card blocks are always 512 bytes
  89. Serial.print("Volume size (bytes): ");
  90. Serial.println(volumesize);
  91. Serial.print("Volume size (Kbytes): ");
  92. volumesize /= 1024;
  93. Serial.println(volumesize);
  94. Serial.print("Volume size (Mbytes): ");
  95. volumesize /= 1024;
  96. Serial.println(volumesize);
  97. Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  98. root.openRoot(volume);
  99. // list all files in the card with date and size
  100. root.ls(LS_R | LS_DATE | LS_SIZE);
  101. }
  102. void loop(void) {
  103. }