Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SD / examples / CardInfo / CardInfo.ino @ 58d82c77

History | View | Annotate | Download (3.33 KB)

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
 		Pin 4 used here for consistency with other Arduino examples
15

    
16
 
17
 created  28 Mar 2011
18
 by Limor Fried 
19
 modified 16 Mar 2011
20
 by Tom Igoe
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
const int chipSelect = 4;    
35

    
36
void setup()
37
{
38
  Serial.begin(9600);
39
  Serial.print("\nInitializing SD card...");
40
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
41
  // Note that even if it's not used as the CS pin, the hardware SS pin 
42
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output 
43
  // or the SD library functions will not work. 
44
  pinMode(10, OUTPUT);     // change this to 53 on a mega
45

    
46

    
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

    
59
  // print the type of card
60
  Serial.print("\nCard type: ");
61
  switch(card.type()) {
62
    case SD_CARD_TYPE_SD1:
63
      Serial.println("SD1");
64
      break;
65
    case SD_CARD_TYPE_SD2:
66
      Serial.println("SD2");
67
      break;
68
    case SD_CARD_TYPE_SDHC:
69
      Serial.println("SDHC");
70
      break;
71
    default:
72
      Serial.println("Unknown");
73
  }
74

    
75
  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
76
  if (!volume.init(card)) {
77
    Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
78
    return;
79
  }
80

    
81

    
82
  // print the type and size of the first FAT-type volume
83
  uint32_t volumesize;
84
  Serial.print("\nVolume type is FAT");
85
  Serial.println(volume.fatType(), DEC);
86
  Serial.println();
87
  
88
  volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
89
  volumesize *= volume.clusterCount();       // we'll have a lot of clusters
90
  volumesize *= 512;                            // SD card blocks are always 512 bytes
91
  Serial.print("Volume size (bytes): ");
92
  Serial.println(volumesize);
93
  Serial.print("Volume size (Kbytes): ");
94
  volumesize /= 1024;
95
  Serial.println(volumesize);
96
  Serial.print("Volume size (Mbytes): ");
97
  volumesize /= 1024;
98
  Serial.println(volumesize);
99

    
100
  
101
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
102
  root.openRoot(volume);
103
  
104
  // list all files in the card with date and size
105
  root.ls(LS_R | LS_DATE | LS_SIZE);
106
}
107

    
108

    
109
void loop(void) {
110
  
111
}