Discussion in "Project Help" started by    prashants    Aug 4, 2007.
Sat Aug 04 2007, 02:45 PM
#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, 03:17 PM
#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, 03:21 PM ]
Tags AVRlibC-Language Function Library for Atmel AVR ProcessoAVR Library
Thu Nov 01 2007, 04:02 AM
#3
Hi, do you have similar files but instead for the 8051?
Thu Nov 01 2007, 07: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, 07:33 AM
#5
what does MMCCommand(1,0,0) mean?? :blush
Fri Aug 01 2008, 03:07 PM
#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, 06:28 PM
#7
thanks sir
Sat Aug 09 2008, 06: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, 06:10 AM
#9
please check the download section
Mon Aug 11 2008, 08:19 AM
#10
oh yea, i want to ask. Is MMC same to SD??

Get Social

Information

Powered by e107 Forum System

Conversation

Sun Oct 13 2019, 02:02 AM
Aline Hutchinson
Hey there,
ExperimenterUK
Sat Oct 12 2019, 10:23 PM
Log in.. then post your code in the 8051 forum
Sat Oct 12 2019, 10:18 AM
Bhadra Sajikumar
i am doing a countdown timer with 8051 for my project, unfortunately our professor had to take a leave of absence before he could teach us most of it.. can someone explain the .asm code of it for me?
Mon Sep 23 2019, 07:37 AM
Nathaniel
I am doing a project for my schools wireless security system with cameras,sensors and spot lights using 8051 micro controller, could you help me out with the design and its source code.
Wed Sep 18 2019, 08:48 PM
Caryn Kover
Thank you for your time,
ExperimenterUK
Mon Aug 05 2019, 07:09 PM
@Ismail ..post in forum, give details
Mon Aug 05 2019, 04:33 PM
Ismail balghmi
Hello. Please I won't download
ExperimenterUK
Wed Jul 24 2019, 12:47 AM
@BUSTER123 Please post in the forum
Tue Jul 23 2019, 06:13 PM
BUSTER123
hello! newbie here. i am kind of a guy who hates using ready - made existing libraries, so this helped a lot as i was trying to control and write to a character lcd display. however, i can't figure out how to set the cursor position. for example, i want to set it to the 5th column in the first row, so position 5. so according to the table, i am sending 10000101 on the 8 data lines of the lcd (register select pulled low). the cursor does not move there however. and i cant move the cursor left or right too. is it something wrong that i am doing? thank you.
ExperimenterUK
Thu Jul 11 2019, 07:55 PM
@ramos https://www.theengineeringprojects.com/2017/09/l298-motor-driver-library-proteus.html

Downloads

Comments

ilylily07
Tue Oct 15 2019, 04:32 PM
hientran123
Tue Oct 15 2019, 02:27 AM
Sat Oct 05 2019, 02:38 PM
zexalo
Thu Oct 03 2019, 09:53 AM
thanhle12345
Thu Oct 03 2019, 07:37 AM
ramon_vsc
Fri Sep 20 2019, 04:53 PM
youssoufba
Thu Sep 19 2019, 11:57 AM
Taiphan000
Tue Sep 17 2019, 04:21 AM

Online

Guests: 45, Members: 0 ...

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

Members: 38097
Newest member: ilylily07
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Oct 22 2019, 05:18 AM
wimac Avatar
Posted By wimac
Mon Oct 21 2019, 09:18 AM
I’ve been following this thread and looking at the...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:09 PM
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 03:02 PM
sir, when i on display ,sometime 3rd display is no...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sun Oct 20 2019, 02:53 PM
sir, when i on display ,sometime 3rd display is no...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Oct 20 2019, 12:21 AM
Your code seems okay on my simulationTry my code o...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:13 PM
please find video attachment. 3rd & 4th displa...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Sat Oct 19 2019, 07:05 PM
sir, 3rd and 4th dot matrix display flickering, ...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Oct 18 2019, 09:18 PM
Hopefully you understand some of the program.Which...
s_dinesh40@yahoo.com Avatar
Posted By [email protected]
Thu Oct 17 2019, 09:38 PM