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.

MSP430FR573 problem in I2C receive master mode, eUSCI_B I2C

Other Parts Discussed in Thread: MSP430FR5739

Dear experts,

More  time a need your help to solve a problem that a get in my code write to connected a MSP430FR as slave and ADXL312 accelerometer as slave.

The Problem is :

When the master, msp430fr5739, send a  trama or command to slave to resquest your ID the slave send the correct ID that in my case is 0xE5.

To finish the communication  after master receive the ID send a NACK  followed by STOP Condition. But the two communication line, SDA and SCL held low and to back read the ID I need turn off the MCU power because using reset button only idle SCL High keeping low the SDA line.

Please help me.

Below is my code:

#include <msp430fr5739.h>

unsigned char buffer[3],byte,flag_ack,flag_start=0;

unsigned int Data_Receive_From_Slave=0;

void I2C_init(void)
{
P1SEL1 = BIT6 + BIT7;

UCB0CTLW0 |= UCSWRST ;
UCB0CTLW0 |= UCSSEL_2 + UCMODE_3 + UCMST +UCSYNC + UCSWRST;
UCB0CTLW0 |= UCSWACK;

UCB0BRW = 0x0008;

UCB0I2CSA = 0x53;

UCB0CTLW0 &= ~UCSWRST;
}

unsigned char I2C_Write_Stat(unsigned int slave_address, unsigned char firstdata)
{
UCB0CTLW0 |= UCTR;
UCB0CTLW0 |= UCTXSTT;

while(UCB0CTLW0 & UCTXSTT){}

while((UCB0IFG & UCTXIFG0)==0);

UCB0TXBUF = firstdata;

if((UCB0IFG & UCNACKIFG)==0) // acknowledge
{
return 0;
}
else { return 1;}
}

unsigned char I2C_Write(unsigned char data)
{
UCB0TXBUF = data;

while((UCB0IFG & UCTXIFG0) == 0);
}

unsigned I2C_Read_Byte(unsigned byte)
{
while((UCB0STAT & UCBBUSY)==1);

UCB0CTLW0 |= UCTXSTP;

UCB0CTLW0 |= UCTXSTT;

UCB0CTLW0 &= ~UCTR;

while(UCB0CTLW0 & UCTXSTT){}

//
byte = UCB0RXBUF;

while(!(UCB0IFG & UCRXIFG)){}

byte= UCB0RXBUF;

UCB0CTLW0 |= UCTXSTP;
}
void I2C_Stop()
{
if((UCB0STAT & UCBBUSY)==1);

UCB0CTLW0 |= UCTR;

UCB0CTLW0 |= UCTXSTP;
UCB0IFG &= ~UCTXIFG;

while((UCB0CTLW0 & UCTXSTP)!=0);
}

void Repeat_Start_Condition(unsigned char slave_address)
{
UCB0CTLW0 &= ~UCTR;
UCB0CTLW0 |= UCTXSTT;

while((UCB0CTLW0 & UCTXSTP)!=0);
while(!(UCB0IFG & UCRXIFG0)){}

Data_Receive_From_Slave= UCB0RXBUF;

while((UCB0IFG & UCRXIFG0)!=1); // wait bit clear
}

void I2C_Send_Nack()
{
UCB0CTLW0 |= UCTR;
UCB0CTLW0 |= UCTXNACK;

if(( UCB0CTLW0 & UCTXNACK)==1)
while(( UCB0CTLW0 & UCTXNACK)==1);
else
{
while(!(UCB0CTLW0 & UCTXNACK));
}
}

void system_init()
{
WDTCTL = WDTPW | WDTHOLD;

CSCTL0_H = 0xA5; // Init SMCLK = MCLk = ACLK = 1MHz
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting = 8MHz
CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = MCLK = DCO
CSCTL3 = DIVA_3 + DIVS_3 + DIVM_3; // set all dividers to 1MHz
}
/*
* main.c
*/
int main(void)
{
system_init();

P4DIR &=~(BIT0 +BIT1);
P4OUT |= BIT0 +BIT1;
P4REN |= BIT0 + BIT1;
P4IN=0x00;

I2C_init();

while(1)
{

/*****************************************************************************
* *
* Master send data to the Slave and check if receive acknowledge *
* *
*****************************************************************************/

if((P4IN & BIT0)==0) // Button Start
{
flag_ack=I2C_Write_Stat(0x53,0x1E);
_delay_cycles (25);

if(flag_ack==0)
{
I2C_Write(0xff);
I2C_Stop();
}

}

/*****************************************************************************
* *
* Master Read Data From Accelerometer ADXL312 *
* *
*****************************************************************************/

if((P4IN & BIT1)==0) // Button Start
{
flag_ack=I2C_Write_Stat(0x53,0x00);
_delay_cycles (25);

if(flag_ack==0)
{

Repeat_Start_Condition(0x53);

I2C_Send_Nack();

flag_ack=0;

I2C_Stop();
}
}
}

}

And the two communications

.line held low 

And after receive the 

  • I didn't analyze your code, but yiur problem smells liek a racing condition.

    Take a closer look at the diagrams in the users guide.

    When a byte has been received, the USCI immediately continues to receive the next byte. So reception of the next byte has started befor your code notices that the first byte arrived. Depending on timing, the USCI may reach a point where teh bus stalls until you read teh surplu sbytes from RXBUF. It doesn't matter that you set UCTXSTP in the meantime - it won't be sent until you handled the received bytes.

**Attention** This is a public forum