All variables shared between the interrupt handler and other code must be volatile. The interrupt flags are automatically cleared by reading/writing the corresponding RXBUF/TXBUF register, or by reading the interrupt vector register; you must not do that manually. This code is rather brittle; it will break if there is ever a wrong number of bytes. The RX/TX counts should be reset on a start condition, and a stop condition should reset everything. If the SDA line is stuck low, the slave did not see a clock pulse. This usually happens if the pull-up resistor is too large for the speed used; either use smaller ones, or reduce the speed. To check whether the I²C signals are OK, you must use an oscilloscope. (And don't design the circuit so that it barely works; use a large safety factor.) But in any case, the master should be able to handle this error; see section 3.1.16 of the I²C specification. (This problem sounds suspiciously like erratum USCI30, but your chip does not have it.)
↧