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

📄 vi2c_c51.lst

📁 MCU 通过i2c 总线读写eeprom, i2c 读写程序是软件仿真方式来实现
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.50   VI2C_C51                                                              06/01/2006 10:40:35 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE VI2C_C51
OBJECT MODULE PLACED IN VI2C_C51.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE VI2C_C51.c OPTIMIZE(2,SPEED) BROWSE DEBUG OBJECTEXTEND TABS(2)

line level    source

   1          /********************************************************************
   2                                 VI2C_C51.C
   3            此程序是I2C操作平台(主方式的软件平台)的底层的C子程序,如发送数据
   4          及接收数据,应答位发送,并提供了几个直接面对器件的操作函数,它很方便的
   5          与用户程序连接并扩展.....  
   6            
   7              注意:函数是采用软件延时的方法产生SCL脉冲,固对高晶振频率要作 
   8          一定的修改....
   9          
  10          ********************************************************************/
  11            
  12            
  13                        
  14          #include <reg51.h>          /*include head file*/
  15          #include <intrins.h>
  16          #include "VI2C_C51.h"
  17          
  18          #define  uchar unsigned char /*宏定义*/
  19          #define  uint  unsigned int
  20          
  21          #define  _Nop()  _nop_();_nop_();_nop_()        /*define null operate*/
  22          
  23          
  24          
  25          /*constant/ variable define */
  26           
  27          /*endport define*/
  28          sbit SDA=P1^0;            /*IIC data bit*/
  29          sbit SCL=P1^1;            /*IIC clock control*/
  30          
  31          /*status flag*/
  32          bit ack;           /*response flag bit */
  33             
  34          
  35          
  36          
  37          
  38          /*******************************************************************
  39                               起start bus function               
  40          function original: void  Start_I2c();  
  41          function: start I2C bus, 
  42            
  43          ********************************************************************/
  44          void Start_I2c()
  45          {
  46   1        SDA=1;   /*data start */
  47   1        _Nop();
  48   1        SCL=1;
  49   1        _Nop();    /*start condition setup up time 4.7us, delay*/
  50   1        _Nop();
  51   1        _Nop();
  52   1        _Nop();
  53   1        _Nop();    
  54   1        SDA=0;   /*tx start signal*/
  55   1        _Nop();    /*start condition locked time big than 4us*/
C51 COMPILER V7.50   VI2C_C51                                                              06/01/2006 10:40:35 PAGE 2   

  56   1        _Nop();
  57   1        _Nop();
  58   1        _Nop();
  59   1        _Nop();       
  60   1        SCL=0;   /*keep I2C bus, ready tx or rx data*/
  61   1        _Nop();
  62   1        _Nop();
  63   1      }
  64          
  65          
  66          
  67          
  68          /*******************************************************************
  69                                end bus function            
  70          name: void  Stop_I2c();  
  71          function: end I2C bus, 
  72            
  73          ********************************************************************/
  74          void Stop_I2c()
  75          {
  76   1        SDA=0;  /*tx stop data condition signal*/
  77   1        _Nop();   /*tx stop clock condition signal*/
  78   1        SCL=1;  /*setup time >4us */
  79   1        _Nop();
  80   1        _Nop();
  81   1        _Nop();
  82   1        _Nop();
  83   1        _Nop();
  84   1        SDA=1;  /*tx I2C bus stop signal*/
  85   1        _Nop();
  86   1        _Nop();
  87   1        _Nop();
  88   1        _Nop();
  89   1      }
  90          
  91          
  92          
  93          
  94          /*******************************************************************
  95                           byte data transmit function     
  96          original: void  SendByte(uchar c);
  97          function:  data c Tx-out, data or address, tx done wait response, 
  98                    and status bit operate. error: ack=0, right ack=1.
  99          ********************************************************************/
 100          void  SendByte(uchar c)
 101          {
 102   1       uchar BitCnt;
 103   1       
 104   1       for(BitCnt=0;BitCnt<8;BitCnt++)  /*transmit data length is 8 bit*/
 105   1          {
 106   2           if((c<<BitCnt)&0x80)SDA=1;   /*check tx bit*/
 107   2             else  SDA=0;                
 108   2           _Nop();
 109   2           SCL=1;               /*set SCL to high, infor device rx data*/
 110   2            _Nop(); 
 111   2            _Nop();               /*hold time >4us */
 112   2            _Nop();
 113   2            _Nop();
 114   2            _Nop();         
 115   2           SCL=0; 
 116   2          }
 117   1          
C51 COMPILER V7.50   VI2C_C51                                                              06/01/2006 10:40:35 PAGE 3   

 118   1          _Nop();
 119   1          _Nop();
 120   1          SDA=1;               /*8bit tx done, ready rx ack bit*/
 121   1          _Nop();
 122   1          _Nop();   
 123   1          SCL=1;
 124   1          _Nop();
 125   1          _Nop();
 126   1          _Nop();
 127   1          if(SDA==1)ack=0;     
 128   1             else ack=1;        /*check whether rx response bit*/
 129   1          SCL=0;
 130   1          _Nop();
 131   1          _Nop();
 132   1      }
 133          
 134          
 135          
 136          
 137          
 138          
 139          /*******************************************************************
 140                           bit data rx function            
 141          original: uchar  RcvByte();
 142          function: rx device transmit data, and check bus error(ack=0)
 143                    
 144          ********************************************************************/ 
 145          uchar  RcvByte()
 146          {
 147   1        uchar retc;
 148   1        uchar BitCnt;
 149   1        
 150   1        retc=0; 
 151   1        SDA=1;             /*set SDA to input mode*/
 152   1        for(BitCnt=0;BitCnt<8;BitCnt++)
 153   1            {
 154   2              _Nop();           
 155   2              SCL=0;       /*set SCL=0, ready receive data bit*/
 156   2              _Nop();
 157   2              _Nop();         /*SCL hold time>4.7us*/
 158   2              _Nop();
 159   2              _Nop();
 160   2              _Nop();
 161   2              SCL=1;       /*set SCL=1, SDA data viable*/
 162   2              _Nop();
 163   2              _Nop();
 164   2              retc=retc<<1;
 165   2              if(SDA==1)retc=retc+1; /*read SDA, rx data bit input to retc */
 166   2              _Nop();
 167   2              _Nop(); 
 168   2            }
 169   1        SCL=0;    
 170   1        _Nop();
 171   1        _Nop();
 172   1        return(retc);
 173   1      }
 174          
 175          
 176          
 177          
 178          /********************************************************************
 179                               response sub-function
C51 COMPILER V7.50   VI2C_C51                                                              06/01/2006 10:40:35 PAGE 4   

 180          original:  void Ack_I2c(bit a);
 181           
 182          function: main control response signal(allow ack or no ack)
 183          ********************************************************************/
 184          void Ack_I2c(bit a)
 185          {
 186   1        
 187   1        if(a==0)SDA=0;     /*ack or no ack*/
 188   1              else SDA=1;
 189   1        _Nop();
 190   1        _Nop();
 191   1        _Nop();      
 192   1        SCL=1;
 193   1          _Nop();
 194   1          _Nop();              /*SCL hold time>4us */
 195   1          _Nop();
 196   1          _Nop();
 197   1          _Nop();  
 198   1       SCL=0;                /*clean SCL, keep I2C bus to continue rx*/
 199   1          _Nop();
 200   1          _Nop();    
 201   1      }
 202          
 203          
 204          
 205          
 206          
 207          

⌨️ 快捷键说明

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