This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

USCI I2C : Sending single byte without ISR

Hi!

I'm trying to send a single byte over UCSI B0 configured as I2C with the following function. The initialization code of the I2C is not shown. My problem is that if I run the function without debugging line-by-line, only the START condition and the address are transmitted and then comes the STOP condition (as observed at the scope). On the other hand, if I debug the function with the debugger, I can see that after the address, then comes the transmitted data and then the STOP condition.

I guess that the problem is that I set the UCTXSTP bit before the data in the TXBUF has started transmission, that's why only the address is transmitted and then the STOP condition. However, I cannot understand where is the error in my code  and what else should I do, since after loading my data at the TXBUF, I wait until the TXSTT bit has been cleared and then I set the TXSTP bit. Is there another way to understand when the data in the TXBUF has started to be transmitted?

By the way I don't wish to have an ISR handling with all these, since I call this function from another ISR and this creates some difficulties.

Thank you,

Nikos

 

//===========================================================================

void I2c_TxSingleByte (u8t txByte)

//===========================================================================

{

UCB0CTL1 |= UCTR + UCTXSTT; // I2C transmitter mode (TX), send START condition

while (UCB0IFG & !UCTXIFG); // Wait until start condition has been sent

UCB0IFG &= ~UCTXIFG; // Clear USCI TX int flag

UCB0TXBUF = *PTxData++; // Load TX buffer

while (UCB0CTL1 & !UCTXSTT); // Ensure start condition got sent

UCB0IFG &= ~UCTXIFG; // Clear USCI TX int flag

UCB0CTL1 |= UCTXSTP; // I2C stop condition

while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent

}

  • You're too fast.

    Seeing UCTXSTT cleared means that the slave has responded to the start sequence. The sending of the byte in TXBUF hasn't begun right now. If you set UCTXSTP now, the hardware won't start it.

    After seeing UCTXSTT cleared, you need to wait for UCTXIFG set, which means that the previously written content of TXBUF has been moved to the output. THEN you can set UCTXSTP for the transmission to end after the current byte is sent.

    Clearing UCTXIFG is superfluous, It will be cleared automatically as soon as UCTXSTP is set since there is nothing more to send. But first you have to wait for it to be set.

**Attention** This is a public forum