SD卡读写的方式(用SD模式)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

大家读写SD卡怎么不用SD模式方式读取,是SPI方式读取简单?

下面贴一段代码,是在FPGA上程序,只能用SD模式进行读,没有文件系统,没有用AVR硬件实现,电路图:

电路说明,SD_DAT3一直给高电平

程序:

#ifndef __SD_Card_H__

#define __SD_Card_H__

#define High 1

#define Low 0

//-------------------------------------------------------------------------

// SD Card Set I/O Direction

#define SD_CMD_IN DDRX.1 = Low

#define SD_CMD_OUT DDRX.1 = High

#define SD_DAT_IN DDRX.2 = Low

#define SD_DAT_OUT DDRX.2 = High

#define SD_CLK_OUT DDRX.3 = High

#define SD_DAT3_OUT DDRX.0 = High

// SD Card Output High/Low

#define SD_CMD_LOW PORTX.1 = Low

#define SD_CMD_HIGH PORTX.1 = High

#define SD_DAT_LOW PORTX.2 = Low

#define SD_DAT_HIGH PORTX.2 = High

#define SD_CLK_LOW PORTX.3 = Low

#define SD_CLK_HIGH PORTX.3 = High

#define SD_DAT3_HIGH PORTX.0 = High

// SD Card Input Read

#define SD_TEST_CMD PINX.1

#define SD_TEST_DAT PINX.2

//------------------------------------------------------------------------- #define BYTE unsigned char

#define UINT16 unsigned int

#define UINT32 unsigned long

//------------------------------------------------------------------------- void Ncr(void);

void Ncc(void);

BYTE response_R(BYTE);

BYTE send_cmd(BYTE *);

BYTE SD_read_lba(BYTE *,UINT32,UINT32);

BYTE SD_card_init(void);

//------------------------------------------------------------------------- BYTE read_status;

BYTE response_buffer[20];

BYTE RCA[2];

BYTE cmd_buffer[5];

const BYTE cmd0[5] = {0x40,0x00,0x00,0x00,0x00};

const BYTE cmd55[5] = {0x77,0x00,0x00,0x00,0x00};

const BYTE cmd2[5] = {0x42,0x00,0x00,0x00,0x00};

const BYTE cmd3[5] = {0x43,0x00,0x00,0x00,0x00};

const BYTE cmd7[5] = {0x47,0x00,0x00,0x00,0x00};

const BYTE cmd9[5] = {0x49,0x00,0x00,0x00,0x00};

const BYTE cmd16[5] = {0x50,0x00,0x00,0x02,0x00};

const BYTE cmd17[5] = {0x51,0x00,0x00,0x00,0x00};

const BYTE acmd6[5] = {0x46,0x00,0x00,0x00,0x02};

const BYTE acmd41[5] = {0x69,0x0f,0xf0,0x00,0x00};

const BYTE acmd51[5] = {0x73,0x00,0x00,0x00,0x00};

//------------------------------------------------------------------------- void Ncr(void)

{

SD_CMD_IN;

SD_CLK_LOW;

SD_CLK_HIGH;

SD_CLK_LOW;

SD_CLK_HIGH;

}

//------------------------------------------------------------------------- void Ncc(void)

{

int i;

for(i=0;i<8;i++)

{

SD_CLK_LOW;

SD_CLK_HIGH;

}

}

//------------------------------------------------------------------------- BYTE SD_card_init(void)

{

BYTE x,y;

SD_CMD_OUT;

SD_DAT_IN;

SD_CLK_HIGH;

SD_CMD_HIGH;

SD_DAT_LOW;

read_status=0;

for(x=0;x<40;x++)

Ncr();

for(x=0;x<5;x++)

cmd_buffer[x]=cmd0[x];

y = send_cmd(cmd_buffer);

do

{

for(x=0;x<40;x++);

Ncc();

for(x=0;x<5;x++)

cmd_buffer[x]=cmd55[x];

y = send_cmd(cmd_buffer);

Ncr();

if(response_R(1)>1) //response too long or crc error

return 1;

Ncc();

for(x=0;x<5;x++)

cmd_buffer[x]=acmd41[x];

y = send_cmd(cmd_buffer);

Ncr();

} while(response_R(3)==1);

Ncc();

for(x=0;x<5;x++)

相关文档
最新文档