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

📄 iic读写.lst

📁 VB环境下的串口通讯设计
💻 LST
字号:
C51 COMPILER V8.05a   IIC读写                                                              01/07/2008 16:20:13 PAGE 1   


C51 COMPILER V8.05a, COMPILATION OF MODULE IIC读写
OBJECT MODULE PLACED IN IIC读写.OBJ
COMPILER INVOKED BY: C:\Keilc51\C51\BIN\C51.EXE IIC读写.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          /************************************************/
   2          #include <REG52.H>
   3          #define uchar unsigned char
   4          #define uint unsigned int
   5          #define ulong unsigned long
   6          sbit I2C_SDA= P1^5;
   7          sbit I2C_SCK= P1^4;
   8          sbit I2C_WP= P1^3;
   9          sbit GRE=P1^7;
  10          uchar tab[4][16]={
  11                                          {0x40,0x00,0x27,0xF0,0x24,0x10,0x07,0xF0,0x94,0x10,0x54,0x10,0x17,0xF0,0x20,0x00},
  12                                          {0x2F,0xFC,0xC9,0x24,0x49,0x24,0x49,0x24,0x49,0x24,0x49,0x24,0x5F,0xFE,0x40,0x00},
  13                                          {0x00,0x00,0x12,0x10,0x12,0x10,0x12,0x10,0x12,0x10,0xFF,0xFE,0x12,0x10,0x12,0x10},
  14                                          {0x12,0x10,0x13,0xF0,0x12,0x10,0x10,0x00,0x10,0x08,0x1F,0xFC,0x00,0x00,0x00,0x00}
  15                                          };
  16          uchar tab2[2][16]={
  17                                          {0x00,0x00,0x12,0x10,0x12,0x10,0x12,0x10,0x12,0x10,0xFF,0xFE,0x12,0x10,0x12,0x10},
  18                                          {0x12,0x10,0x13,0xF0,0x12,0x10,0x10,0x00,0x10,0x08,0x1F,0xFC,0x00,0x00,0x00,0x00}
  19                                     };
  20          //===============================================
  21          
  22          //=============================
  23          //延时10US
  24          //内部调用函数
  25          void  Delay_10_uS(void)
  26          {
  27   1           char i=10;
  28   1           while(i--);
  29   1      }
  30          
  31          //延时n_milisecond MS
  32          //供内部调用函数
  33          void delay_n_ms( uint n_milisecond)  /* n mS delay */
  34          {
  35   1           uchar i;
  36   1           while(n_milisecond--)
  37   1           {
  38   2                i=37;
  39   2                while(i--);
  40   2           }
  41   1      }
  42          
  43          //开始位
  44          //内部调用函数
  45          bit I2C_Start(void)
  46          {
  47   1           Delay_10_uS();
  48   1           I2C_SDA =1;
  49   1           Delay_10_uS();
  50   1           I2C_SCK =1;
  51   1           Delay_10_uS();
  52   1           if ( I2C_SDA == 0) return 0;
  53   1           if ( I2C_SCK == 0) return 0;
  54   1           I2C_SDA = 0;
  55   1           Delay_10_uS();
C51 COMPILER V8.05a   IIC读写                                                              01/07/2008 16:20:13 PAGE 2   

  56   1           I2C_SCK = 0;
  57   1           Delay_10_uS();
  58   1           return 1;
  59   1      }
  60          
  61          //停止位
  62          //内部调用函数
  63          void  I2C_Stop(void)
  64          {
  65   1           Delay_10_uS();
  66   1           I2C_SDA = 0;
  67   1           Delay_10_uS();
  68   1           I2C_SCK = 1;
  69   1           Delay_10_uS();
  70   1           I2C_SDA = 1;
  71   1           Delay_10_uS();
  72   1      }
  73          
  74          
  75          //写单字节数据
  76          //内部调用函数
  77          bit I2C_Send_Byte( uchar d)
  78          {
  79   1           uchar i = 8;
  80   1           bit bit_ack;
  81   1           while( i-- )
  82   1           {
  83   2                Delay_10_uS();
  84   2                if ( d &0x80 )   I2C_SDA =1;
  85   2                else             I2C_SDA =0;
  86   2                Delay_10_uS();
  87   2                I2C_SCK = 1;
  88   2                Delay_10_uS();
  89   2                I2C_SCK = 0;
  90   2                d = d << 1;
  91   2           }
  92   1           Delay_10_uS();
  93   1           I2C_SDA = 1;
  94   1           Delay_10_uS();
  95   1           I2C_SCK = 1;
  96   1           Delay_10_uS();
  97   1           bit_ack = I2C_SDA;
  98   1           I2C_SCK =0;
  99   1           Delay_10_uS();
 100   1           return bit_ack;
 101   1      }
 102          
 103          
 104          
 105          //写多字节数据
 106          //供外部调用函数
 107          void AT24C64_W(void *mcu_address,uint AT24C64_address,uint count)
 108          {
 109   1      
 110   1           while(count--)
 111   1           {
 112   2                I2C_Start();
 113   2                /*I2C_Send_Byte( 0xa0 + AT24C64_address /256 *2);*/  /* 24C16  USE */
 114   2                I2C_Send_Byte( 0xa0 );//写命令设备地址为10100000最后一位为0"写标志"
 115   2                I2C_Send_Byte(  AT24C64_address/256 );//写存储器页地址
 116   2                I2C_Send_Byte( AT24C64_address %256 );//写存储器字节地址
 117   2                I2C_Send_Byte( *(uchar*)mcu_address );//写MCU地址指针指数据到存储单元
C51 COMPILER V8.05a   IIC读写                                                              01/07/2008 16:20:13 PAGE 3   

 118   2                I2C_Stop();
 119   2                delay_n_ms(10);       /* waiting for write cycle to be completed */
 120   2                ((uchar*)mcu_address)++;
 121   2                AT24C64_address++;
 122   2           }
 123   1      }
 124          
 125          //读多字节数据
 126          //供外部调用函数
 127          
 128          //=====================================
 129          void main()
 130          {
 131   1              //uchar i,j;
 132   1              P1=0;
 133   1              P2=0;
 134   1              P3=0;
 135   1              GRE=0;
 136   1              AT24C64_W(tab,0x00,64);
 137   1              GRE=1;
 138   1      }


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