Get MPU6050 data (I2C)

Get MPU6050 data (I2C)1. Learning objectivesIntroduction to I2CBasic parameters of I2C2. Hardware Construction3. Experimental steps1. Open the SYSCONFIG configuration tool2. Pin parameter configuration3. Serial port configuration4. Use of I2C protocol5. Write the program6. Compile4. Program Analysis5. Experimental phenomenon

1. Learning objectives

  1. Learn the basic knowledge of IIC communication.
  2. Get MPU6050 data.

Introduction to I2C

The IIC bus is a bidirectional two-wire serial bus that provides communication lines between integrated circuits. It means a protocol that completes information exchange between integrated circuits or functional units.

The IIC module receives and sends data and converts data from serial to parallel or from parallel to serial. Interrupts can be enabled or disabled. The interface is connected to the IIC bus through the data pin (SDA) and the clock pin (SCL). It allows connection to a standard (up to 100kHz) or fast (up to 400kHz) IIC bus. (The data line SDA and the clock SCL constitute a serial bus that can send and receive data).

There are three types of signals in the IIC bus during data transmission, namely: start signal (START), stop (end) signal (STOP), and acknowledgement signal (ACK). Secondly, it is in an idle state when no data transmission is performed.

Basic parameters of I2C

Rate: The I2C bus has two transmission modes: standard mode (100 kbit/s) and fast mode (400 kbit/s), and there are also faster extended mode and high-speed mode to choose from.

Device address: Each device has a unique 7-bit or 10-bit address, and the address selection can be used to determine who to communicate with.

Bus state: The I2C bus has five states, namely idle state, start signal, end signal, response signal, and data transmission.

Data format: The I2C bus has two data formats, standard format and fast format. The standard format is an 8-bit data byte plus a 1-bit ack/nack (acknowledgement/non-acknowledgement) bit, and the fast format allows two bytes to be transmitted simultaneously.

Since the SCL and SDA lines are bidirectional, they may also have level errors due to external reasons (such as capacitance in the line), which may cause communication errors. Therefore, in the IIC bus, pull-up resistors are usually used to ensure that the signal line is at a high level in the idle state.

2. Hardware Construction

The I2C of the MSPM0G series supports master-slave mode, has 7 address bits that can be set, supports I2C standard transmission rates of 100kbps, 400kbps, and 1Mbps, and supports SMBUS. Whether it is a master or a slave, there are independent 8-byte FIFOs for sending and receiving. MSPM0 I2C has 8-byte FIFOs, generates independent interrupts for controller and target modes, and supports DMA.

img

Software I2C refers to implementing the I2C communication protocol by writing code in the program. It uses general-purpose input and output (GPIO) pins to simulate the data line (SDA) and clock line (SCL) of I2C, and transmits data and generates timing signals by controlling the level changes of the pins through software. Compared with hardware I2C, the advantage of software I2C is that it does not require specific hardware support and can be implemented on any microcontroller that supports GPIO functions. It uses the general IO pins of the microcontroller to implement the I2C communication protocol.

Hardware I2C refers to processing the I2C communication protocol through a dedicated hardware module. Most modern microcontrollers and some external devices have integrated hardware I2C modules, which are responsible for handling the details of I2C communication, including generating correct timing signals, automatically handling signal conflicts, data transmission and error detection, etc. You can directly use the hardware pin connection without writing timing code.

This experiment uses software IIC to read the data of the MPU6050 module.

Hardware connection

MSPM0G3507MPU6050
PA0SCL
PA1SDA
3V3GND
GNDVCC

image-20241125203947914

3. Experimental steps

This course configures the PA0 and PA1 pins as SCL and SDA to read the data of the MPU6050 six-axis sensor module.

Here we use the template project we provide for introduction.

Unzip the template project in the root directory of the SDK, and I rename it to the 10_I2C folder.

1. Open the SYSCONFIG configuration tool

Then open the project.

image-20241125205219306

Open the empty.syscfg file in the Keil main interface. When the empty.syscfg file is open, open the SYSCONFIG GUI interface.

image-20241125204945930

Find the GPIO column on the left, click to enter, and add a group of GPIO.

image-20241125205320552

2. Pin parameter configuration

SCL:

image-20241125205745754

SDA:

image-20241125205842521

3. Serial port configuration

This course uses the I2C protocol to send the data of MPU6050 to the host computer (computer) for display through the serial port. So we also need to configure the serial port.

The undisplayed part is the default option.

image-20241204142836249

image-20241204144708340

image-20241204144729231

4. Use of I2C protocol

We create two new files in the project template, namely bsp_mpu6050.c and bsp_mpu6050.h. Save them in the BSP folder of the project.

Include the following drivers for the MPU6050 gyroscope and accelerometer design and put them in the eMPL folder

Open the project manager, click BSP, and add the files we created and copied before to the BSP.

image-20241126113103240

Update the header file path.

image-20241126113958602

5. Write the program

When using software to implement I2C communication, you need to select appropriate pins as the data line (SDA) and clock line (SCL). Generally, any programmable general-purpose input and output (GPIO) pins can be selected as the pins of software I2C. For software I2C, at least two pins are required for the data line (SDA) and the clock line (SCL), and ensure that these pins can meet the timing requirements of the I2C communication protocol. The following is a general pin description:

  1. Data line (SDA): The pin used to transmit data. In software I2C, the pin needs to be set to output mode (for the master device to send data) and input mode (for the master device to receive data). During communication, data transmission needs to be achieved by controlling the level change of the data line.
  2. Clock line (SCL): The pin used to control the clock signal for data transmission. In software I2C, the pin needs to be set to output mode, and the clock pulse is generated by controlling the level change of the clock line to control the transmission of the data line. It should be noted that the following aspects should be considered when selecting the appropriate pin:

It should be noted that the implementation of software I2C requires more program code and calculations. Compared with hardware I2C, software I2C is more sensitive to processor performance and timing control. Therefore, when selecting pins, the performance and programmability of the processor also need to be considered. In order to ensure the maintainability and portability of the code, the relevant functions are macro-defined here.

The macro definitions of the SDA pin and the SCL pin are as follows:

bsp_mpu6050.h

The next step is to configure the timing part of I2C,

bsp_mpu6050.c (only part is intercepted here, please check the project source code for details)

Then write the following code in the empty.c file

6. Compile

Click the Rebuild icon. The following prompt appears, indicating that the compilation is complete and there are no errors.

image-20241119150803274

image-20241119150902489

4. Program Analysis

This code implements the initialization of the MPU6050, providing efficient motion tracking and attitude resolution capabilities through the DMP. . Call mpu_init to initialize the hardware module; enable the gyroscope and accelerometer and configure their sampling rate and FIFO buffer; load the DMP firmware to support advanced functions (such as attitude resolution and gesture detection); set the device orientation matrix and DMP functional characteristics (such as quaternion calculation, data calibration, etc.); finally enable the DMP output so that the device can provide stable attitude and sensor data at a set frequency.

This function reads the sensor data (including gyroscope, accelerometer and quaternion) processed by DMP from the FIFO of MPU6050, calculates the pitch angle (pitch), roll angle (roll) and heading angle (yaw) by parsing the quaternion, and returns the result.

This program implements the real-time attitude angle measurement function of MPU6050. By initializing the development board and MPU6050 and loading the DMP module, the pitch, roll and yaw of the device are calculated. The program first initializes DMP, retries when it fails, and enters the main loop after success, continuously reading attitude angle data and printing it out through the serial port.

5. Experimental phenomenon

After the program is downloaded, configure the serial port assistant as shown below, open the serial port, and you can read the real-time data of the MPU6050 module.

image-20241126122920549