C51 COMPILER V7.06   I2C                                                                   04/25/2016 22:03:28 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE I2C
OBJECT MODULE PLACED IN i2c.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE i2c.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1          #include "i2c.h"  //包含i2c.h头文件，注意自建的头文件是用双引号
   2          void delay()   //微秒级延时函数
   3          {
   4   1              ;;  //用两个空语句实现短时间延时，当晶振为11.0592MHz时，约4~5微秒
   5   1      }
   6          void start()  //起始信号
   7          {
   8   1              SDA=1;
   9   1              SCL=1;
  10   1              delay();
  11   1              SDA=0;
  12   1              delay();
  13   1      }
  14          
  15          void stop()  //终止信号
  16          {
  17   1              SDA=0;
  18   1              SCL=1;
  19   1              delay();
  20   1              SDA=1;
  21   1              delay();
  22   1      }
  23          
  24          void ack()  //应答信号
  25          {
  26   1              uchar i;
  27   1              SCL=1;
  28   1              delay();
  29   1              while((SDA==1)&&(i<250))i++;
  30   1              SCL=0;
  31   1              delay();
  32   1      }
  33          
  34          void nack()  //无应答信号
  35          {
  36   1              SCL=1;
  37   1              delay();
  38   1              SDA=1;
  39   1              SCL=0;
  40   1              delay();
  41   1      }
  42          
  43          void write_byte(uchar date) //写入一个字节到I2C总线
  44          {
  45   1              uchar i,temp;
  46   1              temp=date;
  47   1              for(i=0;i<8;i++)
  48   1              {
  49   2                      temp=temp<<1;
  50   2                      SCL=0;
  51   2                      delay();
  52   2                      SDA=CY;
  53   2                      delay();
  54   2                      SCL=1;
  55   2                      delay();
C51 COMPILER V7.06   I2C                                                                   04/25/2016 22:03:28 PAGE 2   

  56   2              }
  57   1              SCL=0;
  58   1              delay();
  59   1              SDA=1;
  60   1              delay();
  61   1      }
  62          
  63          uchar read_byte() //从I2C读一个字节
  64          {
  65   1              uchar i,j,k;
  66   1              SCL=0;
  67   1              delay();
  68   1              for(i=0;i<8;i++)
  69   1              {
  70   2                      SCL=1;
  71   2                      delay();
  72   2                      j=SDA;
  73   2                      k=(k<<1)|j;
  74   2                      SCL=0;
  75   2                      delay();
  76   2              }
  77   1              return k;
  78   1      }
  79          
  80          void write_at24c02(uchar address ,uchar date)  //at24c02按字节写入函数
  81          {
  82   1              start();
  83   1              write_byte(0xa0);
  84   1              ack();
  85   1              write_byte(address);
  86   1              ack();
  87   1              write_byte(date);
  88   1              ack();
  89   1              stop();
  90   1      }
  91          
  92          uchar read_at24c02(uchar address)  //对at24c02随机读函数
  93          {
  94   1              uchar date;
  95   1              start();
  96   1              write_byte(0xa0);
  97   1              ack();
  98   1              write_byte(address);
  99   1              ack();
 100   1              start();
 101   1              write_byte(0xa1);
 102   1              ack();
 103   1              date=read_byte();
 104   1              nack();
 105   1              stop();
 106   1              return date;
 107   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    194    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----       1
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
C51 COMPILER V7.06   I2C                                                                   04/25/2016 22:03:28 PAGE 3   

END OF MODULE INFORMATION.


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