本帖最后由 action99 于 2009-8-20 15:13 编辑
做了一个pic18f4420与AT24C01通讯程序,但是写入的是0X48读出来的就是00,,请高手帮我指点一下?谢谢
i2c_write(0x05,0x48);
Nop();
Read_data0=i2c_read(0x05);
//*********************************
/*=======================================================
i2c_write(地址,数据),写一个字节
=======================================================*/
void i2c_write(unsigned char Address,unsigned char Data){
ack3:
i2c_start();
i2c_send8bit(0xA0);
i2c_ack();
if(F6.Bit.ack_f)goto ack3;
i2c_send8bit(Address);
i2c_ack();
i2c_send8bit(Data);
i2c_ack();
i2c_stop();
return;
}
/*=======================================================
i2c_read(地址,数据),写一个字节
=======================================================*/
unsigned char i2c_read(unsigned char Address){
unsigned char c;
ack1:
i2c_start();
i2c_send8bit(0xA0);
i2c_ack(); //=1,表示无确认,再次发送
if(F6.Bit.ack_f)goto ack1;
i2c_send8bit(Address);
i2c_ack();
ack2:
i2c_start();
i2c_send8bit(0xA1);
i2c_ack();
if(F6.Bit.ack_f)goto ack2;
c=i2c_receive8bit();
i2c_ack();
i2c_stop();
return(c);
}
//=======================================================
//发送开始信号
void i2c_start(void) {
SDA_C_PC0=0;
SDA_PC0 = 1;
SCL_PC1 = 1;
SDA_PC0 = 0;
SCL_PC1 = 0;
}
//发送结束信号
void i2c_stop(void) {
SDA_C_PC0=0;
SDA_PC0=0;
SCL_PC1 = 1;
SDA_PC0 = 1;
Delay100TCYx(1);
}
//发送接收确认信号
void i2c_ack(void) {
SDA_C_PC0=1;
SCL_PC1=1;
if(SDA_PC0)
F6.Bit.ack_f = 1;
else
F6.Bit.ack_f = 0;
SCL_PC1=0;
return (F6.Bit.ack_f);
}
//送八位数据
void i2c_send8bit(unsigned char b){
unsigned char i;
Sign=b;
SCL_PC1=0;
SDA_C_PC0=0;
for(i=0;i<8;i++){ //送出地址
if(Sign & cSetb7)
SDA_PC0 = 1;
else
SDA_PC0 = 0;
SCL_PC1=1;
SCL_PC1=0;
Rlncf(Sign,1,1);
}
}
//接收八位数据
unsigned char i2c_receive8bit() {
unsigned char i;
unsigned char j=0;
SDA_C_PC0=1;
for(i=0;i<8;i++){
SCL_PC1 = 1;
Rlncf(Sign,1,1);
if(SDA_PC0)
Sign|=cSetb0;
else
Sign&=cClrb0;
SCL_PC1 = 0;
}
SCL_PC1= 0;
return Sign;
} |