Can simulate sd card using arduino with proteus

Discussion in 'Embedded Systems and Microcontrollers' started by kuannygohcheetatt, Oct 4, 2014.

  1. kuannygohcheetatt

    Thread Starter Member

    Oct 31, 2013
    61
    0
    i try to simulate it using the code below

    Code (Text):
    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. created  28 Mar 2011
    17. by Limor Fried
    18. modified 9 Apr 2012
    19. by Tom Igoe
    20. */
    21. // include the SD library:
    22. #include <SD.h>
    23.  
    24. // set up variables using the SD utility library functions:
    25. Sd2Card card;
    26. SdVolume volume;
    27. SdFile root;
    28.  
    29. // change this to match your SD shield or module;
    30. // Arduino Ethernet shield: pin 4
    31. // Adafruit SD shields and modules: pin 10
    32. // Sparkfun SD shield: pin 8
    33. const int chipSelect = 4;
    34.  
    35. void setup()
    36. {
    37. // Open serial communications and wait for port to open:
    38.   Serial.begin(9600);
    39.    while (!Serial) {
    40.     ; // wait for serial port to connect. Needed for Leonardo only
    41.   }
    42.  
    43.  
    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.  
    51.  
    52.   // we'll use the initialization code from the utility libraries
    53.   // since we're just testing if the card is working!
    54.   if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    55.     Serial.println("initialization failed. Things to check:");
    56.     Serial.println("* is a card is inserted?");
    57.     Serial.println("* Is your wiring correct?");
    58.     Serial.println("* did you change the chipSelect pin to match your shield or module?");
    59.     return;
    60.   } else {
    61.    Serial.println("Wiring is correct and a card is present.");
    62.   }
    63.  
    64.   // print the type of card
    65.   Serial.print("\nCard type: ");
    66.   switch(card.type()) {
    67.     case SD_CARD_TYPE_SD1:
    68.       Serial.println("SD1");
    69.       break;
    70.     case SD_CARD_TYPE_SD2:
    71.       Serial.println("SD2");
    72.       break;
    73.     case SD_CARD_TYPE_SDHC:
    74.       Serial.println("SDHC");
    75.       break;
    76.     default:
    77.       Serial.println("Unknown");
    78.   }
    79.  
    80.   // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    81.   if (!volume.init(card)) {
    82.     Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
    83.     return;
    84.   }
    85.  
    86.  
    87.   // print the type and size of the first FAT-type volume
    88.   uint32_t volumesize;
    89.   Serial.print("\nVolume type is FAT");
    90.   Serial.println(volume.fatType(), DEC);
    91.   Serial.println();
    92.  
    93.   volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
    94.   volumesize *= volume.clusterCount();       // we'll have a lot of clusters
    95.   volumesize *= 512;                            // SD card blocks are always 512 bytes
    96.   Serial.print("Volume size (bytes): ");
    97.   Serial.println(volumesize);
    98.   Serial.print("Volume size (Kbytes): ");
    99.   volumesize /= 1024;
    100.   Serial.println(volumesize);
    101.   Serial.print("Volume size (Mbytes): ");
    102.   volumesize /= 1024;
    103.   Serial.println(volumesize);
    104.  
    105.  
    106.   Serial.println("\nFiles found on the card (name, date and size in bytes): ");
    107.   root.openRoot(volume);
    108.  
    109.   // list all files in the card with date and size
    110.   root.ls(LS_R | LS_DATE | LS_SIZE);
    111. }
    112.  
    113.  
    114. void loop(void) {
    115.  
    116. }
    However the result turn out to be i cant even pass the first phase which is initializing the sd card. What is the error?

    The schematic is below
    upload_2014-10-4_21-9-23.png
     
Loading...