yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
MSPM0G3507 Development Board📖
1. Introduction to MSPM0
  • 1. Introduction to MSPM0
2. Development environment construction
  • 01.CCS Theia Development
  • 02.MDK Arm Development
  • 03.UniFlash Downloader
  • 04.DAP-LINK download program
  • 05.Program simulation and debugging
  • 06.Engineering transplantation
3. Keil basic case
  • 1. Turn on the LED light
  • 2. System delay
  • 3. Button control
  • 4. External interruption
  • 5. Serial communication
  • 6. Timer
  • 7. PWM breathing light
  • 8. ADC acquisition
  • 9. DMA transfer
  • 10. Get MPU6050 data (I2C)
  • 11. LCD Display (SPI)
4. CCS basic case
  • 1. Turn on the LED light
  • 2. System delay
  • 3. Button control
  • 4. External interruption
  • 5. Serial communication
  • 6. Timer
  • 7. PWM breathing light
  • 8. ADC acquisition
  • 9. DMA transfer
  • 10. Get MPU6050 data (I2C)
  • 11. LCD Display (SPI)
5. MSPM0 Robot Expansion Board
  • 1. MSPM0 Robot Expansion Board Introduction
  • 2. Custom Buttons
  • 3. Drive Buzzer
6.MSPM0-K230 Vision Application Module
  • 1.k230 color recognition
  • 2.k230 barcode recognition
  • 3.k230 QR code recognition
  • 4.k230Apriltag code recognition
  • 5.k230DM code recognition
  • 6.k230 face detection
  • 7.k230 gaze direction
  • 8.k230 face recognition
  • 9.k230 human body detection
  • 10.k230 fall detection
  • 11.k230 palm detection
  • 12.k230 gesture recognition
  • 13.k230OCR character recognition
  • 14.k230 object detection
  • 15.k230 target tracking
  • 16.k230 self-learning object recognition
  • 17.k230 license plate recognition
7. MSPM0-K210Vision module application
  • 0.Prerequisites for K210 as a coprocessor
  • 1.MSPM0_K210 color recognition
  • 2.MSPM0_K210 barcode recognition
  • 3.MSPM0_K210 QR code recognition
  • 4.MSPM0_K210 mechanical code recognition
  • 5.MSPM0_K210 face detection
  • 6.MSPM0_K210 facial feature detection
  • 7.MSPM0_K210 mask detection
  • 8.MSPM0_K210 face recognition
  • 9.MSPM0_K210 Object Detection
  • 10.MSPM0_K210 Self-study classification
  • 11.MSPM0_K210 handwritten digit recognition
8. Expansion casenew
  • 1.Autonomous driving car
  • 2.Vehicle-mounted balance ball motion control system
Download
  • Code
  • Hareware_Info
  • Software_Tools
  • Communication Protocol
Download ZIP

Welcome to MSPM0G3507 Development Board repository

10. Get MPU6050 data (I2C)

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
3V3VCC
GNDGND

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:
  • Support input/output configuration: The pin needs to support configuration as input or output mode in software and be able to switch dynamically through the program.
  • Hardware restrictions and conflicts: Make sure that the selected pin is not assigned to other hardware functions or peripherals to avoid conflicts.
  • Electrical characteristics: The electrical characteristics of the pins should meet the standard requirements of the I2C bus, such as the correct level and driving capability.

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

​x
#ifndef _BSP_MPU6050_H_
#define _BSP_MPU6050_H_
​
#include "board.h"
​
​
//设置SDA输出模式 Set SDA output mode
#define SDA_OUT()   {                                                  \
                        DL_GPIO_initDigitalOutput(I2C_SDA_IOMUX);     \
                        DL_GPIO_setPins(I2C_PORT, I2C_SDA_PIN);      \
                        DL_GPIO_enableOutput(I2C_PORT, I2C_SDA_PIN); \
                    }
//设置SDA输入模式 Set SDA input mode
#define SDA_IN()    { DL_GPIO_initDigitalInput(I2C_SDA_IOMUX); }
​
//获取SDA引脚的电平变化 Get the level change of the SDA pin
#define SDA_GET()   ( ( ( DL_GPIO_readPins(I2C_PORT,I2C_SDA_PIN) & I2C_SDA_PIN ) > 0 ) ? 1 : 0 )
//SDA与SCL输出 SDA and SCL output
#define SDA(x)      ( (x) ? (DL_GPIO_setPins(I2C_PORT,I2C_SDA_PIN)) : (DL_GPIO_clearPins(I2C_PORT,I2C_SDA_PIN)) )
#define SCL(x)      ( (x) ? (DL_GPIO_setPins(I2C_PORT,I2C_SCL_PIN)) : (DL_GPIO_clearPins(I2C_PORT,I2C_SCL_PIN)) )
​
​
//MPU6050的AD0是IIC地址引脚,接地则IIC地址为0x68,接VCC则IIC地址为0x69
//AD0 of MPU6050 is the IIC address pin. If it is grounded, the IIC address is 0x68. If it is connected to VCC, the IIC address is 0x69.
​
​
#define MPU6050_RA_SMPLRT_DIV       0x19        //陀螺仪采样率 地址  Gyro sampling rate address
#define MPU6050_RA_CONFIG           0x1A        //设置数字低通滤波器 地址 Set digital low-pass filter address
#define MPU6050_RA_GYRO_CONFIG      0x1B        //陀螺仪配置寄存器 Gyro configuration register
#define MPU6050_RA_ACCEL_CONFIG     0x1C        //加速度传感器配置寄存器 Acceleration sensor configuration register
#define MPU_INT_EN_REG              0X38        //中断使能寄存器 Interrupt enable register
#define MPU_USER_CTRL_REG           0X6A        //用户控制寄存器 User control register
#define MPU_FIFO_EN_REG             0X23        //FIFO使能寄存器 FIFO enable register
#define MPU_PWR_MGMT2_REG           0X6C        //电源管理寄存器2 Power management register 2
#define MPU_GYRO_CFG_REG            0X1B        //陀螺仪配置寄存器 Gyroscope configuration register
#define MPU_ACCEL_CFG_REG           0X1C        //加速度计配置寄存器 Accelerometer configuration register
#define MPU_CFG_REG                 0X1A        //配置寄存器 Configuration register
#define MPU_SAMPLE_RATE_REG         0X19        //采样频率分频器 Sampling frequency divider
#define MPU_INTBP_CFG_REG           0X37        //中断/旁路设置寄存器 Interrupt/bypass setting register
​
#define MPU6050_RA_PWR_MGMT_1       0x6B
#define MPU6050_RA_PWR_MGMT_2       0x6C
​
#define MPU6050_WHO_AM_I            0x75
#define MPU6050_SMPLRT_DIV          0            //8000Hz  
#define MPU6050_DLPF_CFG            0        
#define MPU6050_GYRO_OUT            0x43         //MPU6050陀螺仪数据寄存器地址 MPU6050 gyroscope data register address
#define MPU6050_ACC_OUT             0x3B         //MPU6050加速度数据寄存器地址 MPU6050 acceleration data register address
        
#define MPU6050_RA_TEMP_OUT_H       0x41        //温度高位 High temperature
#define MPU6050_RA_TEMP_OUT_L       0x42        //温度低位 Low temperature
​
#define MPU_ACCEL_XOUTH_REG         0X3B        //加速度值,X轴高8位寄存器 Acceleration value, X-axis high 8-bit register
#define MPU_ACCEL_XOUTL_REG         0X3C        //加速度值,X轴低8位寄存器 Acceleration value, X-axis low 8-bit register
#define MPU_ACCEL_YOUTH_REG         0X3D        //加速度值,Y轴高8位寄存器 Acceleration value, Y-axis high 8-bit register
#define MPU_ACCEL_YOUTL_REG         0X3E        //加速度值,Y轴低8位寄存器 Acceleration value, Y-axis low 8-bit register
#define MPU_ACCEL_ZOUTH_REG         0X3F        //加速度值,Z轴高8位寄存器 Acceleration value, Z-axis high 8-bit register
#define MPU_ACCEL_ZOUTL_REG         0X40        //加速度值,Z轴低8位寄存器 Acceleration value, Z-axis low 8-bit register
​
#define MPU_TEMP_OUTH_REG           0X41        //温度值高八位寄存器 Temperature value high eight bits register
#define MPU_TEMP_OUTL_REG           0X42        //温度值低8位寄存器 Temperature value lower 8 bits register
​
#define MPU_GYRO_XOUTH_REG          0X43        //陀螺仪值,X轴高8位寄存器 Gyroscope value, X-axis high 8-bit register
#define MPU_GYRO_XOUTL_REG          0X44        //陀螺仪值,X轴低8位寄存器 Gyroscope value, X-axis low 8-bit register
#define MPU_GYRO_YOUTH_REG          0X45        //陀螺仪值,Y轴高8位寄存器 Gyroscope value, Y-axis high 8-bit register
#define MPU_GYRO_YOUTL_REG          0X46        //陀螺仪值,Y轴低8位寄存器 Gyroscope value, Y-axis low 8-bit register
#define MPU_GYRO_ZOUTH_REG          0X47        //陀螺仪值,Z轴高8位寄存器 Gyroscope value, Z-axis high 8-bit register
#define MPU_GYRO_ZOUTL_REG          0X48        //陀螺仪值,Z轴低8位寄存器 Gyroscope value, Z-axis low 8-bit register
​
​
char MPU6050_WriteReg(uint8_t addr,uint8_t regaddr,uint8_t num,uint8_t *regdata);
char MPU6050_ReadData(uint8_t addr, uint8_t regaddr,uint8_t num,uint8_t* Read);
​
char MPU6050_Init(void);
void MPU6050ReadGyro(short *gyroData);
void MPU6050ReadAcc(short *accData);
float MPU6050_GetTemp(void);
uint8_t MPU6050ReadID(void);
#endif

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)

xxxxxxxxxx
#include "bsp_mpu6050.h"
#include "stdio.h"
​
​
/******************************************************************
 * 函 数 名 称:IIC_Start
 * 函 数 说 明:IIC起始时序
 * 函 数 形 参:无
 * 函 数 返 回:无
 * 作       者:LC
 * 备       注:无
 * Function name: IIC_Start
 * Function description: IIC start timing
 * Function parameter: None
 * Function return: None
 * Author: LC
 * Remarks: None
******************************************************************/
void IIC_Start(void)
{
        SDA_OUT();
        SCL(1); 
        SDA(0);
        
        SDA(1);
        delay_us(5);
        SDA(0);
        delay_us(5);
                       
        SCL(0);
}
/******************************************************************
 * 函 数 名 称:IIC_Stop
 * 函 数 说 明:IIC停止信号
 * 函 数 形 参:无
 * 函 数 返 回:无
 * 作       者:LC
 * 备       注:无
 * Function name: IIC_Stop
 * Function description: IIC stop signal
 * Function parameters: None
 * Function return: None
 * Author: LC
 * Notes: None
******************************************************************/
void IIC_Stop(void)
{
        SDA_OUT();
        SCL(0);
        SDA(0);
        
        SCL(1);
        delay_us(5);
        SDA(1);
        delay_us(5);
        
}
​
...

Then write the following code in the empty.c file

xxxxxxxxxx
#include "board.h"
#include <stdio.h>
#include "bsp_mpu6050.h"
#include "inv_mpu.h"
​
char buf[100];  // 定义缓冲区 Defining buffers
​
int main(void)
{
    // 开发板初始化 Development board initialization
    board_init();
    
    // 初始化 MPU6050 Initialize MPU6050
    MPU6050_Init();
    
    uint8_t ret = 1;
    float pitch = 0, roll = 0, yaw = 0;  // 欧拉角 Euler Angles
​
    printf("start\r\n");
​
    // DMP 初始化 DMP initialization
    while (mpu_dmp_init())
    {
        printf("DMP error\r\n");
        delay_ms(200);
    }
    
    printf("Initialization Data Succeed \r\n");
​
    while (1)
    {
        // 获取欧拉角 Get Euler angles
        if (mpu_dmp_get_data(&pitch, &roll, &yaw) == 0)
        {
            // 格式化数据并发送 Format data and send
            sprintf(buf, "pitch = %d, roll = %d, yaw = %d\n", (int)pitch, (int)roll, (int)yaw);
            uart0_send_string(buf);  // 串口发送 Serial port sending
        }
        else
        {
            // 获取数据失败 Failed to obtain data
            printf("Data get error\r\n");
        }
​
        delay_ms(200);  // 延时,根据实际采样率调整 Delay, adjusted according to the actual sampling rate
    }
​
}

6. Compile

image-20241127155238783

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

4. Program Analysis

  • inv_mpu.c

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.

  • empty.c

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

DMA transfer
LCD Display (SPI)

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文