# I2C Comm

#### TucsonDon

##### Member
I am using a PIC18F47K42 as a master and a PIC18F46K22 as a slave with the following code from MMC for the master

C:
uint16_t i2c2_read2ByteRegister(uint8_t address, uint8_t reg)
{
uint16_t    result;
I2C2_Initialize();
wait4BusFree();
I2C2CNT = 1;
I2C2CON0bits.RSEN = 1;
I2C2CON0bits.S = 1; //Start
wait4Start();
sendByte(reg);
wait4MDRSetcount(2);
I2C2ADB1 = (uint8_t)(address| 0x01); //Change the R/W bit for read
I2C2CON0bits.S = 1; //Start
I2C2CON0bits.RSEN = 0;
result = receiveByte();//read MSB of the 2byte register
result = result<<8;
result = result | receiveByte();//read LSB of the 2byte register
wait4Stop();
return result;
}
AHEN, DHEN, SEN bits on the slave are set.

If you look at the trace you can see that the master send the write address (0x18) and the register to the slave with the slave ACK'ing but the master is not sending the read address, instead it appears to time out and send a P. I know that the master is waiting for something form the slave but not sure what. The slave is reading the buffer to clear the flag and is releasing the clock.

You can see in the trace that it then goes on to write to (0x50) the display.

If the slave NACK's then the master will send both the read and write address

#### Attachments

• 113.4 KB Views: 3