STC8H Development (VII): I2C drive MPU6050 three-axis acceleration + three-axis angular velocity detection module

Publisher:bobojrtLatest update time:2022-06-08 Source: eefocusKeywords:MPU6050 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

MPU-6050

 

MPU-6050 is a six-axis motion tracking chip produced by InvenSense, with a chip size of 4×4×0.9mm and a QFN package. It integrates a three-axis gyroscope, a three-axis accelerometer, an on-chip temperature sensor and a digital motion processor (DMP). It can use the I2C interface to connect to the input of an external three-axis electronic compass to provide a complete nine-axis motion fusion output.


The MPU-6050 contains six 16-bit ADCs, three for gyroscope output and three for accelerometer output. The user can set the gyroscope full-scale range to ±250, ±500, ±1000, ±2000°/second (dps), and the accelerometer full-scale range to ±2g, ±4g, ±8g and ±16g. Communication uses a 400kHz I2C interface or a 1MHz SPI interface (SPI is only available on the MPU-6000).


Wiring between module and STC8H

The modules on the market usually have 8 pins. If you only want to obtain 6-axis sampling and temperature, you only need to connect 4 wires to STC8H.


    P32   -> SCL

    P33   -> SDA

    GND   -> GND

    3.3V  -> VCC


The other 4 pins that are not connected are


XDA and XCL: I2C master device interface, used to connect external I2C slave devices,

AD0: I2C slave device address LSB

INT: interrupt output

Communication between module and STC8H

The highest I2C frequency is 400KHz. The settings of STC8H are as follows. The I2C function multiplexing selects P32/P33. If you change to other multiplexing pins, you need to adjust accordingly.


void I2C_Init(void)

{

    // Master node mode

    I2C_SetWorkMode(I2C_WorkMode_Master);

    /**

     * I2C clock = SYSCLK / 2 / (__prescaler__ * 2 + 4)

     * MPU6050 works with i2c clock up to 400KHz

     * 

     * 44.2368 / 2 / (26 * 2 + 4) = 0.39 MHz

    */

    I2C_SetClockPrescaler(0x1A);

    // Multiplex port selection

    I2C_SetPort(I2C_AlterPort_P32_P33);

    // Start I2C

    I2C_SetEnabled(HAL_State_ON);

}


The communication with MPU6050 can be done directly using the I2C read and write methods in the SDK. There are a few points to note:


If the double-byte result read out at one time is directly converted to uint16_t, the positions of the high byte and low byte are opposite, so they need to be swapped before conversion.

Can read 6-axis + temperature data at one time

uint16_t swap(uint16_t num)

{

    return (num >> 8) | (num << 8);

}


void MPU6050_Write(uint8_t addr, uint8_t dat)

{

    I2C_Write(MPU6050_ADDR, addr, &dat, 1);

}


uint8_t MPU6050_Read(uint8_t addr)

{

    uint8_t right;

    I2C_Read(MPU6050_ADDR, addr, &ret, 1);

    return right;

}


// Read 16-bit data

uint16_t MPU6050_ReadInt(uint8_t addr)

{

    uint16_t right;

    I2C_Read(MPU6050_ADDR, addr, (uint8_t *)&ret, 2);

    return swap(ret); // swap high/low bits for correct order

}


// Read 7 16-bit data at a time

void MPU6050_ReadAll(uint16_t *buf)

{

    uint8_t i;

    I2C_Read(MPU6050_ADDR, MPU6050_REG_ACCEL_XOUT_H, (uint8_t *)buf, 14);

    for (i = 0; i < 7; i++)

    {

        *(buf + i) = swap(*(buf + i));

    }

}


Main register settings

Description of several registers that may be used


Power Management Registers 0x6B and 0x6C

These two registers control the working mode of MPU6050: sleep mode (Sleep), power saving mode (Cycle) and normal working mode


Sleep mode: The sixth position of 0x6B is set to 1. Communication is possible in sleep mode, but all detection conversions are stopped, and the result read is 0.

Power saving mode: It is a mode that alternates between sleep and normal operation. MPU6050 performs detection conversion every once in a while, and is in sleep mode for the rest of the time.

To enter power saving mode, you need to set the sleep position to 0, the cycle position to 1, the temperature sampling disable position to 1, and STBY_XG, STBY_YG, and STBY_ZG to 1

The sampling frequency in node mode can be set to 1.25Hz, 5Hz, 20Hz, 40Hz

Normal mode: Normal mode performs normal detection and conversion according to the settings.

In 0x6C, you can also specify which of the six detection axes to pause detection.


Sampling Rate Register 0x19

This register is used to set the gyroscope output rate division factor. The sampling rate is generated by dividing the gyroscope output rate by the value of this register:


Sampling rate = gyroscope output rate / (1 + SMPLRT_DIV)


Among them, the gyroscope output rate is 8KHz when DLPF (low pass filtering) is disabled (DLPF_CFG = 0 or 7), and 1KHz when DLPF is enabled.


Configuration Register 0x1A

This register is used to set the external frame synchronization (FSYNC) pin sampling and the low-pass filter (DLPF) of the digital gyroscope and accelerometer. The external signal connected to the FSYNC pin can be sampled by configuring EXT_SYNC_SET. The signal change of the FSYNC pin is latched to capture the signal. The FSYNC signal will be sampled at the sampling rate defined in register 0x19. After sampling, the latch will reset to the current FSYNC signal state.


Bit 3, 4, 5: Set the position of the FSYNC bit

Bit 0,1,2: Value 0 - 6, set DLPF, the larger the number, the greater the delay.

Angular velocity detection (gyroscope) configuration register 0x1B

This register is used to set the self-test and full-scale range of the three axes of angular velocity


Acceleration detection configuration register 0x1C

This register is used to set the acceleration three-axis self-test and full-scale range


Module interrupt type and settings

The interrupt function is configured through the interrupt configuration register. The configurable items include INT pin configuration, interrupt latch and clear method, and interrupt trigger. The items that can trigger the interrupt are:


The clock generator locks to the new reference oscillator (for switching clock sources)

New data can be read (from FIFO and data register)

Accelerometer event interrupt

MPU-6050 did not receive acknowledgment from auxiliary sensor on I2C bus

The interrupt status can be read from the Interrupt Status Register.


Test data

During the detection process, MPU6050 continuously outputs the detection values ​​of the six axes plus temperature according to the sampling rate. If the full-scale range is set larger, the measured output number is smaller and the sensitivity is lower. To increase the sensitivity, the full-scale range can be adjusted smaller. Compared with ADXL345, MPU6050 is more suitable for detecting objects in motion and can detect more accurate changes in the posture of objects in motion. Similarly, if there is no electronic compass (magnetometer), only the pitch and roll angle data can be obtained, but the heading angle data cannot be obtained.


Demo Code

The demo code continuously reads 7 detection data at 100ms intervals and outputs them through the UART1 serial port. If the wiring is correct, the detection value changes caused by the module movement can be observed through the serial port software.


GitHub FwLib_STC8/tree/master/demo/i2c/mpu6050

Gitee FwLib_STC8/tree/master/demo/i2c/mpu6050

Keywords:MPU6050 Reference address:STC8H Development (VII): I2C drive MPU6050 three-axis acceleration + three-axis angular velocity detection module

Previous article:STC8H Development (VIII): NRF24L01 wireless audio transmission (walkie-talkie prototype)
Next article:STC8H Development (VI): SPI driving ADXL345 three-axis acceleration detection module

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号