AVR单片机的SD卡接口程序设计

AVR单片机的SD卡接口程序设计,第1张

/*SDCodeforM32L*/

/*Bypasyong*/

/*2006-4*/

/*BaseICC6.31A*/

/************************************************************/

#include

#include

#include“1011.h”

#defineucharunsignedchar

#defineuintunsignedint

#defineMMC_CS_PINBIT(4)//PORTB.4

#defineMMC_PORTPORTB

ucharreading=0,a=0,pointer=0;

voidsd_port_init()

{

MMC_PORT|=MMC_CS_PIN;

}

ucharBUFFER[512];//扇区缓冲区

uinTI=0;

voiddelay_nus(uintn)

{

unsignedcharb;

for(b=1;b;

}

//****************************************************************************

//SendaCommandtoMMC/SD-Card

//Return:thesecondbyteofresponseregisterofMMC/SD-Card

//****************************************************************************

ucharSD_Write_Command(ucharcmd,unsignedlongarg)

{

uchartmp;

ucharretry=0;

//MMC_PORT|=MMC_CS_PIN;//SD卡关闭

//send8CLOCkImpulse

Write_Byte_SPI(0xFF);

//setMMC_Chip_Selecttolow(MMC/SD-CardacTIve)

MMC_PORT&=~MMC_CS_PIN;//SD卡使能

Write_Byte_SPI(cmd|0x40);//送头命令

Write_Byte_SPI(arg》》24);

Write_Byte_SPI(arg》》16);//send6ByteCommandtoMMC/SD-Card

Write_Byte_SPI(arg》》8);

Write_Byte_SPI(arg&0xff);

Write_Byte_SPI(0x95);//仅仅对RESET有效的CRC效验码

//get8bitresponse

//Read_Byte_MMC();//readthefirstbyte,ignoreit.

do

{//Onlylast8biTIsusedhere.Readitout.

tmp=Read_Byte_SPI();

retry++;

}

while((tmp==0xff)&&(retry《100));//当没有收到有效的命令的时候

if(reading==0)

MMC_PORT|=MMC_CS_PIN;//MMC_CS_PIN=1;

elseMMC_PORT&=~MMC_CS_PIN;//MMC_CS_PIN=0;

return(tmp);

}

//****************************************************************************

//SD卡初始化(SPI-MODE)

//****************************************************************************

ucharSD_Init(void)

{

ucharretry,temp;

uchari;

MMC_PORT&=~MMC_CS_PIN;//SD卡使能

delay_nus(250);//WaitMMC/SDready.。.

for(i=0;i《0x0f;i++)

{

Write_Byte_SPI(0xff);//send74clockatleast!!!

}

//SendCommandCMD0toMMC/SDCard

retry=0;

do

{//retry200TImestosendCMD0command

temp=SD_Write_Command(0,0);

retry++;

if(retry==100)

{

;//CMD0Error!

}

}

while(temp!=1);

//SendCommandCMD1toMMC/SD-Card

retry=0;

do

{//retry100timestosendCMD1command

temp=SD_Write_Command(1,0);

retry++;

if(retry==100)

{

;

}

}

while(temp!=0);

retry=0;

SD_Write_Command(16,512);//设置一次读写BLOCK的长度为512个字节

MMC_PORT|=MMC_CS_PIN;//MMC_CS_PIN=1;//setMMC_Chip_Selecttohigh

return(0);//Allcommandshavebeentaken.

}

//****************************************************************************

//从SD卡读一个扇区Return0ifnoError.

//****************************************************************************

ucharSD_Read_Block(unsignedlongaddress)

{

uchartemp=0;uinti=0;

reading=1;

temp=SD_Write_Command(17,address);//读出RESPONSE

while(Read_Byte_SPI()!=0xfe)

{;}//直到读取到了数据的开始头0XFE,才继续

for(i=0;i《512;i++)

{

BUFFER[i]=Read_Byte_SPI();

}

Read_Byte_SPI();//CRC-Byte

Read_Byte_SPI();//CRC-Byte

reading=0;

MMC_PORT|=MMC_CS_PIN;//关闭SD卡

return(temp);

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/dianzi/2592675.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-08
下一篇 2022-08-08

发表评论

登录后才能评论

评论列表(0条)

保存