初级会员

- 积分
- 126
- 金钱
- 126
- 注册时间
- 2017-7-21
- 在线时间
- 48 小时
|
4金钱
为什么24CL256写入的数据与读出的数据不符,读出的数据是写入数据的两倍加1 ,比如我写入 5;6;7;8;读出的数据是11;13;15;17;/******************************************************************/
cucBit[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
/******************************************************************/
unsigned char I2cSend(unsigned char ucData)
{
unsigned char i;
for (i = 0; i < 8; i++)
{
SCL1=0;
delay_5us();
if ( (ucData & cucBit[i]) != 0)
{
SDA1=1;
}
else
{
SDA1=0;
}
delay_5us();
SCL1=1;
delay_5us();
}
SCL1=0;
return true;
}
/******************************************************************/
unsigned char I2cReceive(void)
{
unsigned char ucData=0x00;
unsigned char i;
SDA1=1;
TRISC4=1;
for ( i = 0; i < 8; i++)
{
SCL1=0;
delay_5us();
SCL1=1;
delay_5us();
if (SDA1==1)
{
ucData |= cucBit[i];
}
}
SCL1=0;
TRISC4 = 0;
return ucData;
}
void Write_EEPROM(uchar addr,uint temp)
{
I2cStart();
I2cSend(0xa0);
WaitAck();
I2cSend(addr>>8);
WaitAck();
I2cSend(addr&0x00ff);
WaitAck();
I2cSend(temp);
WaitAck();
I2cStop();
delay_ms(5);
}
/*************************************************************/
uchar Read_EEPROM(uchar addr)
{
uchar temp;
I2cStart();
I2cSend(0xa0);
WaitAck();
I2cSend(addr>>8);
WaitAck();
I2cSend(addr&0x00ff);
WaitAck();
I2cStart();
I2cSend(0xa1);
WaitAck();
delay_5us;
SendNotAck();
temp = I2cReceive();
I2cStop();
delay_ms(5);
return temp;
}
|
|