Quantcast
Channel: Data converters
Viewing all articles
Browse latest Browse all 38418

Forum Post: RE: TMP112 I2C communication

$
0
0

I just wanted to let you know, how I got the communication working for me on the MSP430G2553 at 8MHz DCO clock.

void initClock()
{
BCSCTL1 = CALBC1_8MHZ;              // Set DCO to 8 MHz
DCOCTL = CALDCO_8MHZ;               // MCLK = SMCLK = 8MHz
}

void initI2C()
{
P1SEL |= BIT6 + BIT7;                                                              // Assign I2C pins to USCI_B0
P1SEL2 |= BIT6 + BIT7;                                                            // Assign I2C pins to USCI_B0

UCB0CTL1 |= UCSWRST;                                                      // Enable SW reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;              // I2C Master, I2C Mode, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST;                              // Use SMCLK, keep SW reset
UCB0BR0 = 80;                                                                         // fSCL = SMCLK/80 = ~100kHz
UCB0BR1 = 0;
UCB0I2CSA = 0x48;                                                                  // Set slave address (TMP112)
UCB0CTL1 &= ~UCSWRST;                                                   // Clear SW reset, resume operation
}

uint16_t I2C_TMP112_read()
{
uint16_t temperature = 0;
UCB0CTL1 |= UCTR + UCTXSTT;         // Master Transmit Mode, send start condition
while (!(IFG2 & UCB0TXIFG));                // while TX buffer not ready
UCB0TXBUF = 0x00;                                // set address pointer register to temperature register
UCB0CTL1 |= UCTXSTT;                        // repeated Start Condition
UCB0CTL1 &= ~UCTR;                           // Master Receive Mode
while (!(IFG2 & UCB0RXIFG));                // Loop until I2C STT is cleared, slave acknowledge was received and RXBUFFER is ready
temperature |= UCB0RXBUF >> 4;
while (!(IFG2 & UCB0RXIFG));                // Loop until RXBUFFER ready
temperature |= UCB0RXBUF << 4;
UCB0CTL1 |= UCTXSTP;

return temperature;
}

There is no need to send the address pointer with every temperature read command, but I left it in there just to be sure.


Viewing all articles
Browse latest Browse all 38418