找回密码
 立即注册

QQ登录

只需一步,快速开始

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

stm32和51单片机读写SD卡的源码与电路图

[复制链接]
跳转到指定楼层
楼主
部分51单片机程序的,源文件里面还有stm32的

电路原理图如下:


单片机源程序:
  1. /*
  2. * SD模块测试程序
  3. *
  4. * 用途:SD模块测试程序
  5. *
  6. * 作者                                        日期                                备注
  7. * Huafeng Lin                        20010/10/03                        新增
  8. * Huafeng Lin                        20010/10/03                        修改
  9. *
  10. */

  11. #include "REG52.H"
  12. ////////////////////////****************/
  13. unsigned char *SDInfo1="SD Init Success.";
  14. unsigned char *SDInfo2="SD Init Fail.";
  15. unsigned int xdata ReadBuffer[128]  ;
  16. unsigned int xdata WriteBuffer[128] ;
  17. unsigned int BlockSize;
  18. unsigned long int BlockNR;

  19. //sbit sd_clk=P3^2;
  20. //sbit sd_cse=P3^0;
  21. //sbit sd_dai=P3^3; //Do
  22. //sbit sd_dao=P3^1;  //DI

  23. sbit sd_cse=P1^0;
  24. sbit sd_dao=P1^1;//DI
  25. sbit sd_clk=P1^2;
  26. sbit sd_dai=P1^3;//Do

  27. void Delay5us()
  28. {
  29.         unsigned char a=0;
  30.         for(a=0;a<40;a++)
  31.         ;
  32. }
  33. //********************************************
  34. void SD_2Byte_Write(unsigned int IOData)
  35. {
  36.         unsigned char BitCounter;
  37.                
  38.         for (BitCounter=0;BitCounter<16;BitCounter++)
  39.         {
  40.                 sd_clk=0;//CLK Low
  41.                
  42.                 if(IOData&0x8000)//If the MSB of IOData is 1, then Do=1, else Do=0.
  43.                         sd_dao=1;//Do High
  44.                 else
  45.                         sd_dao=0;//Do Low
  46.                                 
  47.                 sd_clk=1;//CLK High
  48.                 Delay5us();
  49.                
  50.                 IOData=IOData<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  51.         }
  52. }
  53. //********************************************
  54. void SD_Write(unsigned int IOData)
  55. {
  56.         unsigned char BitCounter;
  57.         IOData=IOData<<8;
  58.         
  59.         for (BitCounter=0;BitCounter<8;BitCounter++)
  60.         {
  61.                 sd_clk=0;//CLK Low
  62.                
  63.                 if(IOData&0x8000)//If the MSB of IOData is 1, then Do=1, else Do=0.
  64.                         sd_dao=1;//Do High
  65.                 else
  66.                         sd_dao=0;//Do Low
  67.                                 
  68.                 sd_clk=1;//CLK High
  69.                 Delay5us();
  70.                
  71.                 IOData=IOData<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  72.         }
  73. }
  74. //********************************************
  75. unsigned int SD_2Byte_Read()
  76. {
  77.         unsigned int Buffer;
  78.         unsigned char BitCounter;
  79.         Buffer=0;
  80.         
  81.         for (BitCounter=0;BitCounter<16;BitCounter++)
  82.         {
  83.                 sd_clk=0;//CLK Low
  84.                 Delay5us();
  85.                 sd_clk=1;//CLK High
  86.                 Buffer=Buffer<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  87.                                  //Because the LSB will be damaged, we can not put this line under next line.
  88.                 if(sd_dai)
  89.                         Buffer++;//If SPI_Din=1 then the LSB_of_Buffer=1.               
  90.         }
  91.         
  92.         return Buffer;
  93. }
  94. //********************************************
  95. unsigned int SD_Read()
  96. {
  97.         unsigned int Buffer;
  98.         unsigned char BitCounter;
  99.         Buffer=0xffff;
  100.         
  101.         for (BitCounter=0;BitCounter<8;BitCounter++)
  102.         {
  103.                 sd_clk=0;//CLK Low
  104.                 Delay5us();
  105.                 sd_clk=1;//CLK High
  106.                 Buffer=Buffer<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  107.                                  //Because the LSB will be damaged, we can not put this line under next line.
  108.                 if(sd_dai)
  109.                         Buffer++;//If SPI_Din=1 then the LSB_of_Buffer=1.               
  110.         }
  111.         
  112.         return Buffer;
  113. }
  114. //********************************************
  115. unsigned int SD_CMD_Write(unsigned int CMDIndex,unsigned long CMDArg,unsigned int ResType,unsigned int CSLowRSV)//ResType:Response Type, send 1 for R1; send 2 for R1b; send 3 for R2.
  116. {        //There are 7 steps need to do.(marked by [1]-[7])
  117.         unsigned int temp,Response,Response2,CRC,MaximumTimes;
  118.         Response2=0;
  119.         MaximumTimes=10;
  120.         CRC=0x0095;//0x0095 is only valid for CMD0
  121.         if (CMDIndex!=0) CRC=0x00ff;
  122.         
  123.         sd_cse=0;//[1] CS Low
  124.         
  125.         SD_2Byte_Write(((CMDIndex|0x0040)<<8)+(CMDArg>>24));//[2] Transmit Command_Index & 1st Byte of Command_Argument.
  126.         SD_2Byte_Write((CMDArg&0x00ffff00)>>8);                                //[2] 2nd & 3rd Byte of Command_Argument
  127.         SD_2Byte_Write(((CMDArg&0x000000ff)<<8)+CRC);                //[2] 4th Byte of Command_Argument & CRC only for CMD0
  128.         
  129.         sd_dao=1;//[3] Do High
  130.                                                 //[3] Restore Do to High Level
  131.         
  132.          for (temp=0;temp<8;temp++)//[4] Provide 8 extra clock after CMD
  133.         {
  134.                 sd_clk=0;//CLK Low
  135.                 Delay5us();
  136.                 sd_clk=1;//CLK High
  137.                 Delay5us();
  138.         }
  139.         
  140.         switch (ResType)//[5] wait response
  141.         {
  142.                 case 1://R1
  143.                                 {
  144.                                         do
  145.                                                 Response=SD_Read();
  146.                                         while (Response==0xffff);
  147.                                         break;
  148.                                 }
  149.                 case 2://R1b
  150.                                 {
  151.                                         do
  152.                                                 Response=SD_Read();
  153.                                         while (Response==0xffff);//Read R1 firstly
  154.                                        
  155.                                         do
  156.                                                 Response2=SD_Read()-0xff00;
  157.                                         while (Response2!=0);//Wait until the Busy_Signal_Token is non-zero
  158.                                         break;        
  159.                                 }
  160.                 case 3: Response=SD_2Byte_Read();break;//R2
  161.         }
  162.         
  163.         if (CSLowRSV==0) sd_cse=1;//[6] CS High (if the CMD has data block response CS should be kept low)
  164.          
  165.          for (temp=0;temp<8;temp++)//[7] Provide 8 extra clock after card response
  166.         {
  167.                 sd_clk=0;//CLK Low
  168.                 Delay5us();
  169.                 sd_clk=1;//CLK High
  170.                 Delay5us();
  171.         }
  172.         return Response;
  173. }
  174. //********************************************
  175. unsigned int SD_Reset_Card()
  176. {
  177.         unsigned int temp,MaximumTimes;
  178.         MaximumTimes=10;
  179.         
  180.         for (temp=0;temp<80;temp++)//Send 74+ Clocks
  181.         {
  182.                 sd_clk=0;//CLK Low
  183.                 Delay5us();
  184.                 sd_clk=1;//CLK High
  185.                 Delay5us();
  186.         }
  187.                
  188.         return SD_CMD_Write(0x0000,0x00000000,1,0);//Send CMD0
  189. }
  190. //********************************************
  191. unsigned int SD_Initiate_Card()//Polling the card after reset
  192. {
  193.         unsigned int temp,Response,MaximumTimes;
  194.         MaximumTimes=50;
  195.         
  196.         for(temp=0;temp<MaximumTimes;temp++)
  197.         {
  198.                 Response=SD_CMD_Write(0x0037,0x00000000,1,0);//Send CMD55
  199.                 Response=SD_CMD_Write(0x0029,0x00000000,1,0);//Send ACMD41
  200.                 if (Response==0xff00)
  201.                         temp=MaximumTimes;
  202.         }

  203.         return Response;
  204. }
  205. //********************************************
  206. unsigned int SD_Get_CardInfo()//Read CSD register
  207. {
  208.         unsigned int temp,Response,MaximumTimes;
  209.         MaximumTimes=50;
  210.         
  211.         for(temp=0;temp<MaximumTimes;temp++)
  212.         {
  213.                 Response=SD_CMD_Write(9,0x00000000,1,1);//Send CMD9
  214.                 if (Response==0xff00)
  215.                         temp=MaximumTimes;
  216.         }
  217.         
  218.          for (temp=0;temp<8;temp++)//Provide 8 clock to romove the first byte of data response (0x00fe)
  219.         {
  220.                 sd_clk=0;//CLK Low
  221.                 Delay5us();
  222.                 sd_clk=1;//CLK High
  223.                 Delay5us();
  224.         }
  225.         
  226.         for (temp=0;temp<8;temp++) ReadBuffer[temp]=SD_2Byte_Read();//Get the CSD data
  227.         
  228.         for (temp=0;temp<16;temp++)//Provide 16 clock to remove the last 2 bytes of data response (CRC)
  229.         {
  230.                 sd_clk=0;//CLK Low
  231.                 Delay5us();
  232.                 sd_clk=1;//CLK High
  233.                 Delay5us();
  234.         }
  235.         
  236.         sd_cse=1;//CS_High()
  237.         
  238.         for (temp=0;temp<8;temp++)//Provide 8 extra clock after data response
  239.         {
  240.                 sd_clk=0;//CLK Low
  241.                 Delay5us();
  242.                 sd_clk=1;//CLK High
  243.                 Delay5us();
  244.         }
  245.         
  246.         BlockNR=((ReadBuffer[3]<<2)&0x0fff)+((ReadBuffer[4]>>14)&0x0003)+1;//Calcuate MULT
  247.         BlockNR=BlockNR*(0x0002<<(((ReadBuffer[4]<<1)&0x0007)+((ReadBuffer[5]>>15)&0x0001)+1));//Calcuate Block_Number
  248.         return Response;
  249. }
  250. //********************************************
  251. unsigned int SD_Overall_Initiation()
  252. {
  253.         unsigned int Response,Response_2;
  254.         Response=0x0000;
  255.         Response_2=0xff00;
  256.         
  257.         sd_dao=1;//[1] Do High
  258.                                                 //[1] Do must be High when there is no transmition
  259.         do
  260.                 Response=SD_Reset_Card();//[2] Send CMD0
  261.         while (Response!=0xff01);
  262.         
  263.         if (Response!=0xff01) Response_2+=8;
  264.         
  265.         //Response=SD_CMD_Write(8,0x00000000,1,0);//Send CMD8
  266.         
  267.         Response=SD_Initiate_Card();//[3] Send CMD55+ACMD41
  268.         if (Response==0xff00)
  269.                 ;
  270.         else
  271.                 {
  272.                 Response_2+=4;
  273.                 ;
  274.                 }
  275.         
  276.         do
  277.                 Response=SD_Get_CardInfo();//[4] Read CSD
  278.         while (Response!=0xff00);
  279.         if (Response==0xff01) Response_2+=2;
  280.         
  281.         return Response_2;
  282. //        0000|0000||0000|0000 Response_2
  283. //                  |||_CSD Fail
  284. //                  ||__CMD55+ACMD41 Fail
  285. //                  |___CMD0 Fail
  286. }
  287. //********************************************
复制代码

程序.rar

1.69 MB, 下载次数: 100, 下载积分: 黑币 -5

SD卡原理图.pdf

148.74 KB, 下载次数: 75, 下载积分: 黑币 -5

评分

参与人数 2黑币 +62 收起 理由
catlu + 12
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:1042569 发表于 2022-9-2 22:21 | 只看该作者
请问你那个51单片机读sd卡的那个程序电路图怎么接???
回复

使用道具 举报

板凳
ID:513213 发表于 2022-9-3 18:02 | 只看该作者
SD卡的元件能一起提供吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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