unsigned char EEByteWrite( unsigned char control, unsigned char address, unsigned char data )
{
IdleI2C(); // ensure module is idle
StartI2C(); // initiate START condition
while ( SSPCON2bits.SEN ); // wait until start condition is over
if ( PIR2bits.BCLIF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
else // start condition successful
{
if ( WriteI2C( control ) ) // write byte - R/W bit should be 0
{
return ( -3 ); // set error for write collision
}
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition received
{
if ( WriteI2C( address ) ) // write word address for EEPROM
{
return ( -3 ); // set error for write collision
}
IdleI2C(); // ensure module is idle
if ( !SSPCON2bits.ACKSTAT ) // test for ACK condition received
{
if ( WriteI2C( data ) ) // data byte for EEPROM
{
return ( -3 ); // set error for write collision
}
}
else
{
return ( -2 ); // return with Not Ack error condition
}
}
else
{
return ( -2 ); // return with Not Ack error condition
}
}
IdleI2C(); // ensure module is idle
StopI2C(); // send STOP condition
while ( SSPCON2bits.PEN ); // wait until stop condition is over
if ( PIR2bits.BCLIF ) // test for bus collision
{
return ( -1 ); // return with Bus Collision error
}
return ( 0 ); // return with no error
}