軟體模擬I2C匯流排操作。 (轉)

worldblog發表於2007-12-14
軟體模擬I2C匯流排操作。 (轉)[@more@]

  在微控制器應用中,I2C(I方C)匯流排一種簡單,雙向的二線制同步匯流排,它只需要兩根序列線,脈衝線,就可以在匯流排與連線的器件之間傳送資訊, 它不外乎有以下幾個特點:
a.每個連線到匯流排上的器件都可以進行唯一的定址,還可建立起簡單的主從關係,主器件既可作為傳送器,也可作為接收器.
b.它帶競爭電路和仲裁電路,可以接收多個主器件傳送的資料,而這些資料不會產混亂.
c.同步脈衝可以允許器件可以透過匯流排以不同的波特率進行通訊.
d.因為只有兩根線,連線簡單,方便.
  對於MCS51系列來講,內部並沒I2C匯流排介面,在這種情況下,可以採用模擬的方法來寫出I2C匯流排的操作,下面我的就在開始處定義哪個引腳作為資料線SDA,哪個引腳作為脈衝線,請看我的程式(C51寫的,總共有8個來模擬I2C匯流排):
Common.h File
#pragma  LA SB OE CD OT(SPEED,6)
//LA indicate LARGE Compling Mode-All local and global variables are located in external-ram.
//DB indicate DE
//SB indicate SYMBOL
//OE indicate EXTEND
//CD indicate CODE  //Generate ASM Code
//OT indicate OPTIMIZATION
#include
#include
#include
#include
#pragma REGPARMS
#pragma SAVE
#define BOOL bit
#define BYTE unsigned char
#define UINT unsigned int
#define ULONG unsigned long
#define HIGH 1
#define LOW 0
#define TRUE 1
#define FALSE 0
#define MAXLONGS  2147483647
#define MINLONGS  -2147483648
#define MAXINTS  32767
#define MININTS  -32768
#define MAXLONGU  4294967295
#define MAXINTU  65535
#define BLOCKLOW 0x0000 //The start-address in AT24C64 is 0x0000
#define BLOCKSIZE 0x20 //Each block is 32 bytes
#define BLOCKNUM  0xC8 //At best 200,Only Save the newest 200 records.
#define BLOCKHIGH 0x18FF//The end-address in AT24C64 is 0x18FF
#define OSC  24000000 //Osillcator Frequency
#define BAUDRATE  9600
#define OTV  256-OSC/12/BAUDRATE/32 //character O represents Obtain,character T represents Timer,character V represents Value
//IMPORTANT ANNOUNCEMENT:I use a 0x1FFC,0x1FFD to save the next block's address and 0x1FFE,0x1FFF to save the block's no.
sbit GUN=0x85;//PIN P05
sbit SCL=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
sbit SDA=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
sbit SHKL=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
sbit SHKH=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
sbit KH0=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
sbit KH1=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
sbit KH2=0xFF;  //Undefined bit-address,which will be defined in function:DEFI2C
extern void DEFI2C(char *name) //To define which pin is SDA and which pin is SCL
extern void I2CInit()  //Initialization For I2C Bus
extern void I2CStart()  //Start I2C Bus
extern void I2CStop();  //Stop  I2C Bus
extern bit  I2CClock(); //return SDA while SCL is HIGH
extern void I2CDelay(); //Delay
extern void I2CAck();  //Answer
extern bit  I2CSend(BYTE I2CData);//Send data with I2C Bus
extern BYTE I2CReceive();  //Receive data

Common.c
void DEFI2C(char *name)
{
  //Todo:add your codes here....
  //Announcement:If you question me why use the function DEFI2C?
  //Well,the function will be used to define which PIN is SCL and which PIN is SDA,especially there are more than 1 I2C
  //equipment in your circuit.In this sitiuation,we write a function for defining I2C Bus named DEFI2C....
}
void I2CStart()
{
  SCL=HIGH;
  I2CDelay();
  SDA=LOW;
  I2CDelay();
  SCL=LOW;
  I2CDelay();
}
void I2CStop()
{
  SDA=LOW;
  I2CDelay();
  SCL=HIGH;
  I2CDelay();
  SDA=HIGH;
  I2CDelay();
  SCL=LOW;
  I2CDelay();
}
void I2CInit()
{
  SCL=LOW;
  I2CStop();
}
bit I2CClock() //return SDA while SCL is HIGH
{
  bit sample;
  SCL=HIGH;
  I2CDelay();
  sample=SDA;
  SCL=LOW;
  I2CDelay();
  return sample;
}
bit I2CSend(BYTE I2CData)
{
  BYTE i;
  for(i=0;i<8;i++)
  {
  SDA=(bit)(((I2CData) & 0x80) / 0x80) ;
  I2CData=I2CData << 1;
  I2CClock();
  }
  SDA=HIGH;
  return (~I2CClock());
}
void I2CAck()
{
  SDA=LOW;
  I2CClock();
  SDA=HIGH;
}
void I2CDelay()
{
  BYTE ll;
  for(ll=0;ll<100;ll++)
  {
  ;
  }
}
BYTE I2CReceive()
{
  BYTE I2CData=0;
  BYTE kk;
  for(kk=0;kk<8;kk++)
  {
  I2CData*=2;
  if(I2CClock())
  {
  I2CData++;
  }
  }
  return I2CData;
}


上面的程式碼是用軟體來模擬I2C匯流排,在對特定I2C器件進行操作時,一定要和該器件的時序對應起來才可以,本人從事微控制器開發不久,上述程式是去年在北京時寫的,如有錯誤還請各位大俠,前輩給予指教.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-993456/,如需轉載,請註明出處,否則將追究法律責任。

相關文章