English
Language : 

PIC17C7XX Datasheet, PDF (175/328 Pages) Microchip Technology – High-Performance 8-Bit CMOS EPROM Microcontrollers with 10-bit A/D
PIC17C7XX
EXAMPLE 15-2: INTERFACING TO A 24LC01B SERIAL EEPROM (USING MPLAB-C17) (Cont.’d)
// 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)
WriteI2C(data);
}
IdleI2C();
StopI2C();
IdleI2C();
return;
}
// If 24LC01B ACKs
// Send data
// Wait for idle condition
// Send stop bit
// Wait for idle condition
// 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);
}
© 1998 Microchip Technology Inc.
DS30289A-page 175