写指令的例程:
- //———————————————————————————————–
向SD卡中写入指令,并回来回应的第二个字节 - //———————————————————————————————–
- unsigned
char Write_Command_SD(unsigned char *CMD) - {
unsigned char tmp; unsigned char retry=0; unsigned char i; //制止SD卡片选 SPI_CS=1; //发送8个时钟信号 Write_Byte_SD(0xFF); //使能SD卡片选 SPI_CS=0; //向SD卡发送6字节指令 for (i=0;i<0x06;i++) { Write_Byte_SD(*CMD++); } //取得16位的回应 Read_Byte_SD(); //read the first byte,ignore it. do { //读取后8位 tmp = Read_Byte_SD(); retry++; } while((tmp==0xff)&&(retry<100)); return(tmp); - }
2)
SD卡的初始化是非常重要的,只要进行了正确的初始化,才干进行后边的各项操作。在初始化进程中,SPI的时钟不能太快,否则会造初始化失利。在初始化成功后,应尽量进步SPI的速率。在刚开端要先发送至少74个时钟信号,这是有必要的。在许多读者的试验中,许多是因为忽略了这一点,而使初始化不成功。随后便是写入两个指令CMD0与CMD1,使SD卡进入SPI形式
- //————————————————————————–
初始化SD卡到SPI形式 - //————————————————————————–
- unsigned
char SD_Init() - {
unsigned char retry,temp; unsigned char i; unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95}; SD_Port_Init(); //初始化驱动端口 Init_Flag=1; //将初始化标志置1 for (i=0;i<0x0f;i++) { Write_Byte_SD(0xff); //发送至少74个时钟信号 } //向SD卡发送CMD0 retry=0; do { //为了能够成功写入CMD0,在这里写200次 temp=Write_Command_SD(CMD); retry++; if(retry==200) { //超越200次 return(INIT_CMD0_ERROR);//CMD0 Error! } } while(temp!=1); //回应01h,中止写入 //发送CMD1到SD卡 CMD[0] = 0x41; //CMD1 CMD[5] = 0xFF; retry=0; do { //为了能成功写入CMD1,写100次 temp=Write_Command_SD(CMD); retry++; if(retry==100) { //超越100次 return(INIT_CMD1_ERROR);//CMD1 Error! } } while(temp!=0);//回应00h中止写入 Init_Flag=0; //初始化结束,初始化标志清零 SPI_CS=1; //片选无效 return(0); //初始化成功 - }
3)
CID寄存器存储了SD卡的标识码。每一个卡都有仅有的标识码。
CID寄存器长度为128位。它的寄存器结构如下:
称号
|
域
|
数据宽度
|
CID区分
|
出产标识号
|
MID
|
8
|
[127:120]
|
OEM/运用标识
|
OID
|
16
|
[119:104]
|
产品称号
|
PNM
|
40
|
[103:64]
|
产品版别
|
PRV
|
8
|
[63:56]
|
产品序列号
|
PSN
|
32
|
[55:24]
|
保存
|
-
|
4
|
[23:20]
|
出产日期
|
MDT
|
12
|
[19:8]
|
CRC7校验合
|
CRC
|
7
|
[7:1]
|
未运用,一直为1
|
-
|
1
|
[0:0]
|
它的读取时序如下:
- //————————————————————————————
读取SD卡的CID寄存器 16字节 成功回来0 - //————————————————————————————-
- unsigned
char Read_CID_SD(unsigned char *Buffer) - {
//读取CID寄存器的指令 unsigned char CMD[] = {0x4A,0x00,0x00,0x00,0x00,0xFF}; unsigned char temp; temp=SD_Read_Block(CMD,Buffer,16); //read 16 bytes return(temp); - }
4)读取CSD
CSD(Card-Specific Data)寄存器供给了读写SD卡的一些信息。其间的一些单元能够由用户从头编程。
- //—————————————————————————————–
读SD卡的CSD寄存器 共16字节 回来0阐明读取成功 - //—————————————————————————————–
- unsigned
char Read_CSD_SD(unsigned char *Buffer) - {
//读取CSD寄存器的指令 unsigned char CMD[] = {0x49,0x00,0x00,0x00,0x00,0xFF}; unsigned char temp; temp=SD_Read_Block(CMD,Buffer,16); //read 16 bytes return(temp); - }
4)
归纳上面临CID与CSD寄存器的读取,能够知道许多关于SD卡的信息,以下程序能够获取这些信息。如下:
- //———————————————————————————————–
- //回来
- //
SD卡的容量,单位为M - //
sector count and multiplier MB are in - u08
== C_SIZE / (2^(9-C_SIZE_MULT)) - //
SD卡的称号 - //———————————————————————————————–
- void
SD_get_volume_info() - {
unsigned char i; unsigned char c_temp[5]; VOLUME_INFO_TYPE SD_volume_Info,*vinf; vinf=&SD_volume_Info; //Init the pointoer; - /读取CSD寄存器
Read_CSD_SD(sectorBuffer.dat); - //获取总扇区数
vinf->sector_count = sectorBuffer.dat[6] & 0x03; vinf->sector_count <<= 8; vinf->sector_count += sectorBuffer.dat[7]; vinf->sector_count <<= 2; vinf->sector_count += (sectorBuffer.dat[8] & 0xc0) >> 6; // 获取multiplier vinf->sector_multiply = sectorBuffer.dat[9] & 0x03; vinf->sector_multiply <<= 1; vinf->sector_multiply += (sectorBuffer.dat[10] & 0x80) >> 7; - //获取SD卡的容量
vinf->size_MB = vinf->sector_count >> (9-vinf->sector_multiply); // get the name of the card Read_CID_SD(sectorBuffer.dat); vinf->name[0] = sectorBuffer.dat[3]; vinf->name[1] = sectorBuffer.dat[4]; vinf->name[2] = sectorBuffer.dat[5]; vinf->name[3] = sectorBuffer.dat[6]; vinf->name[4] = sectorBuffer.dat[7]; vinf->name[5] = 0x00; //end flag - }
以上程序将信息装载到一个结构体中,这个结构体的界说如下: - typedef
struct SD_VOLUME_INFO - {
//SD/SD Card info unsigned int size_MB; unsigned char sector_multiply; unsigned int sector_count; unsigned char name[6]; - }
VOLUME_INFO_TYPE;
5)
扇区读是对SD卡驱动的意图之一。SD卡的每一个扇区中有512个字节,一次扇区读操作将把某一个扇区内的512个字节悉数读出。进程很简单,先写入指令,在得到相应的回应后,开端数据读取。
扇区读的时序:
- unsigned
char SD_Read_Sector(unsigned long sector,unsigned char *buffer) - {
unsigned char retry; //指令16 unsigned char CMD[] = {0x51,0x00,0x00,0x00,0x00,0xFF}; unsigned char temp; //地址改换 由逻辑块地址转为字节地址 sector = sector << 9; //sector = sector * 512 CMD[1] = ((sector & 0xFF000000) >>24 ); CMD[2] = ((sector & 0x00FF0000) >>16 ); CMD[3] = ((sector & 0x0000FF00) >>8 ); //将指令16写入SD卡 retry=0; do { //为了确保写入指令 总共写100次 temp=Write_Command_MMC(CMD); retry++; if(retry==100) { return(READ_BLOCK_ERROR); //block write Error! } } while(temp!=0); //Read Start Byte form MMC/SD-Card (FEh/Start Byte) //Now data is ready,you can read it out. while (Read_Byte_MMC() != 0xfe); readPos=0; SD_get_data(512,buffer) ; //512字节被读出到buffer中 return 0; - }
- 其间SD_get_data函数如下:
- //—————————————————————————-
获取数据到buffer中 - //—————————————————————————-
- void
SD_get_data(unsigned int Bytes,unsigned char *buffer) - {
unsigned int j; for (j=0;j<="" span="" style="word-wrap: break-word;"> *buffer++ = Read_Byte_SD(); - }
6)
扇区写是SD卡驱动的另一意图。每次扇区写操作将向SD卡的某个扇区中写入512个字节。进程与扇区读类似,仅仅数据的方向相反与写入指令不同罢了。
扇区写的程序例程:
- //——————————————————————————————–
写512个字节到SD卡的某一个扇区中去 回来0阐明写入成功 - //——————————————————————————————–
- unsigned
char SD_write_sector(unsigned long addr,unsigned char *Buffer) - {
unsigned char tmp,retry; unsigned int i; //指令24 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 ); //写指令24到SD卡中去 retry=0; do { //为了牢靠写入,写100次 tmp=Write_Command_SD(CMD); retry++; if(retry==100) { return(tmp); //send commamd Error! } } while(tmp!=0); //在写之前先发生100个时钟信号 for (i=0;i<100;i++) { Read_Byte_SD(); } //写入开端字节 Write_Byte_MMC(0xFE); //现在能够写入512个字节 for (i=0;i<512;i++) { Write_Byte_MMC(*Buffer++); } //CRC-Byte Write_Byte_MMC(0xFF); //Dummy CRC Write_Byte_MMC(0xFF); //CRC Code tmp=Read_Byte_MMC(); // read response if((tmp & 0x1F)!=0x05) // 写入的512个字节是未被承受 { SPI_CS=1; return(WRITE_BLOCK_ERROR); //Error! } //比及SD卡不忙停止 - //因为数据被承受后,SD卡在向贮存阵列中编程数据
while (Read_Byte_MMC()!=0xff){}; //制止SD卡 SPI_CS=1; return(0);//写入成功 - }
单片机选用STC89LE单片机(SD卡的初始化电压为2.0V~3.6V,操作电压为3.1V~3.5V,因而不能用5V单片机,或进行分压处理),作业于22.1184M的时钟下,因为所选用的单片机中没硬件SPI,选用软件模仿SPI,因而读写速率都较慢。假如要半SD卡运用于音频、视频等要求高速场合,则需求选用有硬件SPI的控制器,或运用SD形式,有了 SPI形式的根底,SD形式应该不是什么难事。