新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > I2C 24LC02 C讀寫程序設(shè)計(jì)

I2C 24LC02 C讀寫程序設(shè)計(jì)

作者: 時(shí)間:2011-11-22 來源:網(wǎng)絡(luò) 收藏

/*************************************************************
起動(dòng)總線函數(shù)
函數(shù)原型: void start_i2c();
Function: start on the bus
*************************************************************/
void start_i2c()
{
SDA=1; //發(fā)送啟始條件的數(shù)據(jù)信號(hào)
nop();
SCL=1;
nop();nop();nop();nop();nop(); //要求建立時(shí)間大于4,7S
SDA=0; //發(fā)送起始信號(hào)
nop();nop();nop();nop();nop();
SCL=0; //鉗住總線,準(zhǔn)備發(fā)送數(shù)據(jù)或接收數(shù)據(jù)
nop();nop();
}


/*************************************************************
停止總線函數(shù)
函數(shù)原型: void stop_i2c();
Function: stop the bus
**************************************************************/
void stop_i2c()
{

SDA=0; //發(fā)送結(jié)束條件的數(shù)據(jù)信號(hào)
nop();
SCL=1;
nop();nop();nop();nop();nop();
SDA=1;
nop();nop();nop();nop();
}

/*====================================================
字節(jié)數(shù)據(jù)傳送函數(shù)
函數(shù)原型: void send_byte(uchar c);
Function: 將數(shù)據(jù)C發(fā)送出去,可以是地址,也可以是數(shù)據(jù),發(fā)完后等待回應(yīng),并對(duì)此狀態(tài)
位進(jìn)行操作(不應(yīng)答或非應(yīng)答都使ack=0 ),發(fā)送數(shù)據(jù)正常,ack=1;ack=0
表示被控器無應(yīng)答或損壞。
====================================================*/
void send_byte(uchar c)
{
uchar bit_count;
for (bit_count=0;bit_count8;bit_count++)
{
if ((cbit_count)0x80) {SDA=1;}
else {SDA=0;}
nop();
SCL=1;
nop();nop();nop();nop();nop();
SCL=0;
}
nop();nop();
SDA=1;
nop();nop();
SCL=1;
nop();nop();nop();
if (RC4==1) ack=0;
else ack=1; //用ASK=1為有應(yīng)答信號(hào)
SCL=0;
nop();nop();
}

/*================================================
字節(jié)數(shù)據(jù)接收函數(shù)
函數(shù)原型:uchar receive_byte();
FUNCTION: 用來接收從器件傳來的數(shù)據(jù),并判斷總線錯(cuò)誤(不發(fā)應(yīng)答信號(hào)),
發(fā)完后請(qǐng)用應(yīng)答函數(shù)。
====================================================*/
uchar receive_byte()
{
uchar retc,bit_count;
retc=0;
SDA=1;
for (bit_count=0;bit_count8;bit_count++)
{
nop();
SCL=0;
nop();nop();nop();nop();nop();
SCL=1;
nop();nop();
retc=retc1;
if (RC4==1) retc=retc+1;
nop();nop();
}
SCL=0;
nop();nop();
return (retc);
}


/*=================================================
向有子地址器件發(fā)送多字節(jié)數(shù)據(jù)函數(shù)
函數(shù)原型: bit I_send_str(uchar sla,uchar suba,uchar *s,uchar no);
Function: 從啟動(dòng)總線到發(fā)送地址,數(shù)據(jù),結(jié)束總線的全過程,從器件地址sla。如果
返回1表示操作成功,否則操作有誤。
================================================*/
void I_send_str(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
start_i2c();
send_byte(sla);
if (ack==0) i2c_error();
send_byte(suba);
if (ack==0) i2c_error();
for (i=0;ino;i++)
{
send_byte(*s);
if (ack==0) i2c_error();
s++;
}
stop_i2c();
// return(1);
}

/*****************************************************************
延時(shí)函數(shù)
函數(shù)原型: void delay_250ms();
FUNCTION: 延明250ms
*****************************************************************/
void delay_250ms()
{
unsigned int d=24999;
while (--d);
}

/*****************************************************************
總線錯(cuò)誤函數(shù)
函數(shù)原型: void i2c_error();
Function: 通過RD7閃動(dòng)8次表示總線操作失敗一次報(bào)警。
*****************************************************************/
void i2c_error ()
{
uchar i;
for (i=0;i8;i++)
{
RD7=0;
delay_250ms();
RD7=1;
delay_250ms();
}

}
/**********END**************/

本文引用地址:http://butianyuan.cn/article/150033.htm
塵埃粒子計(jì)數(shù)器相關(guān)文章:塵埃粒子計(jì)數(shù)器原理

上一頁(yè) 1 2 下一頁(yè)

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉