Discussion in "Project Help" started by    prashants    Aug 4, 2007.
Sat Aug 04 2007, 10:45 AM
#1
hi ,
i want to interface SD card to ATmega 16/128 ,so i want help for how to interface the card & how i write the data to it , any file system is required for that card.
Sat Aug 04 2007, 11:17 AM
#2
you can use the AVRlib it got APIs to write and read in SD/MMC also IDE. you can download AVRlib from here...

AVRlib 2007-03-31

Documentation can be found here.. : Documentation


[ Edited Sat Aug 04 2007, 11:21 AM ]
Tags AVRlibC-Language Function Library for Atmel AVR ProcessoAVR Library
Thu Nov 01 2007, 12:02 AM
#3
Hi, do you have similar files but instead for the 8051?
Thu Nov 01 2007, 03:26 AM
#4
no.. because only the recent S series of 8051 family (AT89S8252, AT89S8253) by atmel has SPI interface otherwise it was very rare in 8051 to find SPI on chip. So maybe in future someone will make it..

Here is a sample code for AVR actually.. but i will give a little info on how can u change it to 8051

/*************************************************************
 * MMC Init function
 *
 * - flushes card receive buffer
 * - selects card
 * - sends the reset command
 * - sends the initialization command, waits for card ready
 *************************************************************/
int MMCInit(void)
{
unsigned int i;
unsigned char Byte;

SPIDDR = SCLK + MOSI + CS + MMCPOWER;
SPIPORT = 0x00;
Delay_1ms(500);
SPIPORT |= MMCPOWER;
SPIPORT |= CS;
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR1) | (1<<SPR0);    /* enable SPI as master, set clk divider */
Delay_1ms(250);

/* start off with 80 bits of high data with card deselected */
for(i=0;i<10;i++)
SpiByte(0xff);
SPIPORT &= ~CS;        /* select card */

/* now send CMD0 - go to idle state */
MMCCommand(0,0,0);
if (MMCGet() != 1)
   {
   SPIPORT |= CS;
   return -1;  // MMC Not detected
   }

/* send CMD1 until we get a 0 back, indicating card is done initializing */
i = 0xffff;
while ((SpiByte(0xff) != 0) && (--i))
     {
     MMCCommand(1,0,0);
     WDR();
     }
if (i == 0)
   {
   SPIPORT |= CS;
   return -2;  // Init Fail
   }

SPIPORT |= CS;
return 0;
}

/************************************************************
 * void MMCInfo(void)
 *
 * - gets and prints formatted CID and CSD info from card
 ************************************************************/
void MMCInfo(void)
{
int i;

MMCCommand(10,0,0);
if (MMCDataToken() != 0xfe) TxString("MMC: error during CID read\n\r\0");
else TxString("MMC: CID read\n\r\0");

// Skip 3 byte Manufacturer ID
SpiByte(0xff);
SpiByte(0xff);
SpiByte(0xff);

TxString("MMC: Product Name : ");
for (i=0;i<7;i++) TxChar(SpiByte(0xff));
TxString("\n\r\0");

for (i=0;i<9;i++) SpiByte(0xff); // Read 9 left byte

SPIPORT |= CS;
}

/************************************************************
 * int MMCReadSector(unsigned long lba, unsigned char * s)
 *
 * - reads a sector from the card (512 bytes)
 * - takes sector # as param
 ************************************************************/
int MMCReadSector(unsigned long lba, char *s)
{
unsigned int i;

MMCCommand(17,(lba>
>
7) & 0xffff, (lba<<9) & 0xffff);
if (MMCDataToken() != 0xfe)
   {
   SEI();
   return -1;
   }

for (i=0;i<512;i++)     /* read the sector */
   {
    *s++ = SpiByte(0xff);
   }
SpiByte(0xff);          /* checksum ->
 don't care about it for now */
SpiByte(0xff);       /* checksum ->
 don't care about it for now */
SPIPORT |= CS;
return 0;
}

/************************************************************
 * int MMCWriteSector(unsigned long lba, unsigned char * s)
 *
 * - reads a sector from the card (512 bytes)
 * - takes sector # as param
 ************************************************************/
int MMCWriteSector(unsigned long lba, char *s)
{
unsigned int i;

MMCCommand(24, (lba>
>
7)& 0xffff, (lba<<9)& 0xffff);
if (MMCGet() == 0xff) return -1;

SpiByte(0xfe);  // Send Start Byte

for (i=0;i<512;i++)       /* read the sector */
   {
    SpiByte(*s++);
   }
SpiByte(0xff);          /* checksum ->
 don't care about it for now */
SpiByte(0xff);       /* checksum ->
 don't care about it for now */
SpiByte(0xff);       /* Read "data response byte"                 */

i = 0xffff;
while ((SpiByte(0xff) == 0x00) && (--i)); /* wait for write finish */
if (i == 0) return -1; // Error

SPIPORT |= CS;
return 0;
}

/************************************************************
 * unsigned char MMCGet(void)
 *
 * - pings the card until it gets a non-0xff value
 * - returns one byte of read info
 ************************************************************/
unsigned char MMCGet(void)
{
unsigned int i = 0xffff;
unsigned char Byte = 0xff;

while((Byte == 0xff) && (--i)) Byte = SpiByte(0xff);
return Byte;
}

/************************************************************
 * int MMCDataToken(void)
 *
 * - pings the card until it gets data token
 * - returns one byte of read info (data token)
 ************************************************************/
unsigned char MMCDataToken(void)
{
unsigned int i = 0xffff;
unsigned char Byte = 0xff;

while((Byte != 0xfe) && (--i)) Byte = SpiByte(0xff);
return Byte;
}

/************************************************************
 * void MMCCommand(unsigned char command, unsigned int px, unsigned int py)
 *
 * - send one byte of 0xff, then issue command + params + (fake) crc
 * - eat up the one command of nothing after the CRC
 ************************************************************/
void MMCCommand(unsigned char command, unsigned int px, unsigned int py)
{
SPIPORT &= ~CS;
SpiByte(0xff);
SpiByte(command | 0x40);
SpiByte((unsigned char)((px >
>
 8)&0x0ff)); /* high byte of param y */
SpiByte((unsigned char)(px & 0x00ff));     /* low byte of param y */
SpiByte((unsigned char)((py >
>
 8)&0x0ff)); /* high byte of param x */
SpiByte((unsigned char)(py & 0x00ff));     /* low byte of param x */
SpiByte(0x95);            /* correct CRC for first command in SPI          */
                          /* after that CRC is ignored, so no problem with */
                          /* always sending 0x95                           */
SpiByte(0xff);
}
/*****************************************************
 * Main SPI routine
 *  - transmits a byte and receives a byte simultaneously
 *  - received byte is returned
 *  - if you only want to read a byte, put a dummy 
 *    (say 0xff) in the transmit slot
 ****************************************************/
unsigned char SpiByte(unsigned char byte)
{
WDR();
SPDR = byte;               /* put byte to send in SPDR, which initiates xmit  */
while(!(SPSR & (1<<SPIF)));/* wait for completion */
return SPDR;               /* return with byte shifted in from slave */
}


If you can write the SpiByte routine for 8051 then you can use the above code to read and write to MMC. further there might be many more changes needed.. that you need to find out.

For more reference take a look at these documents
SD Memory Card Physical Layer Specification, version 1.01 (pdf)
SanDisk SD Card Product Manual v1.9 (pdf)
SanDisk SD Card Product Manual v2.2 (pdf)
Simplified SDIO Card Specification, version 1 (pdf)

also...
How programm SD Card :
http://hdf.ncsa.uiuc.edu/training/HDFtraining/tutorial/sd/sds.html
Link about socket to SD Card :
http://www.sparkfun.com/shop/index.php?shop=1&itemid=330

Project soft and schematic of connecting PIC to SD Card.
http://www.captain.at/electronics/pic-mmc/

MSP430 interfacing to SD card doc from Ti :
http://focus.ti.com/general/docs/lit/getliterature.tsp?literatureNumber=slaa281&fileType=pdf
Fri Aug 01 2008, 03:33 AM
#5
what does MMCCommand(1,0,0) mean?? :blush
Fri Aug 01 2008, 11:07 AM
#6
i will upload Hitachi specifications for MMC and SD soon in download section. Do check it back soon. you will come to know everything.
Sat Aug 09 2008, 02:28 PM
#7
thanks sir
Sat Aug 09 2008, 02:45 PM
#8
i have just checked the download section. May i know in which section you upload the file? Thanks
Mon Aug 11 2008, 02:10 AM
#9
please check the download section
Mon Aug 11 2008, 04:19 AM
#10
oh yea, i want to ask. Is MMC same to SD??

Get Social

Information

Powered by e107 Forum System

Conversation

scena
Wed May 20 2020, 03:59 AM
Does anyone have microcontroller programming software for STC15F104W (core 8051, 4 k, 128 byte, 6 I/O pin, UART ... DIL8), that works?
Sun May 03 2020, 01:08 AM
Tamu Gurung
Hi I am looking for a assembly language program for a digital decimal counter. Time counts max limit of 2 mins starting from 2.00 mins and ending at 0.00. time is to be displayed on 4 7-segment LED displays. Edsim51 simulator to be used. Thanks
Wed Mar 25 2020, 07:09 PM
Tajammul
Sir kindly send the code file i2c for 8051
ExperimenterUK
Sat Mar 14 2020, 04:06 AM
@ Rohitnani,ABEBE..which project ?
Fri Mar 13 2020, 06:25 PM
ABEBE
how can get hadwere of this project
Rohitnani
Sat Mar 07 2020, 09:41 PM
Sir, Could you please send me the zip file input password....
Wed Feb 26 2020, 09:28 PM
Peter Parker
Is it possible to send multiple messages using only 1 GSM module?
Sat Feb 01 2020, 02:42 PM
Pearl Olin
Hello,
Wed Jan 22 2020, 10:46 PM
MINU CHAUDHARY
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.
Tue Jan 14 2020, 11:38 AM
shubham kharade
when i am trying to simulate the program in Proteus 8 professional, I am getting some errors as cannot find module file dhtxx.md can u help me

Downloads

Comments

Davidthils
Thu May 21 2020, 08:44 AM
Jakeror
Wed May 20 2020, 09:28 PM
motorCar
Wed May 20 2020, 03:05 PM
Vordrync
Wed May 20 2020, 07:52 AM
JadenjiT
Wed May 20 2020, 03:15 AM
scena
Tue May 19 2020, 06:08 PM
CodySiz
Tue May 19 2020, 02:22 PM
ClaireSat
Mon May 18 2020, 09:50 PM

Online

Guests: 45, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 01:37 AM

Members: 38211
Newest member: Davidthils
Forum Activity
xrxx Avatar
Posted By xrxx
Thu Apr 30 2020, 06:59 AM
BCD MULTIPLICATIONAssume that you have to 2 BCD nu...
liqian Avatar
Posted By liqian
Tue Apr 28 2020, 05:26 AM
I happened to see your post and I got many useful ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Apr 28 2020, 04:21 AM
The code is hard to read.Please zip it and attach ...
rogermado Avatar
Posted By rogermado
Sun Apr 26 2020, 01:53 AM
@ExperimenterUK, thanks for your reply. You'll fin...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Apr 25 2020, 05:42 PM
Some simulators handle displays better than others...
rogermado Avatar
Posted By rogermado
Sat Apr 25 2020, 03:59 PM
Hello everyone, sorry in advance if i'm not in the...
PHANTOM28 Avatar
Posted By PHANTOM28
Fri Feb 28 2020, 10:25 AM
Hi.I hope you all can help me. I am relatively ne...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Feb 21 2020, 05:05 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Feb 21 2020, 04:45 AM
Yes you can do that.1. Create a channel @Thingspea...
ananyagupta Avatar
Posted By ananyagupta
Thu Feb 20 2020, 03:58 AM
With regards to the particular characters sent to ...