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

📄 flash.lst

📁 实现U盘功能的单片机程序,单片机为C8051F340
💻 LST
字号:
C51 COMPILER V7.20   FLASH                                                                 06/23/2008 19:39:41 PAGE 1   


C51 COMPILER V7.20, COMPILATION OF MODULE FLASH
OBJECT MODULE PLACED IN Flash.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\c51.exe Flash.c DB OE

line level    source

   1          //头文件
   2          /////////////////////////////
   3          #include "c8051F340.h"
   4          #include "stdio.h"
   5          ////////////////////////////////////////////////////////////////////////////////////////////////////
   6          //位定义
   7          sbit FLASH_WP   =P3^0;  //命令锁存
   8          sbit FLASH_WE   =P3^1;  //地址锁存
   9          sbit FLASH_ALE  =P3^2;  //写保护
  10          sbit FLASH_CLE  =P3^3;  //片选
  11          sbit FLASH_CE   =P3^7;  //写使能
  12          sbit FLASH_RE   =P3^4;  //读使能
  13          sbit FLASH_RB   =P3^5;  //忙指示
  14          sbit FLASH_SE   =P3^6;
  15          
  16          //P4口为数据口
  17          //注释掉的函数在需要时可以取掉注释即可
  18          ///////////////////////////////////////////////////////////////////////////////////////////////////
  19          //FLASH函数声明
  20          void                    Flash_Init(void);                                                               //FLASH初始化
  21          //unsigned int  Get_Flash_ID(void);                                                             //读FLASH的ID号
  22          //unsigned char         Get_Flash_Status(void);                                         //读FLASH的状态
  23          unsigned char   Flash_Erase_Block(unsigned int BlockNum);               //擦除FLASH中的第BlockNum个BLOCK,擦除成功则返回1
  24          void                    Flash_Read_Page(unsigned int BLockNum,unsigned char BlockPageNum,unsigned char *P,unsigned int Dat
             -a_Length);
  25                                          //读出第BlockNum个BLOCK中第BlockPageNum个PAGE中的内容,长度为Data_Length
  26          unsigned char   Flash_Write_Page(unsigned int BLockNum,unsigned char BlockPageNum,unsigned char *P,unsigned
             - int Data_Length);
  27                                          //意义同上
  28          void                    Flash_Write_Command(unsigned char command);             //写命令
  29          void                    Flash_Write_Address(unsigned char address);             //写地址
  30          void                    Flash_Write_Data(unsigned char num);                    //写一字节数据
  31          unsigned char   Flash_Read_Data(void);                                                  //读一字节数据
  32          
  33          //void Test_Flash(void);                        //FLASH测试函数,首先读取FLASH的ID号,然后写入512字节数据,再读出来,判断是否正
             -确
  34          
  35          extern  void    Time_Delay(unsigned int time);
  36          ////////////////////////////////////////////////////////////////////////////////////////////////////
  37          
  38          
  39          void Flash_Init()
  40          {
  41   1              unsigned int i=0;
  42   1              FLASH_SE=0;FLASH_WP=1;FLASH_CE=0;FLASH_ALE=0;
  43   1              FLASH_CLE=0;FLASH_WE=1;FLASH_RE=1;FLASH_RB=1;   //控制位的初始状态
  44   1              Flash_Write_Command(0xff);                                              //复位命令
  45   1              Time_Delay(50);                                                                 //延时50ms
  46   1      }
  47          /*
  48          unsigned int Get_Flash_ID(void)
  49          {
  50                  unsigned int id=0;
  51                  Flash_Write_Command(0x90);              //读ID命令
  52                  Flash_Write_Address(0x00);              //地址
C51 COMPILER V7.20   FLASH                                                                 06/23/2008 19:39:41 PAGE 2   

  53                  id=Flash_Read_Data();               //读ID的高8位
  54                  id *=256;
  55                  id+=Flash_Read_Data();                  //读ID的低8位
  56                  return(id);
  57          }
  58          
  59          
  60          unsigned char Get_Flash_Status()
  61          {
  62                  unsigned char status=0;
  63                  Flash_Write_Command(0x70);
  64                  status=Flash_Read_Data();
  65                  return(status);
  66          }*/
  67          
  68          unsigned char Flash_Erase_Block(unsigned int BlockNum)
  69          {
  70   1              unsigned char Address_M=0,Address_H=0,Address_HH=0;
  71   1              unsigned long  PageNum=0;
  72   1              unsigned char status=0;
  73   1              PageNum=BlockNum*32;                            //计算PAGE地址(这是该BLOCK中的第一个PAGE),每个BLOCK含32个PAGE
  74   1              Address_M=PageNum & 0xff;
  75   1              Address_H=(PageNum>>8) & 0xff;          //擦除BLOCK时,只需要输入地址的高两字节,此处为计算地址的高两字节
  76   1              Address_HH=(PageNum>>8) & 0xff;
  77   1                      
  78   1              Flash_Write_Command(0x60);                      //发送擦除命令
  79   1              Flash_Write_Address(Address_M);
  80   1              Flash_Write_Address(Address_H);         //发送地址
  81   1              Flash_Write_Address(Address_HH);
  82   1              Flash_Write_Command(0xd0);                      //启动擦除
  83   1              P2MDOUT =0xbf;
  84   1              FLASH_RB=1;                                                     //FLASH_RB漏极开路输出
  85   1              while(!FLASH_RB)                                        //判断是否擦除完毕
  86   1              {}
  87   1      
  88   1              Flash_Write_Command(0x70);                      //发送读状态命令
  89   1              status=Flash_Read_Data();                       //读状态
  90   1              if(status & 0x01)                                       //如果有错误,则返回false
  91   1              {
  92   2                      return(0);
  93   2              }
  94   1              else                                                            //否则返回true
  95   1              {
  96   2                      return(1);
  97   2              }
  98   1      }
  99          
 100          void Flash_Read_Page(unsigned int BLockNum,unsigned char BlockPageNum,unsigned char *P,unsigned int Data_L
             -ength)
 101          {
 102   1              unsigned int  i=0;
 103   1              unsigned long  PageNum=0;
 104   1              unsigned char Address_L=0,Address_M=0,Address_H=0,Address_HH=0;
 105   1              PageNum=BLockNum*32+BlockPageNum;               //计算该PAGE在整个FLASH中的PAGE地址
 106   1              Address_L=0;
 107   1              Address_M=PageNum & 0xff;
 108   1              Address_H=(PageNum>>8) & 0xff;
 109   1              Address_HH=(PageNum>>8) & 0xff;
 110   1      
 111   1              Flash_Write_Command(0x00);                              //将内部指针指向A区
 112   1              Flash_Write_Address(Address_L);             //写地址
 113   1              Flash_Write_Address(Address_M);
C51 COMPILER V7.20   FLASH                                                                 06/23/2008 19:39:41 PAGE 3   

 114   1              Flash_Write_Address(Address_H);
 115   1              Flash_Write_Address(Address_HH);
 116   1              for(i=0;i<Data_Length;i++)
 117   1              {
 118   2                      *(P+i)=Flash_Read_Data();                       //读数据
 119   2              }
 120   1      
 121   1      }
 122          
 123          unsigned char Flash_Write_Page(unsigned int BLockNum,unsigned char BlockPageNum,unsigned char *P,unsigned 
             -int Data_Length)
 124          {
 125   1              unsigned int  i=0;
 126   1              unsigned long  PageNum=0;
 127   1              unsigned char Address_L=0,Address_M=0,Address_H=0,Address_HH=0;
 128   1              unsigned char status=0;
 129   1              PageNum=BLockNum*32+BlockPageNum;               //计算该PAGE在整个FLASH中的PAGE地址
 130   1              Address_L=0;
 131   1              Address_M=PageNum & 0xff;
 132   1              Address_H=(PageNum>>8) & 0xff;
 133   1              Address_HH=(PageNum>>8) & 0xff;
 134   1              
 135   1              Flash_Write_Command(0x00);                      //将内部指针指向A区
 136   1              Flash_Write_Command(0x80);                      //写FLASH命令
 137   1              Flash_Write_Address(Address_L);         //写地址
 138   1              Flash_Write_Address(Address_M);
 139   1              Flash_Write_Address(Address_H);
 140   1              Flash_Write_Address(Address_HH);
 141   1              for(i=0;i<Data_Length;i++)
 142   1              {
 143   2                      Flash_Write_Data(*(P+i));               //将数据写入缓冲区
 144   2              }
 145   1              Flash_Write_Command(0x10);                      //启动写入,将数据从缓冲区写入到FLASH
 146   1              P2MDOUT =0xbf;                                          //FLASH_RB漏极开路输出
 147   1              FLASH_RB=1;
 148   1              while(!FLASH_RB)                                        //判断是否操作完毕
 149   1              {}
 150   1      
 151   1              Flash_Write_Command(0x70);                      //发送读状态命令
 152   1              status=Flash_Read_Data();                       //读状态
 153   1              if(status & 0x01)                                       //如果有错误,则返回false
 154   1              {
 155   2                      return(0);
 156   2              }
 157   1              else                                                            //否则返回true
 158   1              {
 159   2                      return(1);
 160   2              }
 161   1      
 162   1      }
 163          
 164          void Flash_Write_Command(unsigned char command)
 165          {
 166   1              FLASH_ALE=0;    //地址锁存禁止
 167   1              FLASH_CLE=1;    //命令锁存使能
 168   1              FLASH_WE =0;    //写使能信号,上升沿有效
 169   1              P4=command;
 170   1              FLASH_WE =1;    //命令在上升沿写入
 171   1              FLASH_CLE=0;    //命令锁存禁止
 172   1              FLASH_ALE=1;
 173   1      }
 174          
C51 COMPILER V7.20   FLASH                                                                 06/23/2008 19:39:41 PAGE 4   

 175          void Flash_Write_Address(unsigned char address)
 176          {
 177   1              FLASH_WE=1;
 178   1              FLASH_CLE=0;    //命令锁存禁止
 179   1              FLASH_ALE=1;    //地址锁存使能
 180   1              FLASH_WE =0;    //写使能信号,上升沿有效
 181   1              P4=address;
 182   1              FLASH_WE =1;    //上升沿写入
 183   1              FLASH_ALE=0;    //地址锁存禁止
 184   1      }
 185          
 186          void Flash_Write_Data(unsigned char num)
 187          {
 188   1              FLASH_WE=1;
 189   1              FLASH_CLE=0;    //命令锁存禁止
 190   1              FLASH_ALE=0;    //地址锁存禁止
 191   1              FLASH_WE =0;    //写使能信号, 上升沿有效
 192   1              P4=num;
 193   1              FLASH_WE =1;    //命令在上升沿写入
 194   1      }
 195          
 196          unsigned char Flash_Read_Data(void)
 197          {
 198   1              unsigned char num=0;
 199   1              FLASH_CLE       =0;             //命令锁存禁止
 200   1              FLASH_ALE       =0;             //地址锁存禁止
 201   1              P4                      =0xff;
 202   1              FLASH_RE        =0;             //读信号使能
 203   1              P3MDOUT =0xdf;          //FLASH_RB漏极开路输出
 204   1              FLASH_RB=1;
 205   1              while(!FLASH_RB)        //判断是否操作完毕
 206   1              {}
 207   1              num=P4;
 208   1              FLASH_RE=1;
 209   1              return(num);
 210   1      }
 211          /*
 212          void Test_Flash()
 213          {
 214                  unsigned int Flash_ID;
 215                  xdata unsigned char Buffer[512];
 216                  unsigned int i=0,dat=0;
 217                  Flash_ID=Get_Flash_ID();
 218                  printf("Flash_ID is %u\n",Flash_ID);
 219          
 220                  for(i=0;i<512;i++)
 221                  {Buffer[i]=i;}
 222                  for(i=0;i<256;i++)
 223                  {Buffer[256+i]=i;}
 224                  Flash_Write_Page(1,1,Buffer,512);
 225                  for(i=0;i<512;i++)
 226                  {Buffer[i]=0;}
 227                  Flash_Read_Page(1,1,Buffer,512);
 228                  for(i=0;i<512;i++)
 229                  {
 230                          dat=Buffer[i];
 231                          printf("the No.%u data is %u\n",i,dat);
 232                  }
 233          
 234          }*/


C51 COMPILER V7.20   FLASH                                                                 06/23/2008 19:39:41 PAGE 5   

MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    546    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      47
   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 + -