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/TM4C123GH6PM: MPU6050 with TM4C123gxl launchpad

Part Number: TM4C123GH6PM

Tool/software: Code Composer Studio

Hi everyone,

    I have been trying to read sensor data(gy-521) with Tm4c123gxl launchpad . But i have problem with the code  and I'm sure my circuit is good. the example code is unable to print the data.

with some trails (using UARTprintf() i got to know the code got stuck at the first while(!g_bMPU6050Done)  loop without leaving it.

I'm new to this so please help me out with the code below. thank you

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#include "sensorlib/i2cm_drv.h"
#include "sensorlib/hw_mpu6050.h"
#include "sensorlib/mpu6050.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/i2c.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"

tI2CMInstance g_sI2CMSimpleInst;

void InitI2C0(void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    // Set up debugging UART
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTStdioConfig(0, 115200, SysCtlClockGet());

    //enable I2C module 0
    SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);

    //reset module
    SysCtlPeripheralReset(SYSCTL_PERIPH_I2C0);

    //enable GPIO peripheral that contains I2C 0
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

    // Configure the pin muxing for I2C0 functions on port B2 and B3.
    GPIOPinConfigure(GPIO_PB2_I2C0SCL);
    GPIOPinConfigure(GPIO_PB3_I2C0SDA);

    // Select the I2C function for these pins.
    GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
    GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);

    // Enable and initialize the I2C0 master module.  Use the system clock for
    // the I2C0 module.     // I2C data transfer rate set to 400kbps.
    I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), true);

    //clear I2C FIFOs
    HWREG(I2C0_BASE + I2C_O_FIFOCTL) = 80008000;        // Initialize the I2C master driver.
    I2CMInit(&g_sI2CMSimpleInst, I2C0_BASE, INT_I2C0, 0xff, 0xff, 120000000);
}

//
// A boolean that is set when a MPU6050 command has completed.
//

volatile bool g_bMPU6050Done;

//
// The function that is provided by this example as a callback when MPU6050
// transactions have completed.
//


void MPU6050Callback(void *pvCallbackData, uint_fast8_t ui8Status)
    {
//
// See if an error occurred.
//
        if(ui8Status != I2CM_STATUS_SUCCESS)
        {
//
// An error occurred, so handle it here if required.
//
        }
//
// Indicate that the MPU6050 transaction has completed.
//
        g_bMPU6050Done = true;
    }
//
// The MPU6050 example.
//

void MPU6050Example(void)
    {
        float fAccel[3], fGyro[3];
        tI2CMInstance sI2CInst;
        tMPU6050 sMPU6050;
//
// Initialize the MPU6050. This code assumes that the I2C master instance
// has already been initialized.
//
        g_bMPU6050Done = false;
        MPU6050Init(&sMPU6050, &sI2CInst, 0x68, MPU6050Callback, 0);
while(!g_bMPU6050Done) { } // // Configure the MPU6050 for +/- 4 g accelerometer range. // g_bMPU6050Done = false; MPU6050ReadModifyWrite(&sMPU6050, MPU6050_O_ACCEL_CONFIG, ~MPU6050_ACCEL_CONFIG_AFS_SEL_M, MPU6050_ACCEL_CONFIG_AFS_SEL_4G, MPU6050Callback,0); while(!g_bMPU6050Done) { } // // Loop forever reading data from the MPU6050. Typically, this process // would be done in the background, but for the purposes of this example, // it is shown in an infinite loop. // while(1) { // // Request another reading from the MPU6050. // g_bMPU6050Done = false; MPU6050DataRead(&sMPU6050, MPU6050Callback, 0); while(!g_bMPU6050Done) { } // // Get the new accelerometer and gyroscope readings. // MPU6050DataAccelGetFloat(&sMPU6050, &fAccel[0], &fAccel[1], &fAccel[2]); MPU6050DataGyroGetFloat(&sMPU6050, &fGyro[0], &fGyro[1], &fGyro[2]); // // Do something with the new accelerometer and gyroscope readings. // } } int main() { InitI2C0(); MPU6050Example();
UARTprintf("02X\n", MPU6050DataGyroGetFloat(&sMPU6050, &fGyro[0], &fGyro[1], &fGyro[2]));
return(0); }


  • I understand your frustration, but I am unable to debug your code for you. First of all, it is impractical for me to debug everyone's program. There would be way too many requests. The best I can do is answer specific questions and perhaps give some advice.

    I suggest that instead of trying to debug your code as a whole, that you break it into simple steps. In this case, the first step might be to verify you can configure the I2C pins correctly. This can usually be verified with a digital oscilloscope of logic analyzer.

    The next step would be to see if you can talk to the MPU6050. I suggest you start with a simple test such as reading the "who am I" register. (Register 117.)

    Doing your debug in small simple steps will not only help you identify issues along the way, it will allow others to be more helpful as the source of your problem will be more easily identified since the scope of your code will be less. Also remember, the rest of us often will not have your specific hardware. I have heard that the examples in TivaWare for the MPU9150 also work on the MPU6050, but I have not personally verified this.
  • It is great to see vendor agents - rising to (yet w/out admitting) - their "Belief in "KISS!"    

    Poster presents a "Monster Program" - with SO MANY "MOVING PARTS" - the odds of success are,  "SO VERY SMALL!"

    Do not the "schools" - or "Life in general" - teach that,  "Juggling MANY Balls - simultaneously" - is best left - to (seriously) trained performers?

    Reducing the code to its:  "Smallest,  individually Focused pieces" and then,  "Testing & Verifying, "Each/Every" small (and isolated) part" in a "Measured & Systematic FASHION" - clearly defines "KISS."   (Keep It Simple Student!)  

    Only after - all of the individual pieces have been "Tested & Verified" - should the attempt to logically combine - just one additional piece at a time - be launched!     (and of course - testing /verifying - every step along the way!)

    Might it be noted - that  "NEVER" (or at least ineffectively)  are client-users advised - of  such, "KISS -> Proceed by  Systematic & Careful Refinement"  - vastly   superior method.     Pity.