GlobLib
HAL and API libraries for MCUs and hardware.
MPU6050

Routines for interaction with MPU6050 IMU. More...

Files

file  mpu6050.h
 Header file for Invensence IMU MPU6050.
 

Data Structures

struct  MPU6050
 Data structure containing variables specific to individual slave devices. More...
 

Macros

#define MPU_DEVICE_ADDRESS   0x68
 Standard slave address for the module (ADD -> GND).
 
#define MPU_DEVICE_ADDRESS_ALTERNATE   0x69
 Altenate slave address for the module (ADD -> VCC).
 
#define MPU_DATA_RANGE   32767.0f
 The decimal rangle of the ADC (15-bit <=> 32767).
 

Enumerations

Functions

uint8_t MPU6050_setup (MPU6050 *target, uint8_t i2c_number, uint8_t address)
 Initalizes the slave device and populates data structure variable. More...
 
void MPU6050_writeByte (MPU6050 *target, uint8_t address, uint8_t data)
 Writes a single byte to a given address. More...
 
uint8_t MPU6050_readByte (MPU6050 *target, uint8_t address)
 Read a single byte from a given address. More...
 
uint16_t MPU6050_readHalf (MPU6050 *target, uint8_t address)
 Read a half word from a given address. More...
 
void MPU6050_raw (MPU6050 *target, mpu_data what, int16_t *container)
 Get raw data values (16-bit each) More...
 
void MPU6050_calc (MPU6050 *target, mpu_data what, float *container)
 Get calculated data values. More...
 
void MPU6050_setAcc (MPU6050 *target, mpu_acc range)
 Set acceleration range. More...
 
void MPU6050_setGyr (MPU6050 *target, mpu_gyr range)
 Set gyroscope range. More...
 
uint8_t MPU6050_getAcc (MPU6050 *target)
 Get the acceleration range. More...
 
uint16_t MPU6050_getGyr (MPU6050 *target)
 Get the gyroscope range. More...
 

Detailed Description

Routines for interaction with MPU6050 IMU.

The MPU6060 operates on the I2C bus. This module provides routines for

Note
This library requires external functions to be declared.
  • I2C_start()
  • I2C_address()
  • I2C_write()
  • I2C_read()
  • I2C_repeatRead()
  • I2C_stop()
Author
Stuart Ianna
Version
1.0
Date
June 2018
Warning
None
Bug:
None
Todo:
Verified Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Example - Typical usage.
#include <stm32f103cb_core.h>
#include <coms128.h>
#include <mpu6050.h>
//Create a data structure for the device
MPU6050 mpu;
int main(void)
{
//Serial communications
USART_setup(USART_1);
COMS(USART_1);
//Debug enable
MCU_debugEnable(USART_add_put(USART_1));
//Setup I2C
I2C_setup(I2C_2);
//Setup Sensor
//Set the accelerometer and gyroscope range
//Container to store the calculated values
float data[7];
//For delay
volatile int i;
while (1) {
//Get raw data
MPU6050_calc(&mpu,MPU_ALL,data);
//Acceleration
printt(data[0]);
printt(data[1]);
printt(data[2]);
//Temperature
printt(data[3]);
//Gyro
printt(data[4]);
printt(data[5]);
printl(data[6]);
//wait a bit
for(i = 0; i < 40000;i++);
}
return 0;
}

Data Structure Documentation

struct MPU6050

Data structure containing variables specific to individual slave devices.

Definition at line 63 of file mpu6050.h.

Data Fields

uint8_t bus
 The port the device is connected to.
 
uint8_t slaveAddress
 The address of the slave device.
 
uint8_t accelSen
 The accelerometer sesitivity, set during MPU6050_setup() by reading slave data register.
 
uint16_t gyroSen
 The gyroscope sesitivity, set during MPU6050_setup() by reading slave data register.
 

Enumeration Type Documentation

enum mpu_acc
Enumerator
MPU_ACC_2G 

2G range for accelerometer

MPU_ACC_4G 

4G range for accelerometer

MPU_ACC_8G 

8G range for accelerometer

MPU_ACC_16G 

16G range for accelerometer

Definition at line 102 of file mpu6050.h.

enum mpu_data

Different options for getting data with MPU6050_raw() and MPU6050_calc()

Enumerator
MPU_ACCX 

Acceleration data x-axis.

MPU_ACCY 

Acceleration data y-axis.

MPU_ACCZ 

Acceleration data z-axis.

MPU_TEMP 

Temperature data.

MPU_GYRX 

Gyrometer data x-axis.

MPU_GYRY 

Gyrometer data y-axis.

MPU_GYRZ 

Gyrometer data z-axis.

MPU_ACC 

All raw accelerometer data.

MPU_GYR 

All raw gyro data.

MPU_IMU 

All gyro and all accelermeter.

MPU_ALL 

All raw data.

Definition at line 86 of file mpu6050.h.

enum mpu_gyr
Enumerator
MPU_GYR_250 

250 degree range for gyroscope

MPU_GYR_500 

500 degree range for gyroscope

MPU_GYR_1000 

1000 degree range for gyroscope

MPU_GYR_2000 

2000 degree range for gyroscope

Definition at line 111 of file mpu6050.h.

Function Documentation

void MPU6050_calc ( MPU6050 target,
mpu_data  what,
float *  container 
)

Get calculated data values.

This function calculates G-force and degrees per second using the sensitivity values defined in data structure MPU6050.

Parameters
targetThe target slave device
whatThe type of data to get
container
Returns
none

Definition at line 52 of file mpu6050.c.

uint8_t MPU6050_getAcc ( MPU6050 target)

Get the acceleration range.

Parameters
targetThe target slave device
Returns
The range for the accelerometer

Definition at line 455 of file mpu6050.c.

uint16_t MPU6050_getGyr ( MPU6050 target)

Get the gyroscope range.

Parameters
targetThe target slave device
Returns
The range for the gyroscope

Definition at line 485 of file mpu6050.c.

void MPU6050_raw ( MPU6050 target,
mpu_data  what,
int16_t *  container 
)

Get raw data values (16-bit each)

Parameters
targetThe target slave device
whatThe type of data to get
container
Returns
none

Definition at line 238 of file mpu6050.c.

uint8_t MPU6050_readByte ( MPU6050 target,
uint8_t  address 
)

Read a single byte from a given address.

This function can be used to read individual values of the device registers

Parameters
targetThe target slave device
addressThe target register address of the slave
Returns
The data at that address.

Definition at line 368 of file mpu6050.c.

uint16_t MPU6050_readHalf ( MPU6050 target,
uint8_t  address 
)

Read a half word from a given address.

Parameters
targetThe target slave device
addressThe target register address for the first register target
Returns
The data at that address.

Definition at line 380 of file mpu6050.c.

void MPU6050_setAcc ( MPU6050 target,
mpu_acc  range 
)

Set acceleration range.

Parameters
targetThe target slave device
rangeThe range to set
Returns
none

Definition at line 394 of file mpu6050.c.

void MPU6050_setGyr ( MPU6050 target,
mpu_gyr  range 
)

Set gyroscope range.

Parameters
targetThe target slave device
rangeThe range to set
Returns
none

Definition at line 425 of file mpu6050.c.

uint8_t MPU6050_setup ( MPU6050 target,
uint8_t  i2c_number,
uint8_t  address 
)

Initalizes the slave device and populates data structure variable.

Note
The relavent I2C port must be setup before this is called
Parameters
targetPointer to the MPU6050 data structure
i2c_numberThe number of the peripheral port connected, typically I2C_1 or I2C_2
addressThe slave address of the device MPU_DEVICE_ADDRESS or MPU_DEVICE_ADDRESS_ALTERNATE
Returns
0 if setup was ok, 1 if error occured.

Definition at line 31 of file mpu6050.c.

void MPU6050_writeByte ( MPU6050 target,
uint8_t  address,
uint8_t  data 
)

Writes a single byte to a given address.

This function can be used to modify individual registers of the device.

Parameters
targetthe target slave device
addressThe target register address of the slave
dataThe byte of data to write
Returns
none

Definition at line 358 of file mpu6050.c.