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

📄 i2c.lst

📁 该工程文件包含了 myson系列mtv018/130 osd芯片的i2C通讯和访问程序
💻 LST
字号:
C51 COMPILER V7.50   I2C                                                                   10/13/2006 22:08:45 PAGE 1   


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

line level    source

   1          #pragma REGISTERBANK(0)
   2          
   3          #include <reg52.h>
   4          #include <intrins.h>
   5          //#include "def.h"
   6          //#include "timer.h"
   7          //#include "delay.h"
   8          
   9          sbit SCK = P0 ^ 6;
  10          sbit SDA = P0 ^ 7;
  11          
  12          extern void i2c_init(void);
  13          
  14          void    mdelay(unsigned char delay)
  15          {
  16   1              unsigned char i;
  17   1              for(i = 0;i<= delay;i++){
  18   2                      _nop_();_nop_();
  19   2                      _nop_();_nop_();
  20   2              }
  21   1      }
  22          
  23          static void i2c_delay(void)
  24          {
  25   1              _nop_(); _nop_();
  26   1              _nop_(); _nop_();
  27   1      }
  28          
  29          static void i2c_start(void)
  30          {
  31   1              SDA = 0;
  32   1              i2c_delay();
  33   1      }
  34          
  35          static void i2c_restart(void)
  36          {
  37   1              SCK = 0;
  38   1              SDA = 1;
  39   1              i2c_delay();
  40   1              SCK = 1;
  41   1              i2c_delay();
  42   1              SDA = 0;
  43   1              i2c_delay();
  44   1      }
  45          
  46          static void i2c_stop(void)
  47          {
  48   1              SCK = 0;
  49   1              SDA = 0;
  50   1              i2c_delay();
  51   1              SCK = 1;
  52   1              i2c_delay();
  53   1              SDA = 1;
  54   1              i2c_delay();
  55   1      }
C51 COMPILER V7.50   I2C                                                                   10/13/2006 22:08:45 PAGE 2   

  56          
  57          static void i2c_outb(unsigned char c)
  58          {
  59   1              unsigned char i;
  60   1              for (i = 0; i < 8; i++) {
  61   2                      SCK = 0;
  62   2                      SDA = (c & 0x80);
  63   2                      i2c_delay();
  64   2                      SCK = 1;
  65   2                      i2c_delay();
  66   2                      c <<= 1;
  67   2              }
  68   1      }
  69          
  70          static unsigned char i2c_inb(void)
  71          {
  72   1              unsigned char i,c = 0;
  73   1              for (i = 0; i < 8; i++) {
  74   2                      c <<= 1;
  75   2                      SCK = 0;
  76   2                      SDA = 1;
  77   2                      i2c_delay();
  78   2                      SCK = 1;
  79   2                      i2c_delay();
  80   2                      if (SDA)
  81   2                              c |= 1;
  82   2              }
  83   1              return c;
  84   1      }
  85          
  86          static bit wait_for_pin(void)
  87          {
  88   1              unsigned char loops = 255;
  89   1              SCK = 0;
  90   1              SDA = 1;
  91   1              i2c_delay();
  92   1              SCK = 1;
  93   1              while (SDA && --loops)
  94   1                      _nop_();
  95   1              return !SDA;
  96   1      }
  97          
  98          static void do_ack(void)
  99          {
 100   1              SCK = 0;
 101   1              SDA = 0;
 102   1              i2c_delay();
 103   1              SCK = 1;
 104   1              i2c_delay();
 105   1      }
 106          
 107          static bit i2c_send(const unsigned char data *buf, unsigned char count)
 108          {
 109   1              while (count) {
 110   2                      i2c_outb(*buf);
 111   2                      if (!wait_for_pin()) {
 112   3                              return 0;
 113   3                      }
 114   2                      buf++;
 115   2                      count--;
 116   2              }
 117   1              return 1;
C51 COMPILER V7.50   I2C                                                                   10/13/2006 22:08:45 PAGE 3   

 118   1      }
 119          
 120          static bit i2c_recv(unsigned char data *buf, count)
 121          {
 122   1              if (count == 0)
 123   1                      return 1;
 124   1              for (;;) {
 125   2                      *buf = i2c_inb();
 126   2                      buf++;
 127   2                      count--;
 128   2                      if (count == 0)
 129   2                              break;
 130   2                      do_ack();
 131   2              }
 132   1              return 1;
 133   1      }
 134          
 135          bit i2c_xfer(unsigned char addr, unsigned char data *buf, unsigned char count)
 136          {
 137   1              bit ret = 0;
 138   1      
 139   1              i2c_start();
 140   1              i2c_outb(addr);
 141   1              if (!wait_for_pin())
 142   1                      goto out;
 143   1              if (addr & 1)
 144   1                      ret = i2c_recv(buf, count);
 145   1              else
 146   1                      ret = i2c_send(buf, count);
 147   1      out:
 148   1              i2c_stop();
 149   1              return ret;
 150   1      }
 151          
 152          #define AT_ADDR 0x50
 153          
 154          static bit at24cxx_poll(bit read)
 155          {
 156   1              bit ret = 0;
 157   1      
 158   1              i2c_start();
 159   1              i2c_outb(read ? ((AT_ADDR<<1)|1) : (AT_ADDR<<1));
 160   1              if (wait_for_pin())
 161   1                      return 1;
 162   1              return 0;
 163   1      }
 164          
 165          bit at24cxx_writebyte(unsigned char addr, unsigned char c)
 166          {
 167   1              bit ret = 0;
 168   1      
 169   1              if (!at24cxx_poll(0))
 170   1                      goto out;
 171   1      
 172   1              i2c_outb(addr);
 173   1              if (!wait_for_pin())
 174   1                      goto out;
 175   1              i2c_outb(c);
 176   1              if (!wait_for_pin())
 177   1                      goto out;
 178   1              ret = 1;
 179   1      out:
C51 COMPILER V7.50   I2C                                                                   10/13/2006 22:08:45 PAGE 4   

 180   1              i2c_stop();
 181   1              if (ret) {
 182   2                      mdelay(20);
 183   2              }
 184   1              return ret;
 185   1      }
 186          
 187          bit at24cxx_read(unsigned char addr, unsigned char data *buf, unsigned char count)
 188          {
 189   1              bit ret = 0;
 190   1      
 191   1              if (!at24cxx_poll(0))
 192   1                      goto out;
 193   1      
 194   1              i2c_outb(addr);
 195   1              if (!wait_for_pin())
 196   1                      goto out;
 197   1      
 198   1              i2c_restart();
 199   1              i2c_outb((AT_ADDR<<1)|1);
 200   1              if (!wait_for_pin())
 201   1                      goto out;
 202   1              ret = i2c_recv(buf, count);
 203   1      out:
 204   1              i2c_stop();
 205   1              return ret;
 206   1      }
 207          
 208          void i2c_init(void)
 209          {
 210   1              SCK = 1;
 211   1              SDA = 1;
 212   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    356    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       5
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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