I am using the MSP430F5529 Launchpad with the SensorHub Boosterpack. I am just trying to get the I2C working with the BMP180.
When I hook up the oscilloscope I can see that the MSP430 is properly addressing the BMP180 and receiving data. Unfortunately I am only able to store the most significant bit into my variable. I have a second variable for storing the least significant bit but nothing gets stored into here, even though I see both bits being sent on the oscilloscope.
In my code I have a loop that waits for the RX buffer flag to be set, then I place the RXBUFF data into my variable. Then I immediately check again to see if the RX flag is set, which then stored the least significant bit into the other variable. I guess I am not 100% sure I understand how the RX flag is set. I am assuming it is set immediately after a new byte is sent.
Here is my code:
/***************************************************************************/
//
//
//
//
//
/***************************************************************************/
/***************************************************************************/
// Includes and defines
#include <msp430.h>
#define MSP430F5529_USCIB0_ADDR 0x05E0
#define BMP180_ADDR 0x77
void initI2C(void);
void Setup_TX(void);
void i2c_start(void);
void i2c_stop(void);
/***************************************************************************/
// Main
int main(void){
int i = 0;
int cmd = 0xAA;
int MSB = 0;
int LSB = 0;
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
initI2C();
while(1){
Setup_TX();
i2c_start();
if(UCB0IFG & UCTXIFG){
UCB0TXBUF = cmd;
}
for (i=0; i<1000; i++) {
if (UCB0IFG & UCTXIFG){
break;
}
}
// Data was not sent, return
if (i == 1000) {
return -1;
}
UCB0CTL1 &= ~UCTR; // Switch to receive mode
i2c_start(); // Restart
// Wait to receive data in RX buffer
for (i = 0; i < 1000; i++){
// Get data if RXIFG is set
// RXIFG is set when buffer has received a byte
if (UCB0IFG & UCRXIFG){
MSB = UCB0RXBUF; // Get byte from RX buffer
UCB0IFG &= ~UCRXIFG; // Set RXIFG to 0
break;
}
if (UCB0IFG & UCRXIFG){
LSB = UCB0RXBUF; // Get byte from RX buffer
UCB0IFG &= ~UCRXIFG; // Set RXIFG to 0
break;
}
}
i2c_stop();
// Clear USCI_B0 TX int flag
UCB0IFG &= ~UCTXIFG;
}
}
/***************************************************************************/
//
void initI2C(void){
P3DIR |= BIT0 + BIT1; // Set SDA and SCL as outputs
P3OUT &= ~BIT0; // Write 0 to SDA
P3SEL |= BIT0 + BIT1; // Assign I2C pins to USCI_B0
}
/***************************************************************************/
//
void Setup_TX(void){
int i;
// Ensure stop condition got sent
for (i = 0; i < 1000; i++) {
if ((UCB0CTL1 & UCTXSTP) == 0)
break;
}
// Enable SW reset
UCB0CTL1 |= UCSWRST;
// I2C Master, synchronous mode
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
// Use SMCLK, keep SW reset
UCB0CTL1 = UCTR + UCSSEL_2 + UCSWRST;
// fSCL = SMCLK/12 = ~100kHz
UCB0BR0 = 12;
UCB0BR1 = 0;
UCB0I2COA = MSP430F5529_USCIB0_ADDR; // MSP430 addr
UCB0I2CSA = BMP180_ADDR; // BMP180 addr
// Clear SW reset, resume operation
UCB0CTL1 &= ~UCSWRST;
}
/***************************************************************************/
//
void i2c_start(void){
UCB0CTL1 |= UCTXSTT;
}
/***************************************************************************/
//
void i2c_stop(void){
int i;
// I2C stop condition
UCB0CTL1 |= UCTXSTP;
// Ensure stop condition got sent
for (i=0; i<1000; i++) {
if ((UCB0CTL1 & UCTXSTP) == 0)
break;
}
P1OUT |= BIT0;
}