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

📄 i2c_24c02.lst

📁 51单片机
💻 LST
字号:
C51 COMPILER V7.02a   I2C_24C02                                                            09/05/2007 21:17:13 PAGE 1   


C51 COMPILER V7.02a, COMPILATION OF MODULE I2C_24C02
OBJECT MODULE PLACED IN I2C_24C02.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE I2C_24C02.C BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          /***********************************************************
   2          **模块名称:24C02的读写
   3          **编写人:bradley   日期 2005-5-1
   4          **修改人:psp       日期 2006-10-27
   5          **功能描述:24C02储存开机次数实验
   6          **该试验功能是单片机复位一次, 自动从24C02中读取数据
   7          **然后加1,最终数码管中的数据就是开机的次数,具有一定的实用意义
   8          **烧写后用手按复位键可以看到数码管每按一下加一,也可以断电再开机
   9          **********************************************************/
  10          #include "STC89C51RC_RD_PLUS.H"
  11          
  12          #include <intrins.h>
  13          #define uchar unsigned char
  14          #define uint unsigned int
  15          
  16          code unsigned char seg7code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
  17                                         0x82,0xf8,0x80,0x90,0xff}; //共阳数码管段码
  18          sbit    SDA=P3^5;           //定义数据线
  19          sbit    SCL=P3^4;           //定义时钟线
  20          bit     flag;
  21          
  22          uint    idata  ucSendBuffer[1]=0;
  23          uint    idata  ucReceData;
  24          
  25          void    delay(void);
  26          void    delay_10ms(void);
  27          
  28          void    ACK();
  29          void    NoACK();
  30          
  31          
  32          
  33          /**************************************************************/
  34          void delay(void)
  35          {
  36   1        uint i;
  37   1        for(i=100;i>0;i--)
  38   1        _nop_();
  39   1      }
  40          
  41          void delay1ms()
  42          {
  43   1       uchar i;
  44   1       for(i=124;i>0;i--);  //延时124*8+10=1002us
  45   1      }
  46          
  47          /*********************************************************
  48          **名称:I2C_Start
  49          **功能:启动I2C
  50          **输入:无
  51          **返回:无
  52          *********************************************************/
  53          void I2C_Start()
  54          {
  55   1              SDA=1;
C51 COMPILER V7.02a   I2C_24C02                                                            09/05/2007 21:17:13 PAGE 2   

  56   1              delay();
  57   1              SCL=1;
  58   1              delay();
  59   1              SDA=0;
  60   1              delay();
  61   1              SCL=0;                //钳位I2C总线,准备发送数据
  62   1      }
  63          
  64          
  65          
  66          /**********************************************************
  67          **名称:I2C_Stop
  68          **功能:停止I2C
  69          **输入:无
  70          **返回:无
  71          **********************************************************/
  72          void I2C_Stop()
  73          {
  74   1              SDA=0;
  75   1              delay();
  76   1              SCL=1;
  77   1              delay();
  78   1              SDA=1;
  79   1              delay();
  80   1      }
  81          
  82          
  83          
  84          
  85          /**********************************************************
  86          **名称:Ack
  87          **功能:应答信号
  88          **输入:无
  89          **返回:无
  90          **********************************************************/
  91          void Ack()
  92          {
  93   1              SDA=0;
  94   1              delay();
  95   1              SCL=1;
  96   1              delay();
  97   1              SCL=0;
  98   1              delay();
  99   1              SDA=1;
 100   1              delay();
 101   1      }
 102          
 103          
 104          
 105          /********************************************************
 106          **名称:NoAck
 107          **功能:发送非应答信号
 108          **输入:无
 109          **返回:无
 110          ********************************************************/
 111          void NoAck()
 112          {
 113   1              SDA=1;
 114   1              delay();
 115   1              SCL=1;
 116   1              delay();
 117   1              SCL=0;
C51 COMPILER V7.02a   I2C_24C02                                                            09/05/2007 21:17:13 PAGE 3   

 118   1              delay();
 119   1              SDA=0;
 120   1              delay();
 121   1      }
 122          
 123          
 124          
 125          
 126          /********************************************************
 127          **名称:Test_Ack()
 128          **功能:检测应答位
 129          **输入:无
 130          **返回:flag,有应答时flag为0,无应答时flag为1
 131          *********************************************************/
 132          bit Test_Ack()
 133          { 
 134   1              SCL=0;
 135   1              SDA=1;//读入数据
 136   1              _nop_();_nop_();_nop_();_nop_();
 137   1              SCL=1;
 138   1              _nop_();_nop_();_nop_();_nop_();
 139   1              if(SDA==0)
 140   1                      flag=1;
 141   1              else        flag=0;
 142   1              SCL=0;
 143   1              return(flag);
 144   1      }
 145          
 146          
 147          
 148          /********************************************************
 149          **名称:SendData()        
 150          **功能:发送一字节数据
 151          **输入:buffer
 152          **返回:
 153          *******************************************************/
 154          void SendData(uint buffer)
 155          {
 156   1              uint BitCnt=8;//一字节8位
 157   1              uint temp=0;
 158   1              do
 159   1              {
 160   2                      temp=buffer;
 161   2                      SCL=0;
 162   2                      delay();
 163   2                      if((temp&0x80)==0) //判断最高位是0还是1
 164   2                              SDA=0;
 165   2                      else
 166   2                              SDA=1;
 167   2                      delay();
 168   2                      SCL=1;
 169   2                      temp=_crol_(buffer,1);//将buffer中的数据左移一位
 170   2                      buffer=temp;
 171   2                      BitCnt--;
 172   2              }
 173   1              while(BitCnt);
 174   1              SCL=0;        
 175   1      }
 176          
 177          /**************************************************************
 178          **名称:uint ReceiveData()
 179          **功能:接收一字节数据
C51 COMPILER V7.02a   I2C_24C02                                                            09/05/2007 21:17:13 PAGE 4   

 180          **输入:
 181          **返回:ucReceData
 182          **说明:将接收的数据存放在ucReceData中
 183          **************************************************************/
 184          uint ReceiveData()
 185          {
 186   1              uint BitCnt=8;
 187   1              uint temp=0;
 188   1              SDA=1;//读入数据
 189   1              do
 190   1              {
 191   2                      SCL=0;
 192   2                      delay();
 193   2                      SCL=1;
 194   2                      delay();
 195   2                      if(SDA==1)
 196   2                              ucReceData=ucReceData|0x01;  //低位置1
 197   2                      else
 198   2                              ucReceData=ucReceData&0x0fe; //低位清0
 199   2                      if(BitCnt-1)
 200   2                      {
 201   3                              temp=_crol_(ucReceData,1);   //数据左移一位
 202   3                              ucReceData=temp;
 203   3                      }
 204   2                      BitCnt--;
 205   2              }
 206   1              while(BitCnt);
 207   1              SCL=0;
 208   1              return(ucReceData);
 209   1      }
 210          
 211          /*************************************************************
 212          **名称:bit WriteNByte()
 213          **功能:主机向24C02中写入多字节数据
 214          **输入:
 215          **返回:
 216          **说明:sla-器件地址 suba-数据地址,*s-写入的数据,n-写入的字节数(n<=8)
 217          **************************************************************/
 218          bit WriteNByte(uint sla,uint suba,uint *s,uint n)
 219          {
 220   1              uint i;
 221   1              I2C_Start();//启动I2C
 222   1              SendData(sla);//发送器件地址
 223   1              Test_Ack();
 224   1             // if(flag==0)        return(0);
 225   1              SendData(suba);
 226   1              Test_Ack();
 227   1              //if(flag==0)        return(0);
 228   1              for(i=0;i<n;i++)//写入8字节数据
 229   1              {
 230   2                      SendData(*(s+i));
 231   2                      Test_Ack();
 232   2                     // if(flag==0)        return(0);
 233   2              }
 234   1              I2C_Stop();
 235   1              return(1);
 236   1      }
 237          /*************************************************************
 238          **名称:bit ReadNByte()
 239          **功能:主机从24C02中读出N字节数据(n<=8)
 240          **输入:
 241          **返回:
C51 COMPILER V7.02a   I2C_24C02                                                            09/05/2007 21:17:13 PAGE 5   

 242          **说明:随机地址读操作
 243          **************************************************************/
 244          bit ReadNByte(uint sla,uint suba,uint *p,uint n)
 245          {
 246   1              uint i;
 247   1              I2C_Start();//启动I2C
 248   1              SendData(sla);//发送器件地址
 249   1              Test_Ack();
 250   1              //if(flag==0)        return(0);
 251   1              SendData(suba);//发送器件内部地址
 252   1              Test_Ack();
 253   1              //if(flag==0)        return(0);
 254   1      
 255   1              I2C_Start();
 256   1              SendData(sla+1);
 257   1              Test_Ack();
 258   1              if(flag==0)        return(0);
 259   1              for(i=0;i<n-1;i++)//读取字节数据
 260   1              {
 261   2                      *(p+i)=ReceiveData();//读取数据
 262   2                      ACK();
 263   2              }
 264   1              *(p+n-1)=ReceiveData();
 265   1              
 266   1              NoACK();
 267   1              I2C_Stop();
 268   1              return(1);
 269   1      }
 270          Initial_com()
 271          {
 272   1       EA=1;        //开总中断
 273   1       ES=1;        //允许串口中断
 274   1       ET1=1;        //允许定时器T1的中断
 275   1       TMOD=0x20;   //定时器T1,在方式3中断产生波特率
 276   1       PCON=0x00;   //SMOD=0
 277   1       SCON=0x50;   
 278   1       TH1=0xfd;    //波特率设置为9600
 279   1       TL1=0xfd;
 280   1       TR1=1;       //开定时器T1运行控制位
 281   1      }
 282          
 283          /***************************************************************
 284          **名称:main()
 285          **功能:
 286          **输入:
 287          **返回:
 288          **说明:
 289          ****************************************************************/
 290          void main()
 291          { 
 292   1        Initial_com() ;     
 293   1        ReadNByte(0xa0,0x00,ucSendBuffer,1);
 294   1        ucSendBuffer[0]++;
 295   1       
 296   1        WriteNByte(0xa0,0x00,ucSendBuffer,1);
 297   1      
 298   1        while(1)
 299   1          { unsigned int i;
 300   2             i++;
 301   2          SBUF=ucSendBuffer[0];
 302   2               P0=0xfd;     //P1.1=0,选通第二位
 303   2               P2=seg7code[ucSendBuffer[0]%1000/100];   //百位
C51 COMPILER V7.02a   I2C_24C02                                                            09/05/2007 21:17:13 PAGE 6   

 304   2               delay1ms();
 305   2               P2=0xff;       //消隐
 306   2              
 307   2               P0=0xfb;     //P1.3=0,选通第三位
 308   2               P2=seg7code[ucSendBuffer[0]%100/10];   //十位
 309   2               delay1ms();
 310   2               P2=0xff;         //消隐
 311   2      
 312   2               P0=0xf7;     //P1.3=0,选通第四位
 313   2               P2=seg7code[ucSendBuffer[0]%10];   //个位
 314   2               delay1ms();
 315   2               P2=0xff;       //消隐
 316   2      
 317   2         }
 318   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    714    ----
   CONSTANT SIZE    =     11    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----      24
   IDATA SIZE       =      4    ----
   BIT SIZE         =      1    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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