新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)對(duì)SD卡讀寫(xiě)系列(二)

單片機(jī)對(duì)SD卡讀寫(xiě)系列(二)

作者: 時(shí)間:2016-11-23 來(lái)源:網(wǎng)絡(luò) 收藏
寫(xiě)命令的例程:

本文引用地址:http://www.2s4d.com/article/201611/320311.htm
  1. //-----------------------------------------------------------------------------------------------
  2. SD卡中寫(xiě)入命令,并返回回應(yīng)的第二個(gè)字節(jié)
  3. //-----------------------------------------------------------------------------------------------
  4. unsignedcharWrite_Command_SD(unsignedchar*CMD)
  5. {
  6. unsignedchartmp;
  7. unsignedcharretry=0;
  8. unsignedchari;
  9. //禁止SD卡片選
  10. SPI_CS=1;
  11. //發(fā)送8個(gè)時(shí)鐘信號(hào)
  12. Write_Byte_SD(0xFF);
  13. //使能SD卡片選
  14. SPI_CS=0;
  15. //向SD卡發(fā)送6字節(jié)命令
  16. for(i=0;i<0x06;i++)
  17. {
  18. Write_Byte_SD(*CMD++);
  19. }
  20. //獲得16位的回應(yīng)
  21. Read_Byte_SD();//readthefirstbyte,ignoreit.
  22. do
  23. {//讀取后8位
  24. tmp=Read_Byte_SD();
  25. retry++;
  26. }
  27. while((tmp==0xff)&&(retry<100));
  28. return(tmp);
  29. }

2)初始化
SD卡的初始化是非常重要的,只有進(jìn)行了正確的初始化,才能進(jìn)行后面的各項(xiàng)操作。在初始化過(guò)程中,SPI的時(shí)鐘不能太快,否則會(huì)造初始化失敗。在初始化成功后,應(yīng)盡量提高SPI的速率。在剛開(kāi)始要先發(fā)送至少74個(gè)時(shí)鐘信號(hào),這是必須的。在很多讀者的實(shí)驗(yàn)中,很多是因?yàn)槭韬隽诉@一點(diǎn),而使初始化不成功。隨后就是寫(xiě)入兩個(gè)命令CMD0與CMD1,使SD卡進(jìn)入SPI模式
初始化時(shí)序圖:


初始化例程:

  1. //--------------------------------------------------------------------------
  2. 初始化SD卡到SPI模式
  3. //--------------------------------------------------------------------------
  4. unsignedcharSD_Init()
  5. {
  6. unsignedcharretry,temp;
  7. unsignedchari;
  8. unsignedcharCMD[]={0x40,0x00,0x00,0x00,0x00,0x95};
  9. SD_Port_Init();//初始化驅(qū)動(dòng)端口
  10. Init_Flag=1;//將初始化標(biāo)志置1
  11. for(i=0;i<0x0f;i++)
  12. {
  13. Write_Byte_SD(0xff);//發(fā)送至少74個(gè)時(shí)鐘信號(hào)
  14. }
  15. //向SD卡發(fā)送CMD0
  16. retry=0;
  17. do
  18. {//為了能夠成功寫(xiě)入CMD0,在這里寫(xiě)200次
  19. temp=Write_Command_SD(CMD);
  20. retry++;
  21. if(retry==200)
  22. {//超過(guò)200次
  23. return(INIT_CMD0_ERROR);//CMD0Error!
  24. }
  25. }
  26. while(temp!=1);//回應(yīng)01h,停止寫(xiě)入
  27. //發(fā)送CMD1到SD卡
  28. CMD[0]=0x41;//CMD1
  29. CMD[5]=0xFF;
  30. retry=0;
  31. do
  32. {//為了能成功寫(xiě)入CMD1,寫(xiě)100次
  33. temp=Write_Command_SD(CMD);
  34. retry++;
  35. if(retry==100)
  36. {//超過(guò)100次
  37. return(INIT_CMD1_ERROR);//CMD1Error!
  38. }
  39. }
  40. while(temp!=0);//回應(yīng)00h停止寫(xiě)入
  41. Init_Flag=0;//初始化完畢,初始化標(biāo)志清零
  42. SPI_CS=1;//片選無(wú)效
  43. return(0);//初始化成功
  44. }

3)讀取CID
CID寄存器存儲(chǔ)了SD卡的標(biāo)識(shí)碼。每一個(gè)卡都有唯一的標(biāo)識(shí)碼。
CID寄存器長(zhǎng)度為128位。它的寄存器結(jié)構(gòu)如下:

名稱
數(shù)據(jù)寬度
CID劃分
生產(chǎn)標(biāo)識(shí)號(hào)
MID
8
[127:120]
OEM/應(yīng)用標(biāo)識(shí)
OID
16
[119:104]
產(chǎn)品名稱
PNM
40
[103:64]
產(chǎn)品版本
PRV
8
[63:56]
產(chǎn)品序列號(hào)
PSN
32
[55:24]
保留
4
[23:20]
生產(chǎn)日期
MDT
12
[19:8]
CRC7校驗(yàn)合
CRC
7
[7:1]
未使用,始終為1
1
[0:0]

它的讀取時(shí)序如下:

與此時(shí)序相對(duì)應(yīng)的程序如下:

  1. //------------------------------------------------------------------------------------
  2. 讀取SD卡的CID寄存器16字節(jié)成功返回0
  3. //-------------------------------------------------------------------------------------
  4. unsignedcharRead_CID_SD(unsignedchar*Buffer)
  5. {
  6. //讀取CID寄存器的命令
  7. unsignedcharCMD[]={0x4A,0x00,0x00,0x00,0x00,0xFF};
  8. unsignedchartemp;
  9. temp=SD_Read_Block(CMD,Buffer,16);//read16bytes
  10. return(temp);
  11. }

4)讀取CSD
CSD(Card-Specific Data)寄存器提供了讀寫(xiě)SD卡的一些信息。其中的一些單元可以由用戶重新編程。 讀取CSD 的時(shí)序:


相應(yīng)的程序例程如下:

  1. //-----------------------------------------------------------------------------------------
  2. 讀SD卡的CSD寄存器共16字節(jié)返回0說(shuō)明讀取成功
  3. //-----------------------------------------------------------------------------------------
  4. unsignedcharRead_CSD_SD(unsignedchar*Buffer)
  5. {
  6. //讀取CSD寄存器的命令
  7. unsignedcharCMD[]={0x49,0x00,0x00,0x00,0x00,0xFF};
  8. unsignedchartemp;
  9. temp=SD_Read_Block(CMD,Buffer,16);//read16bytes
  10. return(temp);
  11. }

4)讀取SD卡信息
綜合上面對(duì)CID與CSD寄存器的讀取,可以知道很多關(guān)于SD卡的信息,以下程序可以獲取這些信息。如下:

  1. //-----------------------------------------------------------------------------------------------
  2. //返回
  3. //SD卡的容量,單位為M
  4. //sectorcountandmultiplierMBarein
  5. u08==C_SIZE/(2^(9-C_SIZE_MULT))
  6. //SD卡的名稱
  7. //-----------------------------------------------------------------------------------------------
  8. voidSD_get_volume_info()
  9. {
  10. unsignedchari;
  11. unsignedcharc_temp[5];
  12. VOLUME_INFO_TYPESD_volume_Info,*vinf;
  13. vinf=&SD_volume_Info;//Initthepointoer;
  14. /讀取CSD寄存器
  15. Read_CSD_SD(sectorBuffer.dat);
  16. //獲取總扇區(qū)數(shù)
  17. vinf->sector_count=sectorBuffer.dat[6]&0x03;
  18. vinf->sector_count<<=8;
  19. vinf->sector_count+=sectorBuffer.dat[7];
  20. vinf->sector_count<<=2;
  21. vinf->sector_count+=(sectorBuffer.dat[8]&0xc0)>>6;
  22. //獲取multiplier
  23. vinf->sector_multiply=sectorBuffer.dat[9]&0x03;
  24. vinf->sector_multiply<<=1;
  25. vinf->sector_multiply+=(sectorBuffer.dat[10]&0x80)>>7;
  26. //獲取SD卡的容量
  27. vinf->size_MB=vinf->sector_count>>(9-vinf->sector_multiply);
  28. //getthenameofthecard
  29. Read_CID_SD(sectorBuffer.dat);
  30. vinf->name[0]=sectorBuffer.dat[3];
  31. vinf->name[1]=sectorBuffer.dat[4];
  32. vinf->name[2]=sectorBuffer.dat[5];
  33. vinf->name[3]=sectorBuffer.dat[6];
  34. vinf->name[4]=sectorBuffer.dat[7];
  35. vinf->name[5]=0x00;//endflag
  36. }
  37. 以上程序?qū)⑿畔⒀b載到一個(gè)結(jié)構(gòu)體中,這個(gè)結(jié)構(gòu)體的定義如下:
  38. typedefstructSD_VOLUME_INFO
  39. {//SD/SDCardinfo
  40. unsignedintsize_MB;
  41. unsignedcharsector_multiply;
  42. unsignedintsector_count;
  43. unsignedcharname[6];
  44. }VOLUME_INFO_TYPE;

5)扇區(qū)讀
扇區(qū)讀是對(duì)SD卡驅(qū)動(dòng)的目的之一。SD卡的每一個(gè)扇區(qū)中有512個(gè)字節(jié),一次扇區(qū)讀操作將把某一個(gè)扇區(qū)內(nèi)的512個(gè)字節(jié)全部讀出。過(guò)程很簡(jiǎn)單,先寫(xiě)入命令,在得到相應(yīng)的回應(yīng)后,開(kāi)始數(shù)據(jù)讀取。
扇區(qū)讀的時(shí)序:

扇區(qū)讀的程序例程:

  1. unsignedcharSD_Read_Sector(unsignedlongsector,unsignedchar*buffer)
  2. {
  3. unsignedcharretry;
  4. //命令16
  5. unsignedcharCMD[]={0x51,0x00,0x00,0x00,0x00,0xFF};
  6. unsignedchartemp;
  7. //地址變換由邏輯塊地址轉(zhuǎn)為字節(jié)地址
  8. sector=sector<<9;//sector=sector*512
  9. CMD[1]=((sector&0xFF000000)>>24);
  10. CMD[2]=((sector&0x00FF0000)>>16);
  11. CMD[3]=((sector&0x0000FF00)>>8);
  12. //將命令16寫(xiě)入SD卡
  13. retry=0;
  14. do
  15. {//為了保證寫(xiě)入命令一共寫(xiě)100次
  16. temp=Write_Command_MMC(CMD);
  17. retry++;
  18. if(retry==100)
  19. {
  20. return(READ_BLOCK_ERROR);//blockwriteError!
  21. }
  22. }
  23. while(temp!=0);
  24. //ReadStartByteformMMC/SD-Card(FEh/StartByte)
  25. //Nowdataisready,youcanreaditout.
  26. while(Read_Byte_MMC()!=0xfe);
  27. readPos=0;
  28. SD_get_data(512,buffer);//512字節(jié)被讀出到buffer中
  29. return0;
  30. }
  31. 其中SD_get_data函數(shù)如下:
  32. //----------------------------------------------------------------------------
  33. 獲取數(shù)據(jù)到buffer中
  34. //----------------------------------------------------------------------------
  35. voidSD_get_data(unsignedintBytes,unsignedchar*buffer)
  36. {
  37. unsignedintj;
  38. for(j=0;j<="" span="" style="word-wrap: break-word;">
  39. *buffer++=Read_Byte_SD();
  40. }

6)扇區(qū)寫(xiě)
扇區(qū)寫(xiě)是SD卡驅(qū)動(dòng)的另一目的。每次扇區(qū)寫(xiě)操作將向SD卡的某個(gè)扇區(qū)中寫(xiě)入512個(gè)字節(jié)。過(guò)程與扇區(qū)讀相似,只是數(shù)據(jù)的方向相反與寫(xiě)入命令不同而已。
扇區(qū)寫(xiě)的時(shí)序:

扇區(qū)寫(xiě)的程序例程:

  1. //--------------------------------------------------------------------------------------------
  2. 寫(xiě)512個(gè)字節(jié)到SD卡的某一個(gè)扇區(qū)中去返回0說(shuō)明寫(xiě)入成功
  3. //--------------------------------------------------------------------------------------------
  4. unsignedcharSD_write_sector(unsignedlongaddr,unsignedchar*Buffer)
  5. {
  6. unsignedchartmp,retry;
  7. unsignedinti;
  8. //命令24
  9. unsignedcharCMD[]={0x58,0x00,0x00,0x00,0x00,0xFF};
  10. addr=addr<<9;//addr=addr*512
  11. CMD[1]=((addr&0xFF000000)>>24);
  12. CMD[2]=((addr&0x00FF0000)>>16);
  13. CMD[3]=((addr&0x0000FF00)>>8);
  14. //寫(xiě)命令24到SD卡中去
  15. retry=0;
  16. do
  17. {//為了可靠寫(xiě)入,寫(xiě)100次
  18. tmp=Write_Command_SD(CMD);
  19. retry++;
  20. if(retry==100)
  21. {
  22. return(tmp);//sendcommamdError!
  23. }
  24. }
  25. while(tmp!=0);
  26. //在寫(xiě)之前先產(chǎn)生100個(gè)時(shí)鐘信號(hào)
  27. for(i=0;i<100;i++)
  28. {
  29. Read_Byte_SD();
  30. }
  31. //寫(xiě)入開(kāi)始字節(jié)
  32. Write_Byte_MMC(0xFE);
  33. //現(xiàn)在可以寫(xiě)入512個(gè)字節(jié)
  34. for(i=0;i<512;i++)
  35. {
  36. Write_Byte_MMC(*Buffer++);
  37. }
  38. //CRC-Byte
  39. Write_Byte_MMC(0xFF);//DummyCRC
  40. Write_Byte_MMC(0xFF);//CRCCode
  41. tmp=Read_Byte_MMC();//readresponse
  42. if((tmp&0x1F)!=0x05)//寫(xiě)入的512個(gè)字節(jié)是未被接受
  43. {
  44. SPI_CS=1;
  45. return(WRITE_BLOCK_ERROR);//Error!
  46. }
  47. //等到SD卡不忙為止
  48. //因?yàn)閿?shù)據(jù)被接受后,SD卡在向儲(chǔ)存陣列中編程數(shù)據(jù)
  49. while(Read_Byte_MMC()!=0xff){};
  50. //禁止SD卡
  51. SPI_CS=1;
  52. return(0);//寫(xiě)入成功
  53. }

單片機(jī)采用STC89LE單片機(jī)(SD卡的初始化電壓為2.0V~3.6V,操作電壓為3.1V~3.5V,因此不能用5V單片機(jī),或進(jìn)行分壓處理),工作于22.1184M的時(shí)鐘下,由于所采用的單片機(jī)中沒(méi)硬件SPI,采用軟件模擬SPI,因此讀寫(xiě)速率都較慢。如果要半SD卡應(yīng)用于音頻、視頻等要求高速場(chǎng)合,則需要選用有硬件SPI的控制器,或使用SD模式,有了 SPI模式的基礎(chǔ),SD模式應(yīng)該不是什么難事。



評(píng)論


技術(shù)專區(qū)

關(guān)閉