I'm trying to communicate with an external sensor with I2C interface, using the MSP430G2553 peripheral in polling mode (no interrupt).
The code hangs on the while (UCB0CTL1_bit.UCTXSTT == 1); polling line, just after the MSP sends the start command and then the slave address.
I checked the signals with an oscilloscope: the clock remains low just after the last address bit.
I know that after the UCTXSTT while loop I should also check the interrupt to verify if the slave has the acknowledged the address, but the main problem is that program doesn't exit the while loop.
I also noticed that many MSP users have the same problem in using the I2C in a polling mode.
Shouldn't be used in this way?
Is there any solution?
Hereafter the code:
--------
UCB0CTL1_bit.UCSWRST = 1; // USCI Software Reset
UCB0CTL0_bit.UCSYNC = 1; // Syncronous Mode
UCB0CTL0_bit.UCMODE0 = 1; // I2C Mode
UCB0CTL0_bit.UCMODE1 = 1; // I2C Mode
UCB0CTL0_bit.UCMST = 1; // Master Select
UCB0CTL0_bit.UCMM = 0; // Single Master Environment
UCB0CTL0_bit.UCSLA10 = 0; // 7 bit Slave Address
UCB0CTL0_bit.UCA10 = 0; // 7 bit Own Address
UCB0CTL1_bit.UCSSEL0 = 1; // SMCLCK Select
UCB0CTL1_bit.UCSSEL1 = 1; // SMCLCK Select
UCB0BR1 = 0;
UCB0BR0 = 80;
UCB0I2COA = 0x0000 + I2C_MSP430_ADDRESS; // 7 bit own address (1Bh = 27d) + General Call Disabled
UCB0CTL1_bit.UCSWRST = 0; // USCI Software un-Reset
...
int I2C_start_and_write (unsigned char argData, unsigned char argAddress) {
// // wait for the end of any previous activity on the bus
// while (UCB0CTL1_bit.UCTXSTP == 1);
// Set the slave address
UCB0I2CSA = argAddress;
// set UCTR for transmitter mode
UCB0CTL1_bit.UCTR = 1;
// set UCTXSTT to generate a START condition
// (the slave address is automatically sent after the START condition)
UCB0CTL1_bit.UCTXSTT = 1;
// wait for the end of any previous activity on the bus
while (UCB0CTL1_bit.UCTXSTT == 1);
// Write the data on the I2C bus
UCB0TXBUF = argData;
// Return true
return (1);
}