Advertisement
AtomSoft

Atom32 MicroSD CardInfo

Aug 24th, 2014
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. /*
  2.   SD card test
  3.  
  4.  This example shows how use the utility libraries on which the'
  5.  SD library is based in order to get info about your SD card.
  6.  Very useful for testing a card when you're not sure whether its working or not.
  7.    
  8.  The circuit:
  9.  * SD card attached to SPI bus as follows:
  10.  ** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
  11.  ** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
  12.  ** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
  13.  ** CS - depends on your SD card shield or module
  14.  
  15.  
  16.  created  28 Mar 2011
  17.  by Limor Fried
  18.  
  19.  - Cleaned up all SD examples included with MPIDE for consistency in defining CS pins
  20.  revised  24 May 2013 by Jacob Christ
  21.  */
  22. // include the SD library:
  23. #include <SD.h>
  24.  
  25. // set up variables using the SD utility library functions:
  26. Sd2Card card;
  27. SdVolume volume;
  28. SdFile root;
  29.  
  30. // change this to match your SD shield or module;
  31. // Arduino Ethernet shield: pin 4
  32. // Adafruit SD shields and modules: pin 10
  33. // Sparkfun SD shield: pin 8
  34. // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  35. // Note that even if it's not used as the CS pin, the hardware SS pin
  36. // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  37. // or the SD library functions will not work.
  38.  
  39. // Default SD chip select for Uno and Mega type devices
  40. const int chipSelect_SD_default = 10; // Change 10 to 53 for a Mega
  41.  
  42. // chipSelect_SD can be changed if you do not use default CS pin
  43. const int chipSelect_SD = 4; //ATOM32 CS Line
  44. const int SW2 = 5;
  45. int isReady = 0;
  46.  
  47.  
  48. void setup()
  49. {
  50.   Serial.begin(9600);
  51. }
  52.  
  53. void loadCard()
  54. {
  55.   Serial.print("\nInitializing SD card...");
  56.  
  57.   // Make sure the default chip select pin is set to so that
  58.   // shields that have a device that use the default CS pin
  59.   // that are connected to the SPI bus do not hold drive bus
  60.   pinMode(chipSelect_SD_default, OUTPUT);
  61.  
  62.   digitalWrite(chipSelect_SD_default, HIGH);
  63.  
  64.   pinMode(chipSelect_SD, OUTPUT);
  65.   digitalWrite(chipSelect_SD, HIGH);
  66.  
  67.   // we'll use the initialization code from the utility libraries
  68.   // since we're just testing if the card is working!
  69.   if (!card.init(SPI_HALF_SPEED, chipSelect_SD)) {
  70.     Serial.println("initialization failed. Things to check:");
  71.     Serial.println("* is a card is inserted?");
  72.     Serial.println("* Is your wiring correct?");
  73.     Serial.println("* did you change the chipSelect pin to match your shield or module?");
  74.     return;
  75.   }
  76.   else {
  77.     Serial.println("Wiring is correct and a card is present.");
  78.   }
  79.  
  80.   // print the type of card
  81.   Serial.print("\nCard type: ");
  82.   switch(card.type()) {
  83.   case SD_CARD_TYPE_SD1:
  84.     Serial.println("SD1");
  85.     break;
  86.   case SD_CARD_TYPE_SD2:
  87.     Serial.println("SD2");
  88.     break;
  89.   case SD_CARD_TYPE_SDHC:
  90.     Serial.println("SDHC");
  91.     break;
  92.   default:
  93.     Serial.println("Unknown");
  94.   }
  95.  
  96.   // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
  97.   if (!volume.init(card)) {
  98.     Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
  99.     return;
  100.   }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.   // print the type and size of the first FAT-type volume
  108.   long volumesize;
  109.   Serial.print("\nVolume type is FAT");
  110.   Serial.println(volume.fatType(), DEC);
  111.   Serial.println();
  112.  
  113.   volumesize = volume.blocksPerCluster
  114.   ();    // clusters are collections of blocks
  115.   volumesize *= volume.clusterCount();       // we'll have a lot of clusters
  116.   volumesize *= 512;                            // SD card blocks are always 512 bytes
  117.   Serial.print("Volume size (bytes): ");
  118.   Serial.println(volumesize);
  119.   Serial.print("Volume size (Kbytes): ");
  120.   volumesize /= 1024;
  121.   Serial.println(volumesize);
  122.   Serial.print("Volume size (Mbytes): ");
  123.   volumesize /= 1024;
  124.   Serial.println(volumesize);
  125.  
  126.  
  127.   Serial.println("\nFiles found on the card (name, date and size in bytes): ");
  128.   root.openRoot(volume);
  129.  
  130.  
  131.  
  132.  
  133.   // list all files in the card with date and size
  134.   root.ls(LS_R | LS_DATE | LS_SIZE);
  135. }
  136. void loop(void) {
  137.   isReady = 0;
  138.   pinMode(SW2, INPUT);
  139.   while(isReady == 0)
  140.   {
  141.     if(digitalRead(SW2) == 0)
  142.       isReady = 1;
  143.   }
  144.  
  145.   loadCard();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement