找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 14154|回復(fù): 14
打印 上一主題 下一主題
收起左側(cè)

單片機(jī)SD卡讀卡器仿真+源程序+電路原理圖

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主

分享一個51單片機(jī)做的SD卡讀卡器仿真帶源程序,一個簡單的MMC卡存儲器應(yīng)用。下面是電路原理圖:


可以看到虛擬窗口的結(jié)果出來了.

單片機(jī)SD卡讀卡器設(shè)計的源程序如下:
  1. #include <AT89X52.H>

  2. #define F_OSC  11059200//晶振平率Hz
  3. #define F_BAUD 9600
  4. #define RELOAD 256-F_OSC/12/32/F_BAUD
  5. #define CR 0x0D        //回車

  6. //定義SD卡需要的4根信號線
  7. sbit SD_CLK = P1^4;
  8. sbit SD_DI  = P1^6;
  9. sbit SD_DO  = P1^5;
  10. sbit SD_CS  = P1^7;

  11. unsigned char xdata DATA[512];
  12. //定義512字節(jié)緩沖區(qū),注意需要使用 xdata關(guān)鍵字


  13. //===========================================================
  14. //寫一字節(jié)到SD卡,模擬SPI總線方式
  15. void SdWrite(unsigned char n)
  16. {

  17.     unsigned char i;
  18.    
  19.     for(i=8;i;i--)
  20.     {
  21.         SD_CLK=0;
  22.         SD_DI=(n&0x80);
  23.         n<<=1;
  24.         SD_CLK=1;
  25.         }
  26.         SD_DI=1;
  27.     }
  28. //===========================================================
  29. //從SD卡讀一字節(jié),模擬SPI總線方式
  30. unsigned char SdRead()
  31. {
  32.     unsigned char n,i;
  33.     for(i=8;i;i--)
  34.     {
  35.         SD_CLK=0;
  36.         SD_CLK=1;
  37.         n<<=1;
  38.         if(SD_DO) n|=1;
  39.    
  40.     }
  41.     return n;
  42. }
  43. //============================================================
  44. //檢測SD卡的響應(yīng)
  45. unsigned char SdResponse()
  46. {
  47.     unsigned char i=0,response;
  48.    
  49.     while(i<=8)
  50.     {
  51.         response = SdRead();
  52.         if(response==0x00)
  53.         break;
  54.         if(response==0x01)
  55.         break;
  56.         i++;
  57.     }
  58.     return response;
  59. }
  60. //================================================================
  61. //發(fā)命令到SD卡
  62. void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
  63. {

  64.     SdWrite(command|0x40);
  65.     SdWrite(((unsigned char *)&argument)[0]);
  66.     SdWrite(((unsigned char *)&argument)[1]);
  67.     SdWrite(((unsigned char *)&argument)[2]);
  68.     SdWrite(((unsigned char *)&argument)[3]);
  69.     SdWrite(CRC);
  70. }
  71. //================================================================
  72. //初始化SD卡
  73. unsigned char SdInit(void)
  74. {
  75.     int delay=0, trials=0;
  76.     unsigned char i;
  77.     unsigned char response=0x01;
  78.    
  79.     SD_CS=1;
  80.     for(i=0;i<=9;i++)
  81.     SdWrite(0xff);
  82.     SD_CS=0;
  83.    
  84.     //Send Command 0 to put MMC in SPI mode
  85.     SdCommand(0x00,0,0x95);
  86.    
  87.    
  88.     response=SdResponse();
  89.    
  90.     if(response!=0x01)
  91.     {
  92.         return 0;
  93.     }

  94.     while(response==0x01)
  95.     {
  96.         SD_CS=1;
  97.         SdWrite(0xff);
  98.         SD_CS=0;
  99.         SdCommand(0x01,0x00ffc000,0xff);
  100.         response=SdResponse();
  101.     }

  102.     SD_CS=1;
  103.     SdWrite(0xff);
  104.     return 1;
  105. }
  106. //================================================================
  107. //往SD卡指定地址寫數(shù)據(jù),一次最多512字節(jié)
  108. unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
  109. {
  110.     unsigned int count;
  111.     unsigned char dataResp;
  112.     //Block size is 512 bytes exactly
  113.     //First Lower SS
  114.    
  115.     SD_CS=0;
  116.     //Then send write command
  117.     SdCommand(0x18,address,0xff);
  118.    
  119.     if(SdResponse()==00)
  120.     {
  121.         SdWrite(0xff);
  122.         SdWrite(0xff);
  123.         SdWrite(0xff);
  124.         //command was a success - now send data
  125.         //start with DATA TOKEN = 0xFE
  126.         SdWrite(0xfe);
  127.         //now send data
  128.         for(count=0;count<len;count++) SdWrite(*Block++);
  129.         
  130.         for(;count<512;count++) SdWrite(0);
  131.         //data block sent - now send checksum
  132.         SdWrite(0xff); //兩字節(jié)CRC校驗(yàn), 為0XFFFF 表示不考慮CRC
  133.         SdWrite(0xff);
  134.         //Now read in the DATA RESPONSE token
  135.         dataResp=SdRead();
  136.         //Following the DATA RESPONSE token
  137.         //are a number of BUSY bytes
  138.         //a zero byte indicates the MMC is busy
  139.         
  140.         while(SdRead()==0);
  141.         
  142.         dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
  143.         SD_CS=1;
  144.         SdWrite(0xff);
  145.         if(dataResp==0x0b)
  146.         {
  147.         //printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR\n");
  148.         return 0;
  149.         }
  150.         if(dataResp==0x05)
  151.         return 1;
  152.         
  153.         //printf("Invalid data Response token.\n");
  154.         return 0;
  155.     }
  156.     //printf("Command 0x18 (Write) was not received by the MMC.\n");
  157.     return 0;
  158. }

  159. //=======================================================================
  160. //從SD卡指定地址讀取數(shù)據(jù),一次最多512字節(jié)
  161. unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
  162. {
  163.     unsigned int count;
  164.     //Block size is 512 bytes exactly
  165.     //First Lower SS
  166.    
  167.      //printf("MMC_read_block\n");
  168.    
  169.     SD_CS=0;
  170.     //Then send write command
  171.     SdCommand(0x11,address,0xff);

  172.     if(SdResponse()==00)
  173.     {
  174.         //command was a success - now send data
  175.         //start with DATA TOKEN = 0xFE
  176.         while(SdRead()!=0xfe);
  177.         
  178.         for(count=0;count<len;count++) *Block++=SdRead();
  179.         
  180.         for(;count<512;count++) SdRead();
  181.         
  182.         //data block sent - now send checksum
  183.         SdRead();
  184.         SdRead();
  185.         //Now read in the DATA RESPONSE token
  186.         SD_CS=1;
  187.         SdRead();
  188.         return 1;
  189.     }
  190. //printf("Command 0x11 (Read) was not received by the MMC.\n");
  191.     return 0;
  192. }

  193. /*******************************************
  194.          串口初始化
  195. *******************************************/
  196. void UART()
  197. {
  198.     SCON=0x40;//工作與方式1不允許接受
  199.     TMOD=0x20;//定時器1工作與方式2自動重裝模式
  200.     TH1=RELOAD;
  201.     TR1=1;
  202.     TI=0;   
  203. }
  204. //通過串口發(fā)送一個字符串
  205. void Sen_String(unsigned char *string)
  206. {
  207.     while(*string!='\0')
  208.     {
  209.         if(*string=='\n')
  210.         {
  211.             SBUF=CR;
  212.         }
  213.         else
  214.         {
  215.             SBUF=*string;
  216.         }
  217.         while(TI==0);
  218.         TI=0;
  219.         string++;
  220.     }
  221. }
  222. void main()
  223. {
  224.     UART();
  225.     while(!SdInit()); //等待SD卡初始化完成
  226.     SdWriteBlock("THIS IS A TEST!",0x000000,15);//寫入字符串,然后讀出進(jìn)行檢驗(yàn)
  227.     SdReadBlock(DATA,0x000000,15);
  228.     Sen_String(DATA);        //發(fā)出數(shù)據(jù)
  229.     while(1)
  230.    {
  231.    }
  232. }
復(fù)制代碼




單片機(jī)SD卡讀卡器仿真工程文件及其他完整資料下載:
http://www.torrancerestoration.com/bbs/dpj-54940-1.html


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏12 分享淘帖 頂1 踩

相關(guān)帖子

回復(fù)

使用道具 舉報

沙發(fā)
ID:138115 發(fā)表于 2016-9-1 00:07 | 只看該作者
非常贊

評分

參與人數(shù) 1黑幣 +30 收起 理由
admin + 30

查看全部評分

回復(fù)

使用道具 舉報

板凳
ID:183431 發(fā)表于 2017-3-25 22:46 | 只看該作者
非常感謝,剛好像自己做個sd卡讀卡器呢
回復(fù)

使用道具 舉報

地板
ID:99130 發(fā)表于 2017-4-25 22:34 | 只看該作者
第一個圖右邊的是什么東西 接單片機(jī)TXD那個
回復(fù)

使用道具 舉報

5#
ID:185661 發(fā)表于 2017-4-27 08:50 | 只看該作者
一直在尋找sd卡資料,沒想到在這里找到了,感謝樓主分享
回復(fù)

使用道具 舉報

6#
ID:235656 發(fā)表于 2018-1-2 22:30 來自手機(jī) | 只看該作者
51做的讀卡器讀寫速度怎么樣?
回復(fù)

使用道具 舉報

7#
ID:263813 發(fā)表于 2018-1-3 16:56 | 只看該作者
找資料不易,收藏
回復(fù)

使用道具 舉報

8#
ID:268118 發(fā)表于 2018-1-12 02:31 | 只看該作者
感謝樓主的分享。
回復(fù)

使用道具 舉報

9#
ID:265553 發(fā)表于 2018-1-14 07:38 來自手機(jī) | 只看該作者
正好需要下載了,感謝
回復(fù)

使用道具 舉報

10#
ID:281852 發(fā)表于 2018-2-2 23:15 | 只看該作者
mark下,最近會用到
回復(fù)

使用道具 舉報

11#
ID:79874 發(fā)表于 2018-7-1 05:45 | 只看該作者
非常感謝分享學(xué)習(xí)
回復(fù)

使用道具 舉報

12#
ID:375092 發(fā)表于 2019-3-31 20:50 | 只看該作者
點(diǎn)贊,為你鼓掌
回復(fù)

使用道具 舉報

13#
ID:155507 發(fā)表于 2019-7-7 12:01 | 只看該作者
感謝樓主的分享。
回復(fù)

使用道具 舉報

14#
ID:400715 發(fā)表于 2020-4-17 14:53 | 只看該作者
太有用了
回復(fù)

使用道具 舉報

15#
ID:957712 發(fā)表于 2021-8-17 19:09 | 只看該作者
正好可以學(xué)習(xí),謝謝樓主的分享。
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表