Hi,
You have to properly follow recommended interrupt service flow, as shown in Fig 44-13 of the RM.
So if a master receiver wants to terminate a data transfer, it must inform the slave transmitter by not
acknowledging the last byte of data which can be done by setting the NOACK bit before reading the 2nd last byte of data. This read initiates next byte (last) data receiving. In this case the slave transmits the byte (last) which is not acknowledged by master. In slave transmitter routine, the received acknowledge bit (RXAK) must be tested before transmitting the next byte of data. Setting RXAK means an 'end of data' signal from the master receiver, after which slave must be switched from transmitter mode to receiver mode by software. A dummy read then releases the SDA line so that the master can generate a STOP signal.
So your Slave_transmit and Master_Receive function should be modified little bit as
void I2C_Slave_Transmit(void)
{
uint8_t dummy;
//I2C_3.IBSR.B.IBIF = 0; // writing 0 have no effect
I2C_3.IBDR.R = TxData;
TxCount = TxCount - 1;
while (I2C_3.IBSR.B.IBIF == 0);
if (I2C_3.IBSR.B.RXAK == 1)
{
I2C_3.IBCR.B.TXRX = 0;
dummy = I2C_3.IBDR.R;
}
I2C_3.IBSR.B.IBIF = 1;
}
void I2C_Master_Receive(void)
{
uint8_t RxData;
// I2C_2.IBSR.B.IBIF = 0; // writing 0 have no effect
if (RxCount == 2)
{
I2C_2.IBCR.B.NOACK = 1;
}
if (RxCount == 1)
{
I2C_2.IBCR.B.MSSL = 0; // generate STOP
I2C_2.IBCR.B.NOACK = 0;
}
RxData = I2C_2.IBDR.R;
RxCount = RxCount - 1;
}
Then Stop function is not used in main().
BR, Petr