⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sd.lst

📁 51单片机读写SD列子程序 可以学习51单片机如何读写SD卡
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE SD
OBJECT MODULE PLACED IN SD.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE SD.C LARGE OPTIMIZE(6,SPEED) BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include "REG52.H"
   2          ////////////////////////****************/
   3          unsigned char *SDInfo1="SD Init Success.";
   4          unsigned char *SDInfo2="SD Init Fail.";
   5          unsigned int ReadBuffer[256];
   6          unsigned int WriteBuffer[256]={0x1122,0x3344};
   7          unsigned int BlockSize;
   8          unsigned long int BlockNR;
   9          sbit sd_clk=P1^5;
  10          sbit sd_cse=P1^3;
  11          sbit sd_dai=P1^0;
  12          sbit sd_dao=P1^4;
  13          void Delay5us()
  14          {
  15   1        unsigned char a=0;
  16   1      for(a=0;a<40;a++)
  17   1      ;
  18   1      }
  19          //********************************************
  20          void SD_2Byte_Write(unsigned int IOData)
  21          {
  22   1              unsigned int BitCounter;
  23   1                      
  24   1              for (BitCounter=0;BitCounter<16;BitCounter++)
  25   1              {
  26   2                      sd_clk=0;//CLK Low
  27   2                      
  28   2                      if(IOData&0x8000)//If the MSB of IOData is 1, then Do=1, else Do=0.
  29   2                              sd_dao=1;//Do High
  30   2                      else
  31   2                              sd_dao=0;//Do Low
  32   2                                      
  33   2                      sd_clk=1;//CLK High
  34   2                      Delay5us();
  35   2                      
  36   2                      IOData=IOData<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  37   2              }
  38   1      }
  39          //********************************************
  40          void SD_Write(unsigned int IOData)
  41          {
  42   1              unsigned int BitCounter;
  43   1              IOData=IOData<<8;
  44   1              
  45   1              for (BitCounter=0;BitCounter<8;BitCounter++)
  46   1              {
  47   2                      sd_clk=0;//CLK Low
  48   2                      
  49   2                      if(IOData&0x8000)//If the MSB of IOData is 1, then Do=1, else Do=0.
  50   2                              sd_dao=1;//Do High
  51   2                      else
  52   2                              sd_dao=0;//Do Low
  53   2                                      
  54   2                      sd_clk=1;//CLK High
  55   2                      Delay5us();
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 2   

  56   2                      
  57   2                      IOData=IOData<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  58   2              }
  59   1      }
  60          //********************************************
  61          unsigned int SD_2Byte_Read()
  62          {
  63   1              unsigned int BitCounter,Buffer;
  64   1              Buffer=0;
  65   1              
  66   1              for (BitCounter=0;BitCounter<16;BitCounter++)
  67   1              {
  68   2                      sd_clk=0;//CLK Low
  69   2                      Delay5us();
  70   2                      sd_clk=1;//CLK High
  71   2                      Buffer=Buffer<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  72   2                                       //Because the LSB will be damaged, we can not put this line under next line.
  73   2                      if(sd_dai) 
  74   2                              Buffer++;//If SPI_Din=1 then the LSB_of_Buffer=1.               
  75   2              }
  76   1              
  77   1              return Buffer;
  78   1      }
  79          //********************************************
  80          unsigned int SD_Read()
  81          {
  82   1              unsigned int BitCounter,Buffer;
  83   1              Buffer=0xffff;
  84   1              
  85   1              for (BitCounter=0;BitCounter<8;BitCounter++)
  86   1              {
  87   2                      sd_clk=0;//CLK Low
  88   2                      Delay5us();
  89   2                      sd_clk=1;//CLK High
  90   2                      Buffer=Buffer<<1;//Because the MSB is transmitted firstly, shift to next lower bit.
  91   2                                       //Because the LSB will be damaged, we can not put this line under next line.
  92   2                      if(sd_dai) 
  93   2                              Buffer++;//If SPI_Din=1 then the LSB_of_Buffer=1.               
  94   2              }
  95   1              
  96   1              return Buffer;
  97   1      }
  98          //********************************************
  99          unsigned int SD_CMD_Write(unsigned int CMDIndex,unsigned long CMDArg,unsigned int ResType,unsigned int CSL
             -owRSV)//ResType:Response Type, send 1 for R1; send 2 for R1b; send 3 for R2.
 100          {       //There are 7 steps need to do.(marked by [1]-[7])
 101   1              unsigned int temp,Response,Response2,CRC,MaximumTimes;
 102   1              Response2=0;
 103   1              MaximumTimes=10;
 104   1              CRC=0x0095;//0x0095 is only valid for CMD0
 105   1              if (CMDIndex!=0) CRC=0x00ff;
 106   1              
 107   1              sd_cse=0;//[1] CS Low
 108   1              
 109   1              SD_2Byte_Write(((CMDIndex|0x0040)<<8)+(CMDArg>>24));//[2] Transmit Command_Index & 1st Byte of Command_Ar
             -gument.
 110   1              SD_2Byte_Write((CMDArg&0x00ffff00)>>8);                         //[2] 2nd & 3rd Byte of Command_Argument
 111   1              SD_2Byte_Write(((CMDArg&0x000000ff)<<8)+CRC);           //[2] 4th Byte of Command_Argument & CRC only for CMD0
 112   1              
 113   1              sd_dao=1;//[3] Do High
 114   1                                                      //[3] Restore Do to High Level
 115   1              
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 3   

 116   1               for (temp=0;temp<8;temp++)//[4] Provide 8 extra clock after CMD
 117   1              {
 118   2                      sd_clk=0;//CLK Low
 119   2                      Delay5us();
 120   2                      sd_clk=1;//CLK High
 121   2                      Delay5us();
 122   2              }
 123   1              
 124   1              switch (ResType)//[5] wait response
 125   1              {
 126   2                      case 1://R1
 127   2                                      {
 128   3                                              do
 129   3                                                      Response=SD_Read();
 130   3                                              while (Response==0xffff);
 131   3                                              break;
 132   3                                      }
 133   2                      case 2://R1b
 134   2                                      {
 135   3                                              do
 136   3                                                      Response=SD_Read();
 137   3                                              while (Response==0xffff);//Read R1 firstly
 138   3                                              
 139   3                                              do
 140   3                                                      Response2=SD_Read()-0xff00;
 141   3                                              while (Response2!=0);//Wait until the Busy_Signal_Token is non-zero
 142   3                                              break;  
 143   3                                      }
 144   2                      case 3: Response=SD_2Byte_Read();break;//R2
 145   2              }
 146   1              
 147   1              if (CSLowRSV==0) sd_cse=1;//[6] CS High (if the CMD has data block response CS should be kept low)
 148   1               
 149   1               for (temp=0;temp<8;temp++)//[7] Provide 8 extra clock after card response
 150   1              {
 151   2                      sd_clk=0;//CLK Low
 152   2                      Delay5us();
 153   2                      sd_clk=1;//CLK High
 154   2                      Delay5us();
 155   2              }
 156   1              return Response;
 157   1      }
 158          //********************************************
 159          unsigned int SD_Reset_Card()
 160          {
 161   1              unsigned int temp,MaximumTimes;
 162   1              MaximumTimes=10;
 163   1              
 164   1              for (temp=0;temp<80;temp++)//Send 74+ Clocks
 165   1              {
 166   2                      sd_clk=0;//CLK Low
 167   2                      Delay5us();
 168   2                      sd_clk=1;//CLK High
 169   2                      Delay5us();
 170   2              }
 171   1                      
 172   1              return SD_CMD_Write(0x0000,0x00000000,1,0);//Send CMD0
 173   1      }
 174          //********************************************
 175          unsigned int SD_Initiate_Card()//Polling the card after reset
 176          {
 177   1              unsigned int temp,Response,MaximumTimes;
C51 COMPILER V7.50   SD                                                                    07/20/2008 10:05:07 PAGE 4   

 178   1              MaximumTimes=50;
 179   1              
 180   1              for(temp=0;temp<MaximumTimes;temp++)
 181   1              {
 182   2                      Response=SD_CMD_Write(0x0037,0x00000000,1,0);//Send CMD55
 183   2                      Response=SD_CMD_Write(0x0029,0x00000000,1,0);//Send ACMD41
 184   2                      if (Response==0xff00)
 185   2                              temp=MaximumTimes;
 186   2              }
 187   1      
 188   1              return Response;
 189   1      }
 190          //********************************************
 191          unsigned int SD_Get_CardInfo()//Read CSD register
 192          {
 193   1              unsigned int temp,Response,MaximumTimes;
 194   1              MaximumTimes=50;
 195   1              
 196   1              for(temp=0;temp<MaximumTimes;temp++)
 197   1              {
 198   2                      Response=SD_CMD_Write(9,0x00000000,1,1);//Send CMD9
 199   2                      if (Response==0xff00)
 200   2                              temp=MaximumTimes;
 201   2              }
 202   1              
 203   1               for (temp=0;temp<8;temp++)//Provide 8 clock to romove the first byte of data response (0x00fe)
 204   1              {
 205   2                      sd_clk=0;//CLK Low
 206   2                      Delay5us();
 207   2                      sd_clk=1;//CLK High

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -