微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > I2C 24LC02 C读写例程(PIC单片机)

I2C 24LC02 C读写例程(PIC单片机)

时间:11-13 来源:互联网 点击:

=====================*/

uchar receive_byte()

{

uchar retc,bit_count;

retc=0;

SDA=1;

for (bit_count=0;bit_count<8;bit_count++)

{

nop();

SCL=0;

nop();nop();nop();nop();nop();

SCL=1;

nop();nop();

retc=retc<1;

if (RC4==1) retc=retc+1;

nop();nop();

}

SCL=0;

nop();nop();

return (retc);

}

/*================================================================

向有子地址器件发送多字节数据函数

函数原型: bit I_send_str(uchar sla,uchar suba,uchar *s,uchar no);

Function: 从启动总线到发送地址,数据,结束总线的全过程,从器件地址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;i

{

send_byte(*s);

if (ack==0) i2c_error();

s++;

}

stop_i2c();

// return(1);

}

/*****************************************************************

延时函数

函数原型: void delay_250ms();

FUNCTION: 延明250ms

*****************************************************************/

void delay_250ms()

{

unsigned int d=24999;

while (--d);

}

/*****************************************************************

总线错误函数

函数原型: void i2c_error();

Function: 通过RD7闪动8次表示总线操作失败一次报警。

*****************************************************************/

void i2c_error ()

{

uchar i;

for (i=0;i<8;i++)

{

RD7=0;

delay_250ms();

RD7=1;

delay_250ms();

}

}

/**********END**************/

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top