久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 14141|回復: 14
收起左側

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

  [復制鏈接]
ID:137190 發表于 2016-8-31 23:05 | 顯示全部樓層 |閱讀模式

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

可以看到虛擬窗口的結果出來了.

單片機SD卡讀卡器設計的源程序如下:
  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字節緩沖區,注意需要使用 xdata關鍵字


  13. //===========================================================
  14. //寫一字節到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卡讀一字節,模擬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卡的響應
  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. //發命令到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卡指定地址寫數據,一次最多512字節
  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); //兩字節CRC校驗, 為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卡指定地址讀取數據,一次最多512字節
  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. //通過串口發送一個字符串
  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);//寫入字符串,然后讀出進行檢驗
  227.     SdReadBlock(DATA,0x000000,15);
  228.     Sen_String(DATA);        //發出數據
  229.     while(1)
  230.    {
  231.    }
  232. }
復制代碼


0.png

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


回復

使用道具 舉報

ID:138115 發表于 2016-9-1 00:07 | 顯示全部樓層
非常贊

評分

參與人數 1黑幣 +30 收起 理由
admin + 30

查看全部評分

回復

使用道具 舉報

ID:183431 發表于 2017-3-25 22:46 | 顯示全部樓層
非常感謝,剛好像自己做個sd卡讀卡器呢
回復

使用道具 舉報

ID:99130 發表于 2017-4-25 22:34 | 顯示全部樓層
第一個圖右邊的是什么東西 接單片機TXD那個
回復

使用道具 舉報

ID:185661 發表于 2017-4-27 08:50 | 顯示全部樓層
一直在尋找sd卡資料,沒想到在這里找到了,感謝樓主分享
回復

使用道具 舉報

ID:235656 發表于 2018-1-2 22:30 來自手機 | 顯示全部樓層
51做的讀卡器讀寫速度怎么樣?
回復

使用道具 舉報

ID:263813 發表于 2018-1-3 16:56 | 顯示全部樓層
找資料不易,收藏
回復

使用道具 舉報

ID:268118 發表于 2018-1-12 02:31 | 顯示全部樓層
感謝樓主的分享。
回復

使用道具 舉報

ID:265553 發表于 2018-1-14 07:38 來自手機 | 顯示全部樓層
正好需要下載了,感謝
回復

使用道具 舉報

ID:281852 發表于 2018-2-2 23:15 | 顯示全部樓層
mark下,最近會用到
回復

使用道具 舉報

ID:79874 發表于 2018-7-1 05:45 | 顯示全部樓層
非常感謝分享學習
回復

使用道具 舉報

ID:375092 發表于 2019-3-31 20:50 | 顯示全部樓層
點贊,為你鼓掌
回復

使用道具 舉報

ID:155507 發表于 2019-7-7 12:01 | 顯示全部樓層
感謝樓主的分享。
回復

使用道具 舉報

ID:400715 發表于 2020-4-17 14:53 | 顯示全部樓層
太有用了
回復

使用道具 舉報

ID:957712 發表于 2021-8-17 19:09 | 顯示全部樓層
正好可以學習,謝謝樓主的分享。
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本欧美国产在线观看 | 激情五月综合 | 欧美一级视频在线观看 | 欧美日韩在线国产 | 美国一级片在线观看 | 99久久婷婷 | 91在线资源| 欧美久久视频 | 精品美女 | 久久久91| 毛片网站在线观看视频 | 成人影院免费视频 | 国产精品久久久久久久久久免费 | 亚洲成人在线免费 | 欧美一区二区免费 | 国产成人精品综合 | 久久婷婷色 | 亚洲精品视频一区 | 亚洲电影中文字幕 | 黄色毛片大全 | 久久久久久毛片免费观看 | 成人免费视频7777777 | 亚洲最大福利网 | 97国产精品 | 自拍第一页 | 亚洲天堂中文字幕 | 91福利网址 | 亚洲在线 | 亚洲精品一区二区三区蜜桃久 | 日韩一区二区视频 | 亚洲视频在线免费观看 | 岛国视频 | 特黄色一级毛片 | 国产精品日日做人人爱 | 天堂视频中文在线 | 国产九九九九 | 99久久免费精品 | 91精品国产91久久久久青草 | 午夜影院在线观看 | 久久蜜桃资源一区二区老牛 | 久久久久久久综合色一本 |