⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmc.c

📁 sd 卡的读写程序范例
💻 C
字号:
/*#######################################################################################
Project:MSP430F1XX MCU Development Board
mmc.C
Copyright (C) 2007 edadiy
http://www.edadiy.cn
#######################################################################################*/
#include "mmc.h"
#include <MSP430X14X.h>
#include "io.h"
//############################################################################
//Routine zur Initialisierung der MMC/SD-Karte (SPI-MODE)
unsigned char mmc_init ()
//############################################################################
{
	unsigned int Timeout = 0;
        P3SEL&=~1;
        P3DIR|=1;
	Init_USART0(0x11);
        UBR0_0 = 2;
	for(unsigned long a=0;a<200;a++){
	asm("NOP");
	};		
	for (unsigned char b = 0;b<10;b++) 
	{
		Write_Byte_MMC(0xff);
	}
	
	unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
	
	Write_Command_MMC (CMD);
        Write_Command_MMC (CMD);
        Write_Command_MMC (CMD);
        Write_Command_MMC (CMD);
          
	while(Write_Command_MMC (CMD) !=1)
	{
		/*if (Timeout++ > 200)
			{
			return(1); 
			}*/
	}	
	//Sendet Commando CMD1 
	Timeout = 0;
	CMD[0] = 0x41;//Commando 1
	CMD[5] = 0xFF;
	
	while( Write_Command_MMC (CMD) !=0)
	{
		/*if (Timeout++ > 200)
			{
			return(2); 
			}*/
	}
        //全速4MHZ
        //U0CTL |= SWRST;//串口设置控制位使能 数据位为8bit
        //UBR0_0 = 2;
        //U0CTL &=~SWRST;//串口设置控制位使能 数据位为8bit
	MMC_Disable();
	return(0);
}

//############################################################################
unsigned char Write_Command_MMC (unsigned char *CMD)
//############################################################################
{
        WDTCTL =WDTPW+WDTHOLD;//WDT STOP
	unsigned char tmp = 0xff;
	unsigned int Timeout = 0;

	MMC_Disable();

	//sendet 8 Clock Impulse
	Write_Byte_MMC(0xFF);

	MMC_Enable();

	for (unsigned char a = 0;a<0x06;a++) 
		{
		Write_Byte_MMC(*CMD++);
		}

	while (tmp == 0xff)	
		{
		tmp = Read_Byte_MMC();
		if (Timeout++ > 500)
			{
			break; 
                        
			}
		}
        
        WDTCTL =0x5A1f;   
	return(tmp);
}


//############################################################################
inline  unsigned char Read_Byte_MMC(void)
//############################################################################
{
	char Byte;// = 0;
	TXBUF_0 = 0xff;
	while(!(IFG1&URXIFG0)){};
	Byte =RXBUF_0;
	return (Byte);
}


//############################################################################
inline void Write_Byte_MMC(unsigned char Byte)
//############################################################################
{
	TXBUF_0 = Byte; 	
	while(!(IFG1 &UTXIFG0)){}; 
}
//############################################################################
unsigned char mmc_write_sector (unsigned long addr,unsigned char *Buffer)
//############################################################################
{
        unsigned char tmp;
    unsigned char CMD[] = {0x58,0x00,0x00,0x00,0x00,0x95};
    addr = addr << 9; //addr = addr * 512
    
    CMD[1] = ((addr & 0xFF000000) >>24 );
    CMD[2] = ((addr & 0x00FF0000) >>16 );
    CMD[3] = ((addr & 0x0000FF00) >>8 );

    tmp = Write_Command_MMC (CMD);
    if (tmp != 0)
        {
        return(tmp);
        }

    for (unsigned char a=0;a<3;a++)
        {
        Read_Byte_MMC();
        }

    Write_Byte_MMC(0xFE);    
    for (unsigned  int a=0;a<512;a++)
        {
        Write_Byte_MMC(*Buffer++);
        }
    Write_Byte_MMC(0xFF);
    Write_Byte_MMC(0xFF);
    Write_Byte_MMC(0xFF);
    char i=Read_Byte_MMC();
    
    if((i&0x1f) != 0x05)return(i);//等待是否成功
        while (Read_Byte_MMC()== 0){};
    MMC_Disable();
    
return(0);
}

//############################################################################
void MMC_Read_Block(unsigned char *CMD,unsigned char *Buffer,unsigned  int Bytes)
//############################################################################
{	
	if (Write_Command_MMC (CMD) != 0)
			{
			 return;
			}
	while (Read_Byte_MMC() != 0xfe){};
	for (unsigned  int a=0;a<Bytes;a++)
		{
		*Buffer++ = Read_Byte_MMC();
		}
	//CRC-Byte
	Read_Byte_MMC();//CRC - Byte 
	Read_Byte_MMC();//CRC - Byte 
	MMC_Disable();
	return;
}

//############################################################################
unsigned char mmc_read_sector (unsigned long addr,unsigned char *Buffer)
//############################################################################
{	
	unsigned char CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF};
	addr = addr << 9; //addr = addr * 512
	CMD[1] = ((addr & 0xFF000000) >>24 );
	CMD[2] = ((addr & 0x00FF0000) >>16 );
	CMD[3] = ((addr & 0x0000FF00) >>8 );
        MMC_Read_Block(CMD,Buffer,512);

	return(0);
}

//############################################################################
unsigned char Read_CID_MMC (unsigned char *Buffer)
//############################################################################
{
	unsigned char CMD[] = {0x4A,0x00,0x00,0x00,0x00,0xFF};
	MMC_Read_Block(CMD,Buffer,16);
	return(0);
}

//############################################################################
unsigned char Read_CSD_MMC (unsigned char *Buffer)
//############################################################################
{	
	unsigned char CMD[] = {0x49,0x00,0x00,0x00,0x00,0xFF};
	MMC_Read_Block(CMD,Buffer,16);
	return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -