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

📄 mmc.c

📁 MSP430单片机对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;
 //**************************************
#define SIMO0 BIT1
#define UCLK0 BIT3
#define SOMI0 BIT2
#define UTXD0 BIT4
#define URXD0 BIT5
//**************************************
        P3SEL&=~1;
        P3DIR|=1;
    U0CTL |= (SWRST+CHAR);//串口设置控制位使能 数据位为8bit
    U0TCTL |= (SSEL1+STC+CKPH);//波特率发生器选择SMCLK//+STC为SPI三线模式设置
    U0CTL |=SYNC;//SPI模式
    U0CTL |=MM;//master mode
    P3DIR |=(SIMO0+UCLK0);
    P3DIR &=~SOMI0;
    P3SEL &=~(UTXD0+ URXD0);//禁止UART
    P3SEL |=(SIMO0+SOMI0+UCLK0);//使能SPI

    UBR0_0 = 2;	//波特率在头文件内定义
    UBR1_0 = 0;  //波特率在头文件内定义
    UMCTL_0 =0;//调整寄存器在头文件内定义

    UCTL0 &=~SWRST;//串口设置完成
    ME1 |= USPIE0; //使能SPI
    ME1 &=~ UTXE0;//禁止UART_TDX
    IE1 &=~(URXIE0+UTXIE0);//禁止SPI中断
    
	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++ > 10000)
			{
			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,0xFF};
	//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<100;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); 
	
	while (Read_Byte_MMC() != 0xff){};
        
	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 + -