Interfacing with a Secure Digital (SD) card

From Mech
Revision as of 15:28, 11 February 2009 by Colleen Fryer (talk | contribs)
Jump to navigationJump to search

Original Assignment

Establish SPI communication between your PIC and a Secure Digital (SD) card for data logging. Demonstrate the ability to store data on the card and to read it back later.

Overview

Secure Digital Cards, or SD cards, are used to hold information in many common electronic devices from digital cameras to mobile phones and come in sizes as small as 16-32 MB and as large as 8 GB. In this lab, we will establish communication between a Microchip PIC 18F4520 and a 2GB SD card manufactured by Apacer.

SD cards can operate three different communication modes: One-bit SD mode, Four-bit SD mode, and SPI mode. SPI is a more basic protocol and it is widely supported by many microcontrollers, including the PIC 18F4520. We'll be using SPI mode in this lab.

Circuit

An SD card has 9 pins. Only 7 of these pins are used to communicate with an SD card in SPI mode. SD cards require between 2 and 3.6 VDC. In this lab, we use a bench top power supply to provide 3.3 VDC to both the PIC and to the SD card. 50k pull-up resistors are essential, even for the pins that are not being used for SPI communications. Note that a pull-up resistor should not be used on the clock line.

Circuit Diagram

Code

The original files were included in the CCS v 4.081 library.

///////////////////////////////////////////////////////////////////////// //// ex_mmcsd.c //// //// //// //// Similar to ex_extee.c, an example that demonstrates //// //// writing and reading to an MMC/SD card. //// //// //// ///////////////////////////////////////////////////////////////////////// //// (C) Copyright 2007 Custom Computer Services //// //// This source code may only be used by licensed users of the CCS //// //// C compiler. This source code may only be distributed to other //// //// licensed users of the CCS C compiler. No other use, //// //// reproduction or distribution is permitted without written //// //// permission. Derivative programs created using this software //// //// in object code form are not restricted in any way. //// /////////////////////////////////////////////////////////////////////////

//These settings are for the CCS PICEEC development kit which contains //an MMC/SD connector.

  1. include <18f4520.h>
  2. fuses NOWDT, HS, NOPROTECT
  3. fuses HS,NOLVP,NOWDT,NOPROTECT
  4. use delay(clock=40000000)

//#use rs232(baud=19200, xmit=PIN_A0, rcv=PIN_A1) // you can use any pins for software uart...

  1. use rs232(baud=19200, UART1) // hardware uart much better; uses RC6/TX and RC7/RX

// characters tranmitted faster than the pic eats them will cause UART to hang.

  1. include <stdlib.h> // for atoi32

//meda library, a compatable media library is required for FAT.

  1. use fast_io(c)
  2. define MMCSD_PIN_SCL PIN_B0 //o THIS WAS A HUGE PROBLEM, C3 DIDN'T WORK
  3. define MMCSD_PIN_SDI PIN_C4 //i
  4. define MMCSD_PIN_SDO PIN_C5 //o
  5. define MMCSD_PIN_SELECT PIN_C2 //o
  6. include <mmcsd.c>
  1. include <input.c>

void main(void) {

 BYTE value, cmd;
 int32 address;
 printf("\r\n\nex_mmcsd.c\r\n\n");


 printf("\r\n\n");
 if (mmcsd_init())
 {
    printf("Could not init the MMC/SD!!!!");
    while(TRUE);
 }
 mmcsd_print_cid();
 do {
    do {
       printf("\r\nRead or Write: ");
       cmd=getc();
       cmd=toupper(cmd);
       putc(cmd);
    } while ( (cmd!='R') && (cmd!='W') );
    printf("\n\rLocation: ");
    address = gethex();
    address = (address<<8)+gethex();
    if(cmd=='R')
    {
       mmcsd_read_byte(address, &value);
       printf("\r\nValue: %X\r\n", value);
    }
    if(cmd=='W') {
       printf("\r\nNew value: ");
       value = gethex();
       printf("\n\r");
       mmcsd_write_byte(address, value);
       mmcsd_flush_buffer();
    }
 } while (TRUE);

}