專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

IIC總線通信模擬

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2012年01月02日   【字體:
語言:C語言
 
      簡介:IIC總線作為一種通信規(guī)范,廣泛應(yīng)用于各種芯片之間以及內(nèi)部通信?梢院敛豢鋸埖闹v,不懂IIC總線的就不算是學(xué)過電子!
    /**************************************************************************/
       #include"reg51.h"
       #include "intrins.h"

       /**************************端口定義************************************/
        sbit SDA=P1^0;             //IIC數(shù)據(jù)總線
        sbit SCL=P1^1;             //IIC時(shí)鐘總線
        /**************************數(shù)組定義************************************/
        unsigned char wdata[8]={0x32,0xb5,0xab,0xd0,0xd6,0x50,0x3a,0xd3};
        unsigned char rdata[8];  
 
        /**************************函數(shù)聲明************************************/
        void iic_start(void);
        void iic_stop(void);
        bit iic_write_byte(unsigned char wdata);
        unsigned char iic_read_byte(bit re_data);
        bit iic_read_some_bytes(unsigned reg_add,unsigned char *rdata,unsigned char len);
        bit iic_write_some_bytes(unsigned reg_add,unsigned char *wdata,unsigned char len);   
 
       /**************************主函數(shù)**************************************/
        void main(void)
       {
        ..................
       }
 
       /*************************IIC函數(shù)**************************************/
       void iic_start(void)                            
    {
       SDA=1;
       _nop_();
       SCL=1;
       _nop_();
      _nop_();
      SDA=0;
      _nop_();
      _nop_();
      SCL=0;
     _nop_();
    }
   //_______________________________________________________________________
void iic_stop(void)                                    
{
   SDA=0;
  _nop_();
  _nop_();
  SCL=1;
  _nop_();
  _nop_();
  SDA=1;
  _nop_();
  _nop_();
  _nop_();
  _nop_();
}
//_______________________________________________________________________
bit iic_write_byte( unsigned char wdata )            
{
  unsigned char i=0;
   for ( i =0 ;i < 8 ;i ++ )
  {
 if ( wdata & 0x80 )
 {
  SDA=1;
  _nop_();
 }
 else
 {
  SDA=0;
  _nop_();
 }
  
 wdata <<= 1;
  SCL=1;   
  _nop_();
  SCL=0;
  _nop_();
  
   }
   _nop_();
   SDA=1;
   _nop_();
   SCL=1;
   _nop_();
      if(!SDA==1)
   {    
     SCL=0;
  _nop_();
  return 1;         
   }
   else             
   {
  SCL=0;
  _nop_();
  return 0;
   }
}
//_______________________________________________________________________
// iic_read_byte re_data -- whether send ack bit
unsigned char iic_read_byte(bit re_data)    
{
  unsigned char rdata=0,i=0;
  SDA=1;
   for(i=0;i<8;i++)
  {
      rdata <<=1;
      SCL=0;
      _nop_();
      SCL=1;
      _nop_();
     if(SDA==1)
     {
           rdata++;
      }
     _nop_();
  }
  _nop_();
  SCL=0;
  _nop_();
  if(re_data)
  {
       SDA=1;
      _nop_();
  }
  else
  {
      SDA=0;
     _nop_();
  }
  _nop_();
  SCL=1;
  _nop_();
  SCL=0;
  _nop_();
  return(rdata);
}
//_______________________________________________________________________________________________________
bit iic_write_some_bytes(unsigned reg_add,unsigned char *wdata,unsigned char len) 
{
  unsigned i;
  iic_start();
  if(!iic_write_byte(0xA0))
  {
      iic_stop();
   _nop_();
   return 0;
  }
  if(!iic_write_byte(reg_add))
  {
      iic_stop();
   _nop_();
   return 0;
  }
  for(i=0;i<len;i++)
  {
     if(!iic_write_byte(wdata[i]))
  {
       iic_stop();
    _nop_();
    return 0;
  }
  }
  iic_stop();
  _nop_();
  return 1;
}
//________________________________________________________________________________________________________
bit iic_read_some_bytes(unsigned reg_add,unsigned char *rdata,unsigned char len)   
{
  unsigned char i;
  iic_start();
  if(!iic_write_byte(0xA0))
  {
     iic_stop();
  _nop_();
  return 0;
  }
  if(!iic_write_byte(reg_add))
  {
     iic_stop();
  return 0;
  }
  iic_stop();
  iic_start();
  if(!iic_write_byte(0xA0|1))
  {
     iic_stop();
  return 0;
  }
  for(i=0;i<len-1;i++)
  {
     rdata[i]=iic_read_byte(0);
  _nop_();
  }
  rdata[i]=iic_read_byte(1);
  iic_stop();
  return 1;
 
}
        以上IIC總線模擬帶有數(shù)據(jù)傳輸失敗處理機(jī)制,是標(biāo)準(zhǔn)的IIC總線規(guī)范。在實(shí)際使用過程中,對(duì)于ACK應(yīng)答信號(hào)的處理往往可以
 
簡化!另外,對(duì)于不同的單片機(jī)或者主控芯片,可能涉及端口輸入輸出設(shè)置問題。在IIC讀寫數(shù)據(jù)時(shí)就會(huì)出現(xiàn)端口輸入與輸出的切換。
 
例如STC宏晶單片機(jī)就會(huì)涉及設(shè)置問題!以上使用的是標(biāo)準(zhǔn)51單片機(jī),不涉及輸入輸出設(shè)置問題。
關(guān)閉窗口

相關(guān)文章