Datasheet

1998-2013 Microchip Technology Inc. DS30289C-page 177
PIC17C7XX
// Writes the byte data to 24LC01B at the specified address
void ByteWrite(static unsigned char address, static unsigned char data)
{
StartI2C(); // Send start bit
IdleI2C(); // Wait for idle condition
WriteI2C(CONTROL); // Send control byte
IdleI2C(); // Wait for idle condition
if (!SSPCON2bits.ACKSTAT) // If 24LC01B ACKs
{
WriteI2C(address); // Send control byte
IdleI2C(); // Wait for idle condition
if (!SSPCON2bits.ACKSTAT) // If 24LC01B ACKs
WriteI2C(data); // Send data
}
IdleI2C(); // Wait for idle condition
StopI2C(); // Send stop bit
IdleI2C(); // Wait for idle condition
return;
}
// Reads a byte of data from 24LC01B at the specified address
unsigned char ByteRead(static unsigned char address)
{
StartI2C(); // Send start bit
IdleI2C(); // Wait for idle condition
WriteI2C(CONTROL); // Send control byte
IdleI2C(); // Wait for idle condition
if (!SSPCON2bits.ACKSTAT) // If the 24LC01B ACKs
{
WriteI2C(address); // Send address
IdleI2C(); // Wait for idle condition
if (!SSPCON2bits.ACKSTAT) // If the 24LC01B ACKs
{
RestartI2C(); // Send restart
IdleI2C(); // Wait for idle condition
WriteI2C(CONTROL+1); // Send control byte with R/W set
IdleI2C(); // Wait for idle condition
if (!SSPCON2bits.ACKSTAT) // If the 24LC01B ACKs
{
getcI2C(); // Read a byte of data from 24LC01B
IdleI2C(); // Wait for idle condition
NotAckI2C(); // Send a NACK to 24LC01B
IdleI2C(); // Wait for idle condition
StopI2C(); // Send stop bit
IdleI2C(); // Wait for idle condition
}
}
}
return(SSPBUF);
}
EXAMPLE 15-2: INTERFACING TO A 24LC01B SERIAL EEPROM (USING MPLAB C17)