軟體模擬I2C匯流排操作。 (轉)
在微控制器應用中,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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- I2C 匯流排
- ECU通訊:CAN匯流排模擬測試
- I2C匯流排訊號時序分析
- 英特佩斯匯流排採集測試模擬工具
- 匯流排
- Linux驅動之I2C匯流排裝置以及驅動Linux
- 事件匯流排事件
- 前端匯流排前端
- MACH SYSTEMS—匯流排介面轉換工具Mac
- Keil的軟體模擬和硬體模擬
- FolkMQ v1.3.2 釋出(訊息中介軟體、事件匯流排)MQ事件
- 事件匯流排demo事件
- javascript事件匯流排JavaScript事件
- 將Abp預設事件匯流排改造為分散式事件匯流排事件分散式
- Vue事件匯流排(EventBus)Vue事件
- Vue 事件中央匯流排Vue事件
- ARM 匯流排協議協議
- 資料匯流排模式模式
- AHB匯流排協議協議
- AXI匯流排簡介
- Can匯流排介紹
- 微控制器學習(十一)I2C匯流排和AT24C02的使用
- Altair SimSolid 工程模擬軟體 衡祖模擬AISolid
- PID除錯軟體(C#、模擬、模擬)除錯C#
- 計算匯流排頻寬
- 大資料匯流排(DataHub)大資料
- CAN匯流排原理_學習
- 序列匯流排的學習
- CPU主頻,倍頻,外頻,系統匯流排頻率,前端匯流排頻率前端
- 你玩過輕量系統軟匯流排應用嗎?
- 攝影棚布光模擬用什麼軟體好?棚布光模擬軟體推薦!
- Flutter中的事件匯流排(EventBus)Flutter事件
- SOFA 原始碼分析— 事件匯流排原始碼事件
- Otto事件匯流排框架的使用事件框架
- 伺服器匯流排技術伺服器
- 企業服務匯流排ESB
- 模擬軟體應用案例輯
- 核心下gpio模擬i2c驅動修改