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.

CCS/MSP430F249: Read multiple bytes In I2C Communication

Part Number: MSP430F249

Tool/software: Code Composer Studio

Hello

I'm trying to use a temperature sensor(PCT2075) by MSP430

To get a temperature, I get a 2bytes from this sensor. 

Howerver, I got just two same value. I think that it is MSByte.

I wrote a code from this link.

https://e2e.ti.com/support/microcontrollers/msp430/f/166/t/589712?MSP430FR5969-Read-multiple-bytes-of-data-i2c-with-repeated-start-and-without-interrupts

I'm using MSP430F249. so I modified  a code from this link.

void i2c_read_multi(uint8_t slv_addr, uint8_t reg_addr, uint8_t l, uint8_t *arr)
{
   uint8_t i;

   while(UCB0STAT & UCBBUSY);

   UCB0I2CSA = slv_addr;                   // set slave address

   UCB0CTL1 |= UCTR | UCTXSTT;            // transmitter mode and START condition.

   while(UCB0CTL1 & UCTXSTT);

   UCB0TXBUF = reg_addr;

   while(!(UCB0CTL1 & UCTXSTT));

   UCB0CTL1 &= ~UCTR;                     // receiver mode

   UCB0CTL1 |= UCTXSTT;                   // START condition

   while(UCB0CTL1 & UCTXSTT);             // make sure start has been cleared

   for (i = 0; i < l; i++) {

       while(!(IFG2 & UCB0RXIFG));

       if(i == l - 1){

           UCB0CTL1 |= UCTXSTP;           // STOP condition

       }

       arr[i] = UCB0RXBUF;

   }

   while(UCB0CTL1 & UCTXSTP);

}

  • > while(!(UCB0CTL1 & UCTXSTT));

    The other thread says here:

    > while(!(UCB0IFG & UCTXIFG0));

    Looking at the I2C state diagram [Ref User Guide (SLAU144J) Fig 17-12] I'm not sure how you got past the previous UCTXSTT test, but I guess I won't argue with success.

  • Hey Jaeseo,

    Did you get your I2C working?  I wanted to also point out that there are several I2C examples available here: https://dev.ti.com/tirex/explore/node?node=APbNbwzUDwYij82MW9a0-g__IOGqZri__LATEST

    Thanks,

    JD

  • Thanks 

    I modify my code like this

    void i2c_read_multi_2(uint8_t slv_addr, uint8_t reg_addr, uint8_t l, uint16_t *arr)
    {
    	    uint8_t i;
    	    uint8_t msb;
    	    uint8_t lsb;
    	    uint16_t val16;
    
    	    while (UCB0STAT & UCBBUSY);
    
    	    // set slave address
    	    UCB0I2CSA = slv_addr;
    
    	    // transmitter mode and START condition.
    	    UCB0CTL1 |= UCTR | UCTXSTT;
    
    	    while (UCB0CTL1 & UCTXSTT);
    
    	    UCB0TXBUF = reg_addr;
    
    	    while (IFG2 & UCB0TXIFG == 0);
    
    	    // receiver mode
    	    UCB0CTL1 &= ~UCTR;
    
    	    // START condition
    	    UCB0CTL1 |= UCTXSTT;
    
    	    // make sure start has been cleared
    	    while (UCB0CTL1 & UCTXSTT);
    
    	    for (i = 0; i < l; i++) {
    	        while (!(IFG2 & UCB0RXIFG));
    	        msb = UCB0RXBUF;
    
    	       // while (!(IFG2 & UCB0RXIFG));
    
    	        // STOP condition
    	        if (i == l - 1) {
    	            UCB0CTL1 |= UCTXSTP;
    	        }
    
    	        lsb = UCB0RXBUF;
    
    	        val16 = msb;
    	        val16 <<= 8;
    	        val16 |= lsb;
    
    	     
    	    }
    
    	    while (UCB0CTL1 & UCTXSTP);
    }

  • >        while (IFG2 & UCB0TXIFG == 0);

    This (evidently) isn't causing you trouble now, but it will eventually. Try:

    >        while ((IFG2 & UCB0TXIFG) == 0);

**Attention** This is a public forum