Any help figuring out how to get i2c working with the MPU6050 would be appreciated. or some tutorials to follow for the msp430F55 series. What's the best way to debug serial communication, just print the data to console to see if it's valid? I think it gets stuck in __TI_ISR_TRAP #include /* * main.c */ unsigned char RX_Data[6]; unsigned char TX_Data[2]; unsigned char RX_ByteCtr; unsigned char TX_ByteCtr; int xAccel; int yAccel; int zAccel; unsigned char slaveAddress = 0x68; // Set slave address for MPU-6050 // 0x68 for ADD pin=0 // 0x69 for ADD pin=1 const unsigned char PWR_MGMT_1 = 0x6B; // MPU-6050 register address const unsigned char ACCEL_XOUT_H = 0x3B; // MPU-6050 register address const unsigned char ACCEL_XOUT_L = 0x3C; // MPU-6050 register address const unsigned char ACCEL_YOUT_H = 0x3D; // MPU-6050 register address const unsigned char ACCEL_YOUT_L = 0x3E; // MPU-6050 register address const unsigned char ACCEL_ZOUT_H = 0x3F; // MPU-6050 register address const unsigned char ACCEL_ZOUT_L = 0x40; // MPU-6050 register address void i2cInit(void); void i2cWrite(unsigned char); void i2cRead(unsigned char); int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT // Set clock speed (default = 1 MHz) // set up I2C pins P3SEL |= 0x03; // Assign I2C pins to USCI_B0 // Assign I2C pins to USCI_B0 // Initialize the I2C state machine i2cInit(); // Wake up the MPU-6050 slaveAddress = 0x68; // MPU-6050 address TX_Data[1] = 0x6B; // address of PWR_MGMT_1 register TX_Data[0] = 0x00; // set register to zero (wakes up the MPU-6050) TX_ByteCtr = 2; i2cWrite(slaveAddress); while (1) { // Point to the ACCEL_ZOUT_H register in the MPU-6050 slaveAddress = 0x68; // MPU-6050 address TX_Data[0] = 0x3B; // register address TX_ByteCtr = 1; i2cWrite(slaveAddress); // Read the two bytes of data and store them in zAccel slaveAddress = 0x68; // MPU-6050 address RX_ByteCtr = 6; i2cRead(slaveAddress); xAccel = RX_Data[5] >here<< and read } } //********************************************************************************************* void i2cInit(void) { // set up I2C module UCB0CTL1 |= UCSWRST; // Enable SW reset UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, synchronous mode UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset UCB0BR0 = 10; // fSCL = SMCLK/12 = ~100kHz UCB0BR1 = 0; UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation } //********************************************************************************************* void i2cWrite(unsigned char address) { __disable_interrupt(); UCB0I2CSA = address; // Load slave address UCB0IE |= UCTXIE; // Enable TX interrupt while(UCB0CTL1 & UCTXSTP); // Ensure stop condition sent UCB0CTL1 |= UCTR + UCTXSTT; // TX mode and START condition __enable_interrupt(); // sleep until UCB0TXIFG is set ... } //********************************************************************************************* void i2cRead(unsigned char address) { __disable_interrupt(); UCB0I2CSA = address; // Load slave address UCB0IE |= UCRXIE; // Enable RX interrupt while(UCB0CTL1 & UCTXSTP); // Ensure stop condition sent UCB0CTL1 &= ~UCTR; // RX mode UCB0CTL1 |= UCTXSTT; // Start Condition __enable_interrupt(); } /**********************************************************************************************/ // USCIAB0TX_ISR #pragma vector = USCIAB0TX_VECTOR __interrupt void USCIAB0TX_ISR(void) { if(UCB0CTL1 & UCTR) // TX mode (UCTR == 1) { if (TX_ByteCtr) // TRUE if more bytes remain { TX_ByteCtr--; // Decrement TX byte counter UCB0TXBUF = TX_Data[TX_ByteCtr]; // Load TX buffer } else // no more bytes to send { UCB0CTL1 |= UCTXSTP; // I2C stop condition UCB0IFG &= ~UCSTPIFG; // Clear USCI_B0 TX int flag // Exit LPM0 } } else // (UCTR == 0) // RX mode { RX_ByteCtr--; // Decrement RX byte counter if (RX_ByteCtr) // RxByteCtr != 0 { RX_Data[RX_ByteCtr] = UCB0RXBUF; // Get received byte if (RX_ByteCtr == 1) // Only one byte left? UCB0CTL1 |= UCTXSTP; // Generate I2C stop condition } else // RxByteCtr == 0 { RX_Data[RX_ByteCtr] = UCB0RXBUF; // Get final received byte// Exit LPM0 } } }
↧