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.

I can't read MPU6050 sensor using tm4c1230. Would you help me?

Below is my keil uVision source code.

I try to read MPU6050 sensor data of GY-87 10DOF using tivaware MPU6050 sample program.

I read some data but they didn't vary when I move to sensor to vary sensing value.

I need your help, please.

Thanks in advance.

---------------------------------------------------------------------------------------

#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_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/debug.h"
#include "driverlib/interrupt.h"
#include "driverlib/i2c.h"
#include "driverlib/sysctl.h"
#include "utils/uartstdio.h"
#include "driverlib/uart.h"

//
// A boolean that is set when a MPU6050 command has completed.
//
volatile bool g_bMPU6050Done = true;
tI2CMInstance sI2CInst;

tMPU6050 sMPU6050;

//
//Device frequency
//
int clockFreq;

void DelaySoft(volatile unsigned long ulDelay){
  ROM_SysCtlDelay((ROM_SysCtlClockGet() / 10000) * ulDelay);
}

//
// The interrupt handler for the I2C module.
//
void I2CMSimpleIntHandler(void)
{
  //
  // Call the I2C master driver interrupt handler.
  //
  I2CMIntHandler(&sI2CInst);
}

void InitI2C0(void)
{
  //enable I2C module 0
  SysCtlPeripheralEnable(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);

  I2CIntRegister(I2C0_BASE, &I2CMSimpleIntHandler);
  ROM_IntMasterEnable();

  // Initialize the I2C master driver.
  I2CMInit(&sI2CInst, I2C0_BASE, INT_I2C0, 0xff, 0xff, clockFreq);
}

// The function that is provided by this example as a callback when I2C transactions have completed.
void MPU6050Callback(void *pvData, 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 I2C transaction has completed.
  g_bMPU6050Done = true;
}

//*****************************************************************************
// UARTstdio 유틸리티를 사용하기 위한 UART 설정
//*****************************************************************************
void ConfigureUART(void)
{
// UART로 사용될 GPIO 포트 활성화
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// UART0 Peripheral 활성화
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

// GPIO 핀들을 UART mode로 변경
ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

// 16MHz 내부 오실레이터를 UART clock source로 사용
ROM_UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
// Colsole I/O를 위한 UART 초기화
UARTStdioConfig(0, 115200, 16000000);
}

char buf[100];
char* ftoa(float f)
{
int pos=0,ix,dp,num;
if (f<0)
{
buf[pos++]='-';
f = -f;
}
dp=0;
while (f>=10.0)
{
f=f/10.0;
dp++;
}
for (ix=1;ix<8;ix++)
{
num = (int)f;
f=f-num;
if (num>9)
buf[pos++]='#';
else
buf[pos++]='0'+num;
if (dp==0) buf[pos++]='.';
f=f*10.0;
dp--;
}
return buf;
}

void MPU6050Example(void) {
  float fAccel[3], fGyro[3];

  UARTprintf("MPU6050 initialization\n");
  // Write two bytes of data to the I2C device at address 0x3C.
  g_bMPU6050Done = false;
  uint_fast8_t result = MPU6050Init(&sMPU6050, &sI2CInst, 0x69, MPU6050Callback, 0);
  while(!g_bMPU6050Done) {
  }

  UARTprintf("Configure the MPU6050 for +/- 4 g accelerometer range\n");
  // Read four bytes of data from the I2C device at address 0x3C.
  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
  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]);

    UARTprintf("complete data receive from MPU6050\n");
    UARTprintf("Accel x = %s", ftoa(fAccel[0]));
    UARTprintf(", y = %s", ftoa(fAccel[1]));
    UARTprintf(", z = %s\n", ftoa(fAccel[2]));
    UARTprintf("Gyro x = %s", ftoa(fGyro[0]));
    UARTprintf(", y = %s", ftoa(fGyro[1]));
    UARTprintf(", z = %s\n", ftoa(fGyro[2]));

    DelaySoft(1000);
  }
}

int main()
{
  // 16MHz PIOSC(Precision Internal OSC.) 사용.
  ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_INT | SYSCTL_MAIN_OSC_DIS);
  clockFreq =ROM_SysCtlClockGet();

  // UARTprintf()를 사용하기위한 Utility
  ConfigureUART();

  // Print start
  UARTprintf("\nConfigure UART\n");

  InitI2C0();

  MPU6050Example();

  return(0);
}