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 parameter configuration4. Use of I2C protocol5. Write the program6. Compile4. Program AnalysisV. 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.

1. Open the SYSCONFIG configuration tool

Create a blank project in CCS.

image-20241126182939655

image-20241126183128934

Find and open the empty.syscfg file in the left workspace of CCS.

image-20241127152938881

2. Pin parameter configuration

For the pin configuration of LED lights and buttons, please refer to the previous course. Here, only the I2C related pin configuration is posted

SCL:

image-20241128184037786

SDA:

image-20241128184114478

3. Serial port parameter configuration

The undisplayed part is the default option.

image-20241128184422837

image-20241128184507618

Use the shortcut key Ctrl+S to save the configuration in the .syscfg file.

4. Use of I2C protocol

We create a new folder in the project folder: BSP.

image-20241128184749144

Create two more files in the BSP folder, namely bsp_mpu6050.c and bsp_mpu6050.h.

image-20241128185031225

Then put the drivers designed for the MPU6050 gyroscope and accelerometer into the newly created eMPL folder.

image-20241128185332351

Update the header file path and right-click the project folder.

image-20241128185622951

image-20241128210302511

${PROJECT_ROOT}/BSP Path Description

${PROJECT_ROOT} indicates the current project path.

/BSP indicates the Hardware folder under the current project path.

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 software I2C pins. 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, this 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, this 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 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

image-20241127155238783

If the compilation is successful, you can download the program to the development board.

4. Program Analysis

image-20241128211939549

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.

image-20241128200214215

This program obtains posture data (pitch, roll, yaw) through the MPU6050 sensor and uses the DMP (digital motion processor) to calculate the Euler angle.

After initializing the development board and the MPU6050 sensor, try to initialize the DMP module. If the initialization fails, try again.

In the main loop, the Euler angle data of the sensor is continuously obtained, formatted and sent through the serial port. If the data acquisition fails, an error message is output. Data is acquired every 200 milliseconds to ensure an appropriate sampling frequency.

V. Experimental phenomenon

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

image-20241126122920549