|
找個(gè)例子看看嘛,有時(shí)候自己卡住了不知道,看看別人的成功例子,就可以打開思路了。
共享一個(gè):
IIC.c文件內(nèi)容:
#include"i2c.h"
/*******************************************************************************
* 函數(shù)名 : Delay1us()
* 函數(shù)功能 : 延時(shí)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
*******************************************************************************/
void Delay10us()
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
/*******************************************************************************
* 函數(shù)名 : I2cStart()
* 函數(shù)功能 : 起始信號(hào):在SCL時(shí)鐘信號(hào)在高電平期間SDA信號(hào)產(chǎn)生一個(gè)下降沿
* 輸入 : 無(wú)
* 輸出 : 無(wú)
* 備注 : 起始之后SDA和SCL都為0
*******************************************************************************/
void I2cStart()
{
SDA=1;
Delay10us();
SCL=1;
Delay10us();//建立時(shí)間是SDA保持時(shí)間>4.7us
SDA=0;
Delay10us();//保持時(shí)間是>4us
SCL=0;
Delay10us();
}
/*******************************************************************************
* 函數(shù)名 : I2cStop()
* 函數(shù)功能 : 終止信號(hào):在SCL時(shí)鐘信號(hào)高電平期間SDA信號(hào)產(chǎn)生一個(gè)上升沿
* 輸入 : 無(wú)
* 輸出 : 無(wú)
* 備注 : 結(jié)束之后保持SDA和SCL都為1;表示總線空閑
*******************************************************************************/
void I2cStop()
{
SDA=0;
Delay10us();
SCL=1;
Delay10us();//建立時(shí)間大于4.7us
SDA=1;
Delay10us();
}
/*******************************************************************************
* 函數(shù)名 : I2cSendByte(unsigned char num)
* 函數(shù)功能 : 通過(guò)I2C發(fā)送一個(gè)字節(jié)。在SCL時(shí)鐘信號(hào)高電平期間,保持發(fā)送信號(hào)SDA保持穩(wěn)定
* 輸入 : num
* 輸出 : 0或1。發(fā)送成功返回1,發(fā)送失敗返回0
* 備注 : 發(fā)送完一個(gè)字節(jié)SCL=0,SDA=1
*******************************************************************************/
unsigned char I2cSendByte(unsigned char dat)
{
unsigned char a=0,b=0;//最大255,一個(gè)機(jī)器周期為1us,最大延時(shí)255us。
for(a=0;a<8;a++)//要發(fā)送8位,從最高位開始
{
SDA=dat>>7; //起始信號(hào)之后SCL=0,所以可以直接改變SDA信號(hào)
dat=dat<<1;
Delay10us();
SCL=1;
Delay10us();//建立時(shí)間>4.7us
SCL=0;
Delay10us();//時(shí)間大于4us
}
SDA=1;
Delay10us();
SCL=1;
while(SDA)//等待應(yīng)答,也就是等待從設(shè)備把SDA拉低
{
b++;
if(b>200) //如果超過(guò)2000us沒(méi)有應(yīng)答發(fā)送失敗,或者為非應(yīng)答,表示接收結(jié)束
{
SCL=0;
Delay10us();
return 0;
}
}
SCL=0;
Delay10us();
return 1;
}
/*******************************************************************************
* 函數(shù)名 : I2cReadByte()
* 函數(shù)功能 : 使用I2c讀取一個(gè)字節(jié)
* 輸入 : 無(wú)
* 輸出 : dat
* 備注 : 接收完一個(gè)字節(jié)SCL=0,SDA=1.
*******************************************************************************/
unsigned char I2cReadByte()
{
unsigned char a=0,dat=0;
SDA=1; //起始和發(fā)送一個(gè)字節(jié)之后SCL都是0
Delay10us();
for(a=0;a<8;a++)//接收8個(gè)字節(jié)
{
SCL=1;
Delay10us();
dat<<=1;
dat|=SDA;
Delay10us();
SCL=0;
Delay10us();
}
return dat;
}
/*******************************************************************************
* 函數(shù)名 : I2cReadRespon()
* 函數(shù)功能 : 接收完一個(gè)字節(jié)之后產(chǎn)生應(yīng)答,以便接著接收下一個(gè)字節(jié)
* 輸入 : 無(wú)
* 輸出 : 無(wú)
* 備注 : 接收完一個(gè)字節(jié)SCL=0
*******************************************************************************/
//void I2cReadRespon()
//{
// SDA=0;
// Delay10us();
// SDA=1;
// Delay10us();
//}
IIC.h文件內(nèi)容:
#ifndef __I2C_H_
#define __I2C_H_
#include<reg51.h>
sbit SCL=P2^1;
sbit SDA=P2^0;
void I2cStart();
void I2cStop();
unsigned char I2cSendByte(unsigned char dat);
unsigned char I2cReadByte();
void I2cReadRespon();
#endif
|
|