I'm trying to set up I2C communication between an MSP430F5510 (set as master), and one slave.
I have 10K pullups on both SDA and SCL
To begin with I'm just trying to write to the slave device.
I would like to write to the slave just by polling register values and without the use of interrupts. Please see my code at the end of this message.
The problem I'm having is that I initiate a write start condition with UCB1CTL1 |= UCTR + UCTXSTT;
I then check to make sure the start condition was sent with while(UCB1CTL1 & UCTXSTT); The code hangs up here. For some reason the UCTXSTT bit is never cleared.
If I comment out this line and single step through the code everything works just fine. But as soon as I try to run it full speed of course it messes up.
One strange thing is if I switch to a read operation with UCB1CTL1 &= ~UCTR and then check to see if the start condition was sent, the code while(UCB1CTL1 & UCTXSTT); does not hang. Can anyone explain why this is the case?
#include <msp430f5510.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
P4SEL |= 0x06; // Assign I2C pins to USCI_B0
UCB1CTL1 |= UCSWRST; // Enable SW reset
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB1BR0 = 12; // fSCL = SMCLK/12 = ~100kHz
UCB1BR1 = 0;
UCB1I2CSA = 0x20; // Slave Address is 20h
UCB1CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
//**Transmit 0x03 to slave, which is the pointer to the read address
while (UCB1CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB1CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
while(UCB1CTL1 & UCTXSTT); // Start condition sent? <-*CODE HANGS HERE*
//**Write to configuration register
UCB1TXBUF = 0x03;
while (UCB1IFG & !UCTXIFG); // Wait for TX buffer to empty
//**Set up inputs and outputs
UCB1TXBUF = 0x70;
while (UCB1IFG & !UCTXIFG); // Wait for TX buffer to empty
//**Send stop condition then start condition
UCB1CTL1 |= UCTXSTP; // I2C stop condition
while (UCB1CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB1CTL1 |= UCTXSTT; // I2C TX, start condition
while(UCB1CTL1 & UCTXSTT); // Start condition sent? <-*CODE HANGS HERE*
//**Write to output register
UCB1TXBUF = 0x01;
while (UCB1IFG & !UCTXIFG); // Wait for TX buffer to empty
//**Turn on LED's
UCB1TXBUF = 0x0A;
while (UCB1IFG & !UCTXIFG); // Wait for TX buffer to empty
UCB1CTL1 |= UCTXSTP; // I2C stop condition
while (UCB1CTL1 & UCTXSTP); // Ensure stop condition got sent
while(1);
}