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

📄 sd.lst

📁 单片机的实用程序代码
💻 LST
字号:
C51 COMPILER V7.50   SD                                                                    08/15/2010 13:32:15 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE SD
OBJECT MODULE PLACED IN sd.OBJ
COMPILER INVOKED BY: D:\Program Files\KEIL\C51\BIN\C51.EXE sd.c DEBUG OBJECTEXTEND

line level    source

   1          /*-----------------------------------------------
   2          
   3          ------------------------------------------------*/
   4          #include <reg52.h>
   5          #include <intrins.h>
   6          #include <stdio.h>
   7          #include<9325TP.h>
   8          //=============================================================
   9          //定义SD卡需要的4根信号线
  10          sbit SD_CLK = P1^1;
  11          sbit SD_DI  = P1^2;
  12          sbit SD_DO  = P1^0;
  13          sbit SD_CS  = P1^3;
  14          //===========================================================
  15          //定义按键端口
  16          sbit KEY = P3^2;
  17          //===========================================================
  18          //定义512字节缓冲区,注意需要使用 xdata关键字
  19          unsigned char xdata DATA[75]={0};
  20          unsigned char xdata DATA1[75]={0};
  21          
  22          //===========================================================
  23          //写一字节到SD卡,模拟SPI总线方式
  24          void SdWrite(unsigned char n)
  25          {
  26   1      
  27   1      unsigned char i;
  28   1      
  29   1      for(i=8;i;i--)
  30   1      {
  31   2      SD_CLK=0;
  32   2      SD_DI=(n&0x80);
  33   2      n<<=1;
  34   2      SD_CLK=1;
  35   2      }
  36   1      SD_DI=1; 
  37   1      } 
  38          //===========================================================
  39          //从SD卡读一字节,模拟SPI总线方式
  40          unsigned char SdRead()
  41          {
  42   1      unsigned char n,i;
  43   1      for(i=8;i;i--)
  44   1      {
  45   2      SD_CLK=0;
  46   2      SD_CLK=1;
  47   2      n<<=1;
  48   2      if(SD_DO) n|=1;
  49   2      
  50   2      }
  51   1      return n;
  52   1      }
  53          //============================================================
  54          //检测SD卡的响应
  55          unsigned char SdResponse()
C51 COMPILER V7.50   SD                                                                    08/15/2010 13:32:15 PAGE 2   

  56          {
  57   1      unsigned char i=0,response;
  58   1      
  59   1      while(i<=8)
  60   1      {
  61   2      response = SdRead();
  62   2      if(response==0x00)
  63   2      break;
  64   2      if(response==0x01)
  65   2      break;
  66   2      i++;
  67   2      }
  68   1      return response;
  69   1      } 
  70          //================================================================
  71          //发命令到SD卡
  72          void SdCommand(unsigned char command, unsigned long argument, unsigned char CRC)
  73          {
  74   1      
  75   1      SdWrite(command|0x40);
  76   1      SdWrite(((unsigned char *)&argument)[0]);
  77   1      SdWrite(((unsigned char *)&argument)[1]);
  78   1      SdWrite(((unsigned char *)&argument)[2]);
  79   1      SdWrite(((unsigned char *)&argument)[3]);
  80   1      SdWrite(CRC);
  81   1      }
  82          //================================================================
  83          //初始化SD卡
  84          unsigned char SdInit(void)
  85          {
  86   1      int delay=0, trials=0;
  87   1      unsigned char i;
  88   1      unsigned char response=0x01;
  89   1      
  90   1      SD_CS=1;
  91   1      for(i=0;i<=9;i++)
  92   1      SdWrite(0xff);
  93   1      SD_CS=0;
  94   1      
  95   1      //Send Command 0 to put MMC in SPI mode
  96   1      SdCommand(0x00,0,0x95);
  97   1      
  98   1      
  99   1      response=SdResponse();
 100   1      
 101   1      if(response!=0x01)
 102   1      {
 103   2      return 0;
 104   2      } 
 105   1      
 106   1      while(response==0x01)
 107   1      {
 108   2      SD_CS=1;
 109   2      SdWrite(0xff);
 110   2      SD_CS=0;
 111   2      SdCommand(0x01,0x00ffc000,0xff);
 112   2      response=SdResponse();
 113   2      } 
 114   1      
 115   1      SD_CS=1;
 116   1      SdWrite(0xff);
 117   1      return 1; 
C51 COMPILER V7.50   SD                                                                    08/15/2010 13:32:15 PAGE 3   

 118   1      }
 119          //================================================================
 120          //往SD卡指定地址写数据,一次最多512字节
 121          unsigned char SdWriteBlock(unsigned char *Block, unsigned long address,int len)
 122          {
 123   1      unsigned int count;
 124   1      unsigned char dataResp;
 125   1      //Block size is 512 bytes exactly
 126   1      //First Lower SS
 127   1      
 128   1      SD_CS=0;
 129   1      //Then send write command
 130   1      SdCommand(0x18,address,0xff);
 131   1      
 132   1      if(SdResponse()==00)
 133   1      {
 134   2      SdWrite(0xff);
 135   2      SdWrite(0xff);
 136   2      SdWrite(0xff);
 137   2      //command was a success - now send data
 138   2      //start with DATA TOKEN = 0xFE
 139   2      SdWrite(0xfe);
 140   2      //now send data
 141   2      for(count=0;count<len;count++) SdWrite(*Block++);
 142   2      
 143   2      for(;count<512;count++) SdWrite(0);
 144   2      //data block sent - now send checksum
 145   2      SdWrite(0xff); //两字节CRC校验, 为0XFFFF 表示不考虑CRC
 146   2      SdWrite(0xff);
 147   2      //Now read in the DATA RESPONSE token
 148   2      dataResp=SdRead();
 149   2      //Following the DATA RESPONSE token
 150   2      //are a number of BUSY bytes
 151   2      //a zero byte indicates the MMC is busy
 152   2      
 153   2      while(SdRead()==0);
 154   2      
 155   2      dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token
 156   2      SD_CS=1;
 157   2      SdWrite(0xff);
 158   2      if(dataResp==0x0b)
 159   2      {
 160   3      //printf("DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR\n");
 161   3      return 0;
 162   3      }
 163   2      if(dataResp==0x05)
 164   2      return 1;
 165   2      
 166   2      //printf("Invalid data Response token.\n");
 167   2      return 0;
 168   2      }
 169   1      //printf("Command 0x18 (Write) was not received by the MMC.\n");
 170   1      return 0;
 171   1      }
 172          
 173          //=======================================================================
 174          //从SD卡指定地址读取数据,一次最多512字节
 175          unsigned char SdReadBlock(unsigned char *Block, unsigned long address,int len)
 176          {
 177   1      unsigned int count;
 178   1      //Block size is 512 bytes exactly
 179   1      //First Lower SS
C51 COMPILER V7.50   SD                                                                    08/15/2010 13:32:15 PAGE 4   

 180   1      
 181   1       //printf("MMC_read_block\n");
 182   1      
 183   1      SD_CS=0;
 184   1      //Then send write command
 185   1      SdCommand(0x11,address,0xff);
 186   1      
 187   1      if(SdResponse()==00)
 188   1      {
 189   2      //command was a success - now send data
 190   2      //start with DATA TOKEN = 0xFE
 191   2      while(SdRead()!=0xfe);
 192   2      
 193   2      for(count=0;count<len;count++) *Block++=SdRead(); 
 194   2      
 195   2      for(;count<512;count++) SdRead();
 196   2      
 197   2      //data block sent - now send checksum
 198   2      SdRead();
 199   2      SdRead();
 200   2      //Now read in the DATA RESPONSE token
 201   2      SD_CS=1;
 202   2      SdRead();
 203   2      return 1;
 204   2      }
 205   1       //printf("Command 0x11 (Read) was not received by the MMC.\n");
 206   1      return 0;
 207   1      }
 208          /********************************************************************
 209          * 名称 : Com_Init()
 210          * 功能 : 初始化串口程序,晶振11.0592, 波特率9600
 211          * 输入 : 无
 212          * 输出 : 无
 213          ***********************************************************************/
 214          void Com_Init(void)
 215          {
 216   1           TMOD = 0x20;
 217   1           PCON = 0x00;
 218   1           SCON = 0x50;                       
 219   1           TH1 = 0xFd;
 220   1           TL1 = 0xFd;
 221   1           TR1 = 1;                   
 222   1      }
 223          //============================================================
 224          //主程序        
 225          main()
 226          {
 227   1      
 228   1        unsigned long AddTemp=262144;//SD卡地址第一个数据物理地址初始值,可以用winhex查看,这里是512扇区,512x51
             -2=262144,根据实际SD卡内容更改
 229   1      unsigned char i;
 230   1      unsigned char *p;
 231   1              CS=1;
 232   1              delayms(5);
 233   1              RES=0;
 234   1              delayms(5);
 235   1              RES=1;
 236   1              delayms(5);
 237   1              SdInit();         //SD卡初始化
 238   1              Com_Init();
 239   1              for(i=0;i<75;i++)
 240   1              {
C51 COMPILER V7.50   SD                                                                    08/15/2010 13:32:15 PAGE 5   

 241   2                      DATA1[i]=i;
 242   2              }       
 243   1      
 244   1                      SdWriteBlock(DATA1, AddTemp, 75);
 245   1              SdReadBlock(DATA, AddTemp, 75);
 246   1                      p= DATA; 
 247   1      while(1)                
 248   1      for(i=0;i<75;i++)
 249   1              {
 250   2                      SBUF = *p+48; 
 251   2                      while(!TI)                   //如果发送完毕,硬件会置位TI
 252   2                      {
 253   3                              _nop_();        
 254   3                      }
 255   2                      p++;
 256   2                      TI = 0;                         //TI清零
 257   2                      delayms(500);
 258   2              }
 259   1      }
 260          
 261          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1335    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =    150    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      52
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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