找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 12543|回复: 14
打印 上一主题 下一主题
收起左侧

单片机SD卡读卡器仿真+源程序+电路原理图

  [复制链接]
跳转到指定楼层
楼主

分享一个51单片机做的SD卡读卡器仿真带源程序,一个简单的MMC卡存储器应用。下面是电路原理图:


可以看到虚拟窗口的结果出来了.

单片机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. }
复制代码




单片机SD卡读卡器仿真工程文件及其他完整资料下载:
http://www.51hei.com/bbs/dpj-54940-1.html


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏12 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
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那个
回复

使用道具 举报

5#
ID:185661 发表于 2017-4-27 08:50 | 只看该作者
一直在寻找sd卡资料,没想到在这里找到了,感谢楼主分享
回复

使用道具 举报

6#
ID:235656 发表于 2018-1-2 22:30 来自手机 | 只看该作者
51做的读卡器读写速度怎么样?
回复

使用道具 举报

7#
ID:263813 发表于 2018-1-3 16:56 | 只看该作者
找资料不易,收藏
回复

使用道具 举报

8#
ID:268118 发表于 2018-1-12 02:31 | 只看该作者
感謝樓主的分享。
回复

使用道具 举报

9#
ID:265553 发表于 2018-1-14 07:38 来自手机 | 只看该作者
正好需要下载了,感谢
回复

使用道具 举报

10#
ID:281852 发表于 2018-2-2 23:15 | 只看该作者
mark下,最近会用到
回复

使用道具 举报

11#
ID:79874 发表于 2018-7-1 05:45 | 只看该作者
非常感谢分享学习
回复

使用道具 举报

12#
ID:375092 发表于 2019-3-31 20:50 | 只看该作者
点赞,为你鼓掌
回复

使用道具 举报

13#
ID:155507 发表于 2019-7-7 12:01 | 只看该作者
感謝樓主的分享。
回复

使用道具 举报

14#
ID:400715 发表于 2020-4-17 14:53 | 只看该作者
太有用了
回复

使用道具 举报

15#
ID:957712 发表于 2021-8-17 19:09 | 只看该作者
正好可以学习,谢谢楼主的分享。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表