You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.6KB

  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 audio board: pin 10
  30. // Wiz820+SD board: pin 4
  31. // Teensy 2.0: pin 0
  32. // Teensy++ 2.0: pin 20
  33. const int chipSelect = 4;
  34. void setup()
  35. {
  36. // Open serial communications and wait for port to open:
  37. Serial.begin(9600);
  38. while (!Serial) {
  39. ; // wait for serial port to connect. Needed for Leonardo only
  40. }
  41. Serial.print("\nInitializing SD card...");
  42. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  43. // Note that even if it's not used as the CS pin, the hardware SS pin
  44. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  45. // or the SD library functions will not work.
  46. pinMode(10, OUTPUT); // change this to 53 on a mega
  47. // we'll use the initialization code from the utility libraries
  48. // since we're just testing if the card is working!
  49. if (!card.init(SPI_HALF_SPEED, chipSelect)) {
  50. Serial.println("initialization failed. Things to check:");
  51. Serial.println("* is a card is inserted?");
  52. Serial.println("* Is your wiring correct?");
  53. Serial.println("* did you change the chipSelect pin to match your shield or module?");
  54. return;
  55. } else {
  56. Serial.println("Wiring is correct and a card is present.");
  57. }
  58. // print the type of card
  59. Serial.print("\nCard type: ");
  60. switch(card.type()) {
  61. case SD_CARD_TYPE_SD1:
  62. Serial.println("SD1");
  63. break;
  64. case SD_CARD_TYPE_SD2:
  65. Serial.println("SD2");
  66. break;
  67. case SD_CARD_TYPE_SDHC:
  68. Serial.println("SDHC");
  69. break;
  70. default:
  71. Serial.println("Unknown");
  72. }
  73. // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  74. if (!volume.init(card)) {
  75. Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  76. return;
  77. }
  78. // print the type and size of the first FAT-type volume
  79. uint32_t volumesize;
  80. Serial.print("\nVolume type is FAT");
  81. Serial.println(volume.fatType(), DEC);
  82. Serial.println();
  83. volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
  84. volumesize *= volume.clusterCount(); // we'll have a lot of clusters
  85. volumesize *= 512; // SD card blocks are always 512 bytes
  86. Serial.print("Volume size (bytes): ");
  87. Serial.println(volumesize);
  88. Serial.print("Volume size (Kbytes): ");
  89. volumesize /= 1024;
  90. Serial.println(volumesize);
  91. Serial.print("Volume size (Mbytes): ");
  92. volumesize /= 1024;
  93. Serial.println(volumesize);
  94. Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  95. root.openRoot(volume);
  96. // list all files in the card with date and size
  97. root.ls(LS_R | LS_DATE | LS_SIZE);
  98. }
  99. void loop(void) {
  100. }