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

📄 24c02_eeprom读写.lst

📁 EEPROM 24C02的STC89c52程序
💻 LST
字号:
C51 COMPILER V7.06   24C02_EEPROM_羅_                                                      08/21/2008 09:46:55 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE 24C02_EEPROM_羅_
OBJECT MODULE PLACED IN 24c02_EEPROM读写.OBJ
COMPILER INVOKED BY: d:\Keil2\C51\BIN\C51.EXE 24c02_EEPROM读写.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1             
   2          /*copyright 2007,ShangHai  HaoTun Electronic Technology co.,Ltd
   3           *
   4           *This sample is used for EEPROM 24C02 Read and Write.
   5           *
   6           *write by Shifang 2007-4-29
   7           *
   8           *V1.1
   9           */
  10          
  11          
  12           #include <reg52.h>
  13           #include <intrins.h>
  14          
  15           #define AddWr 0xae //Write Address
  16           #define AddRd 0xaf //Read Address
  17          
  18           //全局变量
  19           sbit Sda=P1^2;
  20           sbit Scl=P1^1;
  21           sbit WP=P3^3;//No connect here
  22          
  23           void mDelay(unsigned char j)//A normal delay
  24           {
  25   1        unsigned int i;
  26   1        for(;j>0;j--)
  27   1           {
  28   2                for(i=0;i<125;i++)
  29   2                   {;}
  30   2                }
  31   1        }
  32          
  33            //start IIC
  34            void Start(void)
  35            {
  36   1         Sda=1;
  37   1         _nop_();_nop_();
  38   1         Scl=1;
  39   1         _nop_();_nop_();_nop_();_nop_();_nop_();
  40   1         Sda=0;
  41   1         _nop_();_nop_();_nop_();_nop_();_nop_();
  42   1         Scl=0;
  43   1        }
  44          
  45            //stop IIC
  46            void Stop(void)
  47            {
  48   1         Sda=0;
  49   1         _nop_();
  50   1         Scl=1;
  51   1         _nop_();_nop_();_nop_();_nop_();_nop_();
  52   1         Sda=1;
  53   1         _nop_();_nop_();_nop_();_nop_();_nop_();
  54   1         Scl=0;
  55   1         _nop_();_nop_();
C51 COMPILER V7.06   24C02_EEPROM_羅_                                                      08/21/2008 09:46:55 PAGE 2   

  56   1         Scl=1;
  57   1      
  58   1         }
  59          
  60             //ack IIC
  61             void Ack(void)
  62             {
  63   1          Sda=0;
  64   1              _nop_();_nop_();_nop_();
  65   1              Scl=1;
  66   1              _nop_();_nop_();_nop_();_nop_();_nop_();
  67   1              Scl=0;
  68   1              _nop_();_nop_();
  69   1              }
  70          
  71                  //unack IIC
  72                  void NoAck(void)
  73                  {
  74   1               Sda=1;
  75   1               _nop_();_nop_();_nop_();
  76   1               Scl=1;
  77   1               _nop_();_nop_();_nop_();_nop_();_nop_();
  78   1               Scl=0;
  79   1               _nop_();_nop_();
  80   1               }
  81          
  82                   //send a byte
  83                   void Send(unsigned char Data)
  84                   { 
  85   1                unsigned char BitCounter=8;
  86   1                unsigned char temp;
  87   1      
  88   1                do
  89   1                  {
  90   2                       temp=Data;
  91   2                       Scl=0;
  92   2                       _nop_();_nop_();_nop_();_nop_();_nop_();
  93   2                       if((temp&0x80)==0x80)
  94   2                          Sda=1;
  95   2                       else
  96   2                          Sda=0;
  97   2      
  98   2      
  99   2                              Scl=1;
 100   2                              temp=Data<<1;
 101   2                              Data=temp;
 102   2                              BitCounter--;
 103   2                        }
 104   1                while(BitCounter);
 105   1                    Scl=0;
 106   1                }
 107          
 108                    //read a byte and return
 109                    unsigned char Read(void)
 110                    {
 111   1                 unsigned char temp=0;
 112   1                 unsigned char temp1=0;
 113   1                 unsigned char BitCounter=8;
 114   1      
 115   1                 Sda=1;
 116   1                 do
 117   1                   {
C51 COMPILER V7.06   24C02_EEPROM_羅_                                                      08/21/2008 09:46:55 PAGE 3   

 118   2                        Scl=0;
 119   2                _nop_();_nop_();_nop_();_nop_();_nop_();
 120   2                        Scl=1;
 121   2                        _nop_();_nop_();_nop_();_nop_();_nop_();
 122   2                        if(Sda)
 123   2                           temp=temp|0x01;
 124   2                        else
 125   2                           temp=temp&0xfe;
 126   2      
 127   2                        if(BitCounter-1)
 128   2                           {
 129   3                                temp1=temp<<1;
 130   3                                temp=temp1;
 131   3                                }
 132   2                                BitCounter--;
 133   2                               }
 134   1                      while(BitCounter);
 135   1                      return(temp);
 136   1                }
 137             //write to ROM
 138                    void WrToROM(unsigned char Data[],unsigned char Address,unsigned char Num)
 139                    {
 140   1                 unsigned char i;
 141   1                 unsigned char *PData;
 142   1                 PData=Data;
 143   1                 for(i=0;i<Num;i++)
 144   1                    {
 145   2                         Start();
 146   2                         Send(AddWr);//Write Address
 147   2                         Ack();
 148   2                         Send(Address+i);//Write sub Address
 149   2                         Ack();
 150   2                         Send(*(PData+i));//Write Data
 151   2                         Ack();
 152   2                         Stop();
 153   2                         mDelay(20);
 154   2                         }
 155   1                 }
 156             //read from ROM
 157                    void RdFromROM(unsigned char Data[],unsigned char Address,unsigned char Num)
 158                    {
 159   1                 unsigned char i;
 160   1                 unsigned char *PData;
 161   1                 PData=Data;
 162   1             for(i=0;i<Num;i++)
 163   1                    {
 164   2                         Start();
 165   2                         Send(AddWr);//Write Address
 166   2                         Ack();
 167   2                         Send(Address+i);//Write sub Address
 168   2                         Ack();
 169   2                         Start();
 170   2                         Send(AddRd);//Read Address
 171   2                         Ack();
 172   2                         *(PData+i)=Read();//Read Data
 173   2                         Scl=0;
 174   2                         NoAck();
 175   2                         Stop();
 176   2                         }
 177   1                }
 178          
 179                  void main()
C51 COMPILER V7.06   24C02_EEPROM_羅_                                                      08/21/2008 09:46:55 PAGE 4   

 180                  {
 181   1               unsigned char Number[4]={1,2,4,8};
 182   1               unsigned char i;
 183   1               //WP=1;
 184   1               //P2=0x20;
 185   1               //P0=Number[1];
 186   1               WrToROM(Number,4,4);//写入4个数据
 187   1               mDelay(200);  
 188   1               Number[0]=0;//将现有的数据清0
 189   1               Number[1]=0;
 190   1               Number[2]=0;
 191   1               Number[3]=0;
 192   1               RdFromROM(Number,4,4);//从存储器读出存入的数据
 193   1               
 194   1               while(1)
 195   1                    { 
 196   2                     P1=Number[i];//将这4个数据从P1口显示出来
 197   2                         mDelay(200);
 198   2                         i++;
 199   2                         if(i==4)
 200   2                            i=0;
 201   2                        }
 202   1              }


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