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

📄 at45db.lst

📁 SM858单片机完成AT45DB内部数据读写程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.50   AT45DB                                                                05/14/2008 18:26:14 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE AT45DB
OBJECT MODULE PLACED IN AT45DB.OBJ
COMPILER INVOKED BY: E:\keil\C51\BIN\C51.EXE AT45DB.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /******************************************************************************/ 
   2          /*正常操作电压为2.7~3.6V,实验中发现当电压超过4.25V后读出的状态字节为9A(正常 */ 
   3          /*的状态字节值为9D),并且读写数据均不准确,所以应当保证卡片的供电电压不超过  */ 
   4          /*4.25V。                                                                     */ 
   5          /*SPI规范:Data is always clocked into the device on the rising edge of SCK a-*/ 
   6          /*    nd clocked out of the device on the falling edge of SCK.All instruction-*/ 
   7          /*    s,addresses and data are transferred with the most significant bit(MSB) */ 
   8          /*    first.                                                                  */ 
   9          /*                                                                  2005-06-02*/ 
  10          /******************************************************************************/ 
  11          
  12          #pragma    optimize(5)
  13          #include   <reg52.h> 
  14          #include   <string.h> 
  15          #include"charcode.h"
  16          #define    UCHAR                      unsigned char 
  17          #define    UINT                       unsigned int 
  18          
  19          sbit       SPI_CS                   = P0^1; 
  20          sbit       SPI_SCK                  = P0^3; 
  21          sbit       SPI_SO                   = P2^3; 
  22          sbit       SPI_SI                   = P0^4; 
  23          sbit       SPI_RES                  = P0^2;
  24          sbit       SPI_WP                   = P0^0;
  25          sbit       SPI_AY1                  = P2^4;
  26          sbit       SPI_AY2                  = P2^5;
  27          sbit       SPI_AY3                  = P2^6;
  28          
  29          
  30          
  31          
  32          
  33          static void delay(unsigned int s)
  34          {
  35   1              unsigned int i;
  36   1              for(i=0; i<s; i++);
  37   1              for(i=0; i<s; i++);
  38   1      }
  39          
  40          
  41          void init_serial(void) 
  42          
  43          {
  44   1                                      //定时器1的工作方式2
  45   1              TMOD=0x20;      //装载计数初值
  46   1              TL1=0xfb;
  47   1              TH1=0xfb;       //采用串口工作方式1,无奇偶校验
  48   1              SCON=0x50;      //串口波特率不加倍
  49   1              PCON=0x00;      //开总中断,开串口中断
  50   1              TCON=0x01;  //中断设置
  51   1              IE=0x90;
  52   1                      //启动定时器1
  53   1              TR1=1;
  54   1      }
  55          
C51 COMPILER V7.50   AT45DB                                                                05/14/2008 18:26:14 PAGE 2   

  56          
  57          void SendCh(unsigned char c)
  58          {EA=0;
  59   1       TI=0;
  60   1       SBUF=c;
  61   1       while(!TI);
  62   1       TI=0;
  63   1       EA=1;
  64   1      }
  65          
  66          
  67          unsigned char SPI_HostReadByte(void){ 
  68   1          unsigned char i,rByte=0; 
  69   1           
  70   1          for(i=0;i<8;i++){ 
  71   2              SPI_SCK=0; 
  72   2              SPI_SCK=1; 
  73   2               
  74   2              rByte<<=1; 
  75   2              rByte|=SPI_SO; 
  76   2          } 
  77   1          return rByte;     
  78   1      } 
  79          void SPI_HostWriteByte(unsigned char wByte){ 
  80   1          unsigned char i; 
  81   1           
  82   1          for(i=0;i<8;i++){ 
  83   2              if((wByte<<i)&0x80){SPI_SI=1;} 
  84   2              else{SPI_SI=0;} 
  85   2               
  86   2              SPI_SCK=0; 
  87   2              SPI_SCK=1; 
  88   2          }     
  89   1      } 
  90          /******************************************************************************/ 
  91          /*Status Register Format:                                                     */ 
  92          /*   -----------------------------------------------------------------------  */ 
  93          /*  |  bit7  |  bit6  |  bit5  |  bit4  |  bit3  |  bit2  |  bit1  |  bit0  | */ 
  94          /*  |--------|--------|--------|--------|--------|--------|--------|--------| */ 
  95          /*  |RDY/BUSY|  COMP  |   0    |   1    |   1    |   1    |   X    |   X    | */ 
  96          /*   -----------------------------------------------------------------------  */ 
  97          /*  bit7 - 忙标记,0为忙1为不忙。                                             */ 
  98          /*         当Status Register的位0移出之后,接下来的时钟脉冲序列将使SPI器件继续*/ 
  99          /*         将最新的状态字节送出。                                             */ 
 100          /*  bit6 - 标记最近一次Main Memory Page和Buffer的比较结果,0相同,1不同。     */ 
 101          /*  bit5                                                                      */ 
 102          /*  bit4                                                                      */ 
 103          /*  bit3                                                                      */ 
 104          /*  bit2 - 这4位用来标记器件密度,对于AT45DB041B,这4位应该是0111,一共能标记 */ 
 105          /*         16种不同密度的器件。                                               */ 
 106          /*  bit1                                                                      */ 
 107          /*  bit0 - 这2位暂时无效                                                      */ 
 108          /******************************************************************************/ 
 109          unsigned char AT45DB041B_StatusRegisterRead(void){ 
 110   1          unsigned char i; 
 111   1                       
 112   1          SPI_CS=0;     
 113   1          SPI_HostWriteByte(0xd7); 
 114   1          i=SPI_HostReadByte(); 
 115   1          SPI_CS=1; 
 116   1           
 117   1          return i;     
C51 COMPILER V7.50   AT45DB                                                                05/14/2008 18:26:14 PAGE 3   

 118   1      } 
 119          /******************************************************************************/ 
 120          /*描述:                                                                      */ 
 121          /*    When the last bit in the main memory array has been read,the device will*/ 
 122          /*    continue reading back at the beginning of the first page of memory.As w-*/ 
 123          /*    ith crossing over page boundaries,no delays will be incurred when wrapp-*/ 
 124          /*    ing around from the end of the array to the beginning of the array.     */ 
 125          /*参数:                                                                      */ 
 126          /*    PA      - 页地址,0~2047                                                */ 
 127          /*    BFA     - 指定BUFFER中的起始写入地址                                    */ 
 128          /*    pHeader - 指定数据的首地址                                              */ 
 129          /*    len     - 指定数据的长度                                                */ 
 130          /******************************************************************************/ 
 131          void AT45DB041B_ContinuousArrayRead(UINT PA,UINT BFA,unsigned char *pHeader,UINT len){        
 132   1          unsigned int i;       
 133   1           
 134   1          while(i++<255){if(AT45DB041B_StatusRegisterRead()&0x80){break;}} 
 135   1          SPI_CS=0;     
 136   1          SPI_HostWriteByte(0xe8);     
 137   1          SPI_HostWriteByte((unsigned char)(PA>>7));     
 138   1          SPI_HostWriteByte((unsigned char)((PA<<1)|(BFA>>8))); 
 139   1          SPI_HostWriteByte((unsigned char)BFA); 
 140   1          for(i=0;i<4;i++){SPI_HostWriteByte(0x00);} 
 141   1           
 142   1          for(i=0;i<len;i++){pHeader[i]=SPI_HostReadByte();} 
 143   1          SPI_CS=1; 
 144   1      } 
 145          /******************************************************************************/ 
 146          /*描述:                                                                      */ 
 147          /*    将指定数据读出从某个地址(0~263)开始的BUFFER中。                       */ 
 148          /*参数:                                                                      */ 
 149          /*    buffer  - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2                  */ 
 150          /*              在该指令序列中,操作码d4H选择BUFFER 1,d6H选择BUFFER 2        */ 
 151          /*    BFA     - BUFFER中的起始地址,0~263                                     */ 
 152          /*    pHeader - 待存数据的头指针                                              */ 
 153          /*    len     - 待存数据的长度1~264                                           */ 
 154          /******************************************************************************/ 
 155          void AT45DB041B_BufferRead(UCHAR buffer,UINT BFA,UCHAR *pHeader,UINT len){ 
 156   1          unsigned int i; 
 157   1           
 158   1          while(i++<255){if(AT45DB041B_StatusRegisterRead()&0x80){break;}} 
 159   1          SPI_CS=0;     
 160   1          switch(buffer){ 

⌨️ 快捷键说明

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