Difference between revisions of "Interfacing with a Secure Digital (SD) card"
Line 41: | Line 41: | ||
///////////////////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////////////////// |
||
//These settings are for the CCS PICEEC development kit which contains |
//These settings are for the CCS PICEEC development kit which contains |
||
//an MMC/SD connector. |
//an MMC/SD connector. |
||
#include <18f4520.h> |
#include <18f4520.h> |
||
#fuses NOWDT, HS, NOPROTECT |
#fuses NOWDT, HS, NOPROTECT |
||
#fuses HS,NOLVP,NOWDT,NOPROTECT |
#fuses HS,NOLVP,NOWDT,NOPROTECT |
||
#use delay(clock=40000000) |
#use delay(clock=40000000) |
||
#use rs232(baud=19200, UART1) // hardware uart much better; uses RC6/TX and RC7/RX |
|||
⚫ | |||
#use rs232(baud=19200, UART1) // hardware uart much better; uses RC6/TX and RC7/RX |
|||
⚫ | |||
#include <stdlib.h> // for atoi32 |
#include <stdlib.h> // for atoi32 |
||
// |
//media library, a compatable media library is required for FAT. |
||
#use fast_io(c) |
#use fast_io(c) |
||
#define MMCSD_PIN_SCL PIN_B0 //o THIS WAS A HUGE PROBLEM, C3 DIDN'T WORK |
#define MMCSD_PIN_SCL PIN_B0 //o THIS WAS A HUGE PROBLEM, C3 DIDN'T WORK |
||
#define MMCSD_PIN_SDI PIN_C4 //i |
#define MMCSD_PIN_SDI PIN_C4 //i |
||
#define MMCSD_PIN_SDO PIN_C5 //o |
#define MMCSD_PIN_SDO PIN_C5 //o |
||
#define MMCSD_PIN_SELECT PIN_C2 //o |
#define MMCSD_PIN_SELECT PIN_C2 //o |
||
#include <mmcsd.c> |
#include <mmcsd.c> |
||
#include <input.c> |
#include <input.c> |
||
void main(void) |
void main(void) |
||
{ |
{ |
||
BYTE value, cmd; |
BYTE value, cmd; |
||
int32 address; |
int32 address; |
||
printf("\r\n\nex_mmcsd.c\r\n\n"); |
printf("\r\n\nex_mmcsd.c\r\n\n"); |
||
printf("\r\n\n"); |
printf("\r\n\n"); |
||
Line 79: | Line 74: | ||
while(TRUE); |
while(TRUE); |
||
} |
} |
||
mmcsd_print_cid(); |
mmcsd_print_cid(); |
||
Line 89: | Line 83: | ||
putc(cmd); |
putc(cmd); |
||
} while ( (cmd!='R') && (cmd!='W') ); |
} while ( (cmd!='R') && (cmd!='W') ); |
||
printf("\n\rLocation: "); |
printf("\n\rLocation: "); |
||
address = gethex(); |
address = gethex(); |
||
address = (address<<8)+gethex(); |
address = (address<<8)+gethex(); |
||
if(cmd=='R') |
if(cmd=='R') |
||
{ |
{ |
||
Line 100: | Line 91: | ||
printf("\r\nValue: %X\r\n", value); |
printf("\r\nValue: %X\r\n", value); |
||
} |
} |
||
if(cmd=='W') { |
if(cmd=='W') { |
||
printf("\r\nNew value: "); |
printf("\r\nNew value: "); |
Revision as of 15:48, 11 February 2009
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.
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. #include <18f4520.h> #fuses NOWDT, HS, NOPROTECT #fuses HS,NOLVP,NOWDT,NOPROTECT #use delay(clock=40000000) #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.
#include <stdlib.h> // for atoi32
//media library, a compatable media library is required for FAT. #use fast_io(c) #define MMCSD_PIN_SCL PIN_B0 //o THIS WAS A HUGE PROBLEM, C3 DIDN'T WORK #define MMCSD_PIN_SDI PIN_C4 //i #define MMCSD_PIN_SDO PIN_C5 //o #define MMCSD_PIN_SELECT PIN_C2 //o #include <mmcsd.c>
#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);
}