unsigned char E_Read(int addr, unsigned char *ch, char len)
{
unsigned char byte;
ErrFlags = 0; // Clear error
I2C_Start();
I2C_Write(CHIP_Write);
if(!I2C_nack()) return 1; //
I2C_Write((unsigned char) (addr >> 8 & 0xff)); // << here is high byte of address
if(!I2C_nack()) return 1;
I2C_Write((unsigned char) addr & 0xff); // << here is low byte of address
if(!I2C_nack()) return 1;
I2C_Start();
I2C_Write(CHIP_Read); //
if(!I2C_nack()) return 1;
if(len == 1) // Read a single char.
{
*ch = I2C_Read();
}
else if(len > 1) // Read a sequential data steam
{
while(len-- > 1)
{
*ch++ = I2C_Read(); // read character
I2C_ack(); // Send ack
}
*ch = I2C_Read();
}
else // Read s NULL string
{
do{
*ch = I2C_Read();
I2C_ack();
}while(*ch++ != 0);
*ch = I2C_Read();
}
I2C_Sendnack(); // send nack
I2C_Stop(); // send stop
return 0;
}