Re: MCC 產生之 I2C Slave 範例 code 請益

作者 sarsgg444 於 2018年06月01日 15:14:37
剛剛修改了一下 Code

在 mcc 產生的 i2c2.c 內的 switch case 中

將 case S_SLAVE_TRANSMIT_MODE: 的最後一段加上清除 I2C2bits.I2COV 的動作後

I2C 程式就能正常讀寫了

case S_SLAVE_TRANSMIT_MODE:

// this is the state where an ACK or NACK is expected
// to occur after the slave has placed data to the
// transmit register.

// if the transaction was ACK'ed, more data needs to be sent
// if the transaction was NACK'ed then we don't need to send
// more data
if (I2C2_ACKNOWLEDGE_STATUS_BIT == 0)
{
// prepare next data
I2C2_StatusCallback(I2C2_SLAVE_TRANSMIT_REQUEST_DETECTED);

// transmit more data
I2C2_TransmitProcess();

}
else //if (I2C2_ACKNOWLEDGE_STATUS_BIT == 1)
{
// no more data to be sent so we go to idle state
i2c2_slave_state = S_SLAVE_IDLE;
}


if( I2C2_RECEIVE_OVERFLOW_STATUS_BIT == true )
{
dummy = I2C2_RECEIVE_REG;
I2C2_RECEIVE_OVERFLOW_STATUS_CLR;
}


break;

不確定是什麼原因

在 Master 向 Slave 發起 read 請求後

RBF,I2COV 被設為 1,且直到 Read 結束後沒有被清除

導致後續 Master 發出 ADDRESS 請求,也不會收到 Slave 端的 ACK

目前翻 datasheet 也只翻到 RBF,I2COV 被設為 1 的原因

Overrun is indicated if either (or both):
•The buffer full bit, RBF (I2CxSTAT<1>), was set before the transfer was received
•The overflow bit, I2COV (I2CxSTAT<6>), was set before the transfer was received

來自: http://www.microchip.com.tw/newbb/viewtopic.php?forum=8&topic_id=22307&post_id=78211