微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 51单片机读写SD卡程序

51单片机读写SD卡程序

时间:11-29 来源:互联网 点击:
SD卡简介
SD卡,是一种基于半导体快闪记忆器的新一代记忆设备,SD于1999年8月研制成功,其重量只有2克。但却拥有高记忆容量、快速数据传输率、极大的移动灵活性以及很好的安全性。SD卡也很容易重新格式化,有广泛的应用领域,如音乐、电影、新闻等多媒体文件都可以方便地保存,数码相机也开始支持SD卡。SD卡容量最高能达到4GB。
SD卡在24mm×32mm×2.1mm的体积内结合了〔SanDisk〕快闪记忆卡控制与MLC(Multilevel Cell)技术和Toshiba(东芝)0.16u及0.13u的NAND技术,通过9针的接口界面与专门的驱动器相连接,不需要额外的电源来保持其上记忆的信息。而且它是一体化固体介质,没有任何移动部分,所以不用担心机械运动的损坏。
  SD卡(Secure Digital Memory Card)是一种基于半导体闪存工艺的存储卡,1999年,由日本松下、东芝及美国SanDisk公司共同研制完成。2000年,这几家公司发起成立了SD协会(Secure Digital Association简称SDA),阵容强大,吸引了大量厂商参加。其中包括IBM,Microsoft,Motorola,NEC、Samsung等。在这些领导厂商的推动下,SD卡已成为目前消费数码设备中应用最广泛的一种存储卡。SD卡具有大容量、高性能、安全等多种特点的多功能存储卡,它比MMC卡多了一个进行数据著作权保护的暗号认证功能(SDMI规格),读写速度比MMC卡要快4倍。
好了,既然这么好,我们在这里给出51单片机读写2G SD卡程序:
#include //程序通过调试
#include
//=============================================================
//定义SD卡需要的4根信号线
sbit SD_CLK = P3^7;
sbit SD_DI = P3^5;
sbit SD_DO = P3^6;
sbit SD_CS = P3^4;
//===========================================================
//定义按键端口
sbit KEY = P2^7;
//===========================================================
//定义512字节缓冲区,注意需要使用 xdata关键字
unsigned char xdata DATA[512];
void delayms(unsigned int count)
{
int i,j;
for(i=0;i
{
for(j=0;j<260;j++);
}
}
//===========================================================
//写一字节到SD卡,模拟SPI总线方式
void SdWrite(unsigned char n)
{
unsigned char i;
for(i=8;i;i--)
{
SD_CLK=0;
SD_DI=(n&0x80);
n<=1;
SD_CLK=1;
}
SD_DI=1;
}
//===========================================================
//从SD卡读一字节,模拟SPI总线方式
unsigned char SdRead()
{
unsigned char n,i;
for(i=8;i;i--)
{
SD_CLK=0;
SD_CLK=1;
n<=1;
if(SD_DO) n|=1;
}
return n;
}
//============================================================
//检测SD卡的响应
unsigned char SdResponse()
{
unsigned char i=0,response;
while(i<=8)
{
response = SdRead();
if(response==0x00)
break;
if(response==0x01)
break;
i++;
}
return response;
}
//================================================================
//发命令到SD卡
void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
{
SdWrite(command|0x40);
SdWrite(((unsigned char *)&argument)[0]);
SdWrite(((unsigned char *)&argument)[1]);
SdWrite(((unsigned char *)&argument)[2]);
SdWrite(((unsigned char *)&argument)[3]);
SdWrite(CRC);
}
//================================================================
//初始化SD卡
unsigned char SdInit(void)
{
int delay=0, trials=0;
unsigned char i;
unsigned char response=0x01;
SD_CS=1;
for(i=0;i<=9;i++)
SdWrite(0xff);
SD_CS=0;
//Send Command 0 to put MMC in SPI mode
SdCommand(0x00,0,0x95);

response=SdResponse();
if(response!=0x01)
{
return 0;
}
while(response==0x01)
{
SD_CS=1;
SdWrite(0xff);
SD_CS=0;
SdCommand(0x01,0x00ffc000,0xff);
response=SdResponse();
}
SD_CS=1;
SdWrite(0xff);
return 1;
}
//================================================================
//往SD卡指定地址写数据,一次最多512字节
//unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
unsigned char SdWriteBlock(unsigned long address,int len)
{
unsigned int count;
unsigned char dataResp;
//Block size is 512 bytes exactly
//First Lower SS
SD_CS=0;
//Then send write command
SdCommand(0x18,address,0xff);
if(SdResponse()==00)
{
SdWrite(0xff);
SdWrite(0xff);
SdWrite(0xff);
//command was a success - now send data
//start with DATA TOKEN = 0xFE
SdWrite(0xfe);
//now send data
//for(count=0;countfor(count=0;count//for(count=0;countfor(;count<512;count++) SdWrite(0);
//data block sent - now send checksum
SdWrite(0xff); //两字节CRC校验, 为0XFFFF 表示不考虑CRC
SdWrite(0xff);
//Now read in the DATA RESPONSE token
dataResp=SdRead();
//Following the DATA RESPONSE token
//are a number of BUSY bytes
//a zero byte indicates the MMC is busy
while(SdRead()==0);
dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
SD_CS=1;
SdWrite(0xff);
if(dataResp==0x0b)
{
//printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR");
return 0;
}
if(dataResp==0x05)
return 1;
//printf("Invalid data Response token.");
return 0;
}
//printf("Command 0x18 (Write) was not received by the MMC.");
return 0;
}
//=======================================================================
//从SD卡指定地址读取数据,一次最多512字节
unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
{
unsigned int count;
//Block size is 512 bytes exactly
//First Lower SS
//printf("MMC_read_block");
SD_CS=0;
//Then send write command
SdCommand(0x11,address,0xff);
if(SdResponse()==00)
{
//command was a success - now send data
//start with DATA TOKEN = 0xFE
while(SdRead()!=0xfe);
for(count=0;countfor(;count<512;count++) SdRead();
//data block sent - now send checksum
SdRead();
SdRead();
//Now read in the DATA RESPONSE token
SD_CS=1;
SdRead();
return 1;
}
//printf("Command 0x11 (Read) was not received by the MMC.");
return 0;
}

void initbaud(void)
{
TMOD=0X20;
TH1=0XFD;
TL1=0XFD;
PCON=0X00;
TR1=1;
SCON=0X50;//8位波特可变
//SCON=0X52;//8位波特可变 TI开中断
}
//============================================================
//主程序
main()
{
unsigned int i;
unsigned long AddTemp=331264;//SD卡地址第一个数据物理地址初始值,可以用winhex查看,这里是641物理扇区,512x641=328192,根据实际SD卡内容更改
delayms(5);
SdInit(); //SD卡初始化
while(1)
{
SdWriteBlock(AddTemp,512);
delayms(1000);
AddTemp=331264;
SdReadBlock(DATA,AddTemp,512);//每次读出512字节放到缓冲区
initbaud();
for(i=0;i<512;i++)
{
SBUF=DATA[i];
while(!TI);
TI=0;
delayms(1);
}
while(KEY); //等待按键按下继续执行

}
}
可以在串口中看到SD卡中被写入的数据,程序在MCU-51/AVR开发板进行了实验。具体介绍可访问:
http://item.taobao.com/item.htm?spm=0.0.0.50.xZjZ87&id=21441052281,http://item.taobao.com/item.htm?spm=1103*oQM.3-5SusJ.h-2Yh1mq&id=14049701171& 这是两款功能强大的51、AVR开发板,对初学者和研发人员都是不错的选择,下一节给大家讲下结合SD卡的TFT真彩屏的相关读写!



Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top