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.

msp430F5438 I2C function testing

Other Parts Discussed in Thread: MSP430F5438

Hi All,

I am trying to test the function of I2C on msp430f5438 experimental board.

what i did was that there are two I2C bus available on the experimental board UCB1 which is available on P3.7 and P5.4 and UCB3 which is available on P10.1 and P10.2.

and i want to send data through UCB3 (master) and receive it at UCB1 (slave) and i am sure the bus connection has no error.

the source code is shown as follow

#include <msp430.h> 

/*
 * main.c
 */

/* transfer data from UCB3 to UCB1; UCB3 is master transmitter and UCB1 is slave receiver */

unsigned char *PTxData;
unsigned char TXByteCtr;
unsigned char *PRxData;
unsigned char RXByteCtr;

const unsigned char TxData[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
volatile unsigned char RxBuffer[128];
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer
	/* initialize UCB3 */
    P10SEL |= BIT1 + BIT2;
    UCB3CTL1 |= UCSWRST;

    UCB3CTL0 = UCMST + UCMODE_3 + UCSYNC;
    UCB3CTL1 = UCSSEL_2 + UCSWRST;
    UCB3BR0 = 12;
    UCB3BR1 = 0;
    UCB3I2CSA = 0x48;
    UCB3CTL1 &= ~UCSWRST;

    UCB3IE |= UCTXIE;

    /* initialize UCB1 */
    P3SEL |= BIT7;
    P5SEL |= BIT4;

    UCB1CTL1 |= UCSWRST;

    UCB1CTL0 = UCMODE_3 + UCSYNC;

    UCB1I2COA = 0x48;

    UCB1CTL1 &= ~UCSWRST;
    UCB1IE = UCSTPIE + UCSTTIE + UCRXIE;


    while(1){
    	__delay_cycles(50);

    	PTxData = (unsigned char *)TxData;

    	PRxData = (unsigned char *)RxBuffer;

    	TXByteCtr = sizeof TxData;

    	RXByteCtr = 0;

    	UCB3CTL1 = UCTR + UCTXSTT;


    	__bis_SR_register(LPM0_bits + GIE);
    	__no_operation();

    	while(UCB3CTL1&UCTXSTP);
    }
}

#pragma vector = USCI_B3_VECTOR

__interrupt void USCI_B3_ISR(void){

	switch(UCB3IV){
	case 12:
		if(TXByteCtr){
			UCB3TXBUF = *PTxData++;
			TXByteCtr--;
		}else{
			UCB3CTL1 |= UCTXSTP;
			UCB3IFG &= ~UCTXIFG;
			__bic_SR_register_on_exit(LPM0_bits);
		}
	default: break;
	}

}

#pragma vector = USCI_B1_VECTOR

__interrupt void USCI_B1_ISR(void){

	switch(UCB1IV){
	case 6:
		UCB1IFG &= ~UCSTTIFG;
		break;

	case 8:
		UCB1IFG &= ~UCSTPIFG;
		if(RXByteCtr){
			__bic_SR_register_on_exit(LPM0_bits);
		}
		break;

	case 10:
		*PRxData = UCB1RXBUF;
		RXByteCtr++;
		break;

	default: break;
	}
}

i observed through breakpoints that transmitting is working properly (the program halts at transmit interrupt). but it looks like the receiver doesn't work (the program didn't halt at receiver interrupt)

i couldn't find any reason behind it.

can someone suggest how to fix it?

really appreciate it.

Jensen

  • Reading the IV register automatically clears the corresponding interrupt flag.

    Please don't use magic numbers but the appropriate symbols (USCI_I2C_UCSTTIFG etc.).

    Can you capture the waveform with an oscilloscope or logic analyzer? If not, make the rate really slow and add LEDs.

  • clearing interrupt flag in the interrupt routine is the practice of the example code so it wouldn't make any difference.

    i inserted breakpoints at all interrupts. but the program only jump to transmit interrupt and when it comes to receiver interrupt, the program is stuck.

**Attention** This is a public forum