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.

Problem with I2C on MSP430F5529

Other Parts Discussed in Thread: MSP430F5529, BQ24272

We need to communicate between MSP430F5529 and BQ24272 using I2C. I wrote functions to do I2C with MSP430F5529 as master and it worked well with Arduino. This is the code:

#include <msp430.h>
#include "Clock.h"
#include "uart.h"
//#include "I2C.c"

unsigned char TXData;
unsigned char RXData;

void I2C_init(unsigned char SA)
{
UCB1CTL1 |= UCSWRST; // Enable Software reset so that I2C can be configured
UCB1CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C as Master, synchronous mode
UCB1CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset so that Baud rate can be set
UCB1BR0 = 62; // fSCL = SMCLK/UCB1BR
UCB1BR1 = 0;
UCB1I2CSA = SA; // Sets slave address
UCB1CTL1 &= ~UCSWRST; // Clear Software Reset so that I2C can continue
UCB1IE |= UCTXIE; // Enable TX interrupt
UCB1IE |= UCRXIE; // Enable RX interrupt
}

void I2C_send(unsigned char Data)
{
TXData = Data; // Holds TXData
UCB1CTL1 |= UCTR + UCTXSTT; // Set transmitter mode, generate start condition
}

void I2C_receive()
{
UCB1CTL1 &= ~UCTR; // Sets receiver mode
UCB1CTL1 |= UCTXSTT; // Generate start condition
}

int main(void)
{

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

initCLK(); // Set clock to 25 MHz

P4SEL |= 0x06; // Assign I2C pins to USCI_B0

P4DIR ^= BIT4; // Check whether code is running by lighting an LED
P4OUT |= BIT4;
int i,j;
//for (i =20000;i>0;i--);
P4OUT &=~BIT4;

_bis_SR_register(GIE); // Enable interrupts

I2C_init(0x6B);

I2C_send(0x01);

I2C_receive();
uart_init();
UART_send_char(RXData);
}

//------------------------------------------------------------------------------
// The USCIAB0_ISR is structured such that it can be used to transmit any
// number of bytes by pre-loading TXByteCtr with the byte count.
//------------------------------------------------------------------------------
#pragma vector = USCI_B1_VECTOR
__interrupt void USCI_B1_ISR(void)
{
switch(__even_in_range(UCB1IV,12))
{
case 0: break; // Vector 0: No interrupts
case 2: break; // Vector 2: ALIFG
case 4: break; // Vector 4: NACKIFG
case 6: break; // Vector 6: STTIFG
case 8: break; // Vector 8: STPIFG
case 10:
RXData = UCB1RXBUF; // Reads data
UCB1IFG &= ~UCRXIFG; // Clears interrupt flag
break; // Vector 10: RXIFG

case 12: // Vector 12: TXIFG
// while (!(UCB1IFG & UCTXIFG)); // Checks whether start condition is generated
UCB1TXBUF = TXData; // First data to be transmitted can be written

while (UCB1CTL1 & UCTXSTT); // If slave acknowledges the address, the UCTXSTT is cleared

while (!(UCB1IFG & UCTXIFG)); // Checks whether data is put into the shift register from the buffer
UCB1CTL1 |= UCTXSTP; // Completes the transmission
UCB1IFG &= ~UCTXIFG; // Clears interrupt flag
break;
default: break;
}
}

But when I try to send the address of the register to be read from the BQ24272 through I2C, the code gets stuck in the line, "while (!(UCB1IFG & UCTXIFG)); // Checks whether data is put into the shift register from the buffer ", even though the execution of the previous line means that the slave has acknowledged the address.