当前位置:网站首页>How to use bear pie to get mpu6050 six axis original data

How to use bear pie to get mpu6050 six axis original data

2020-12-07 16:24:31 Huawei cloud developer community

Abstract : Use the bear pie development board , Through hardware IIC And MPU6050 Six axis sensor module communication , Complete the corresponding register configuration , Successful acquisition of gyroscope 、 Accelerometer data .

This question is mainly about the use of bear pie development board +MPU6050 Six axis sensors , Get accelerometer and gyroscope information .

One 、 Experiment preparation

1. Experimental environment

  • A piece of stm32 Development board ( Bear pie is recommended ), And data lines
  • Is already installed STM32CubeMX
  • Is already installed KeilMDK, And import stm32 Development board corresponding chip package ( The bear pie uses STM32L431RCT6)
  • One MPU6050 modular (IIC Interface ) And DuPont line

2. Target effect

  • adopt CubeMX Create a project and configure the parameters
  • IIC Means of communication , To configure MPU6050 register
  • The cubs passed IIC, Get gyroscope 、 Accelerometer data
  • A serial port 1 Reorient the rotation angle of the output encoder 、 angular velocity

Two 、 adopt CubeMX production MDK engineering

A. Chip selection

  • open CubeMX, Go to chip selection :

  • Choose your own stm32 chip ( namely STM32L431RCT6):

B. Clock source RCC Set up

  • Change system clock source

The system clock uses the internal high-speed clock by default (HSI), Choose to use HSE, The clock is more accurate

  • Set the port corresponding to the external clock

  • Configure clock tree

STM32L431RCT6 The maximum system clock can be 80MHz, We can configure it to the maximum

C. Parameter configuration ( Corresponding port settings )

1) To configure USART1

Use USART, The pattern is asynchronous , The baud rate is 115200, No hardware flow control

2) Hardware IIC To configure

IIC(Inter-Integrated Circuit) It's actually IICBus abbreviation , So Chinese should be called integrated circuit bus , It's a serial communication bus , Using multi master slave architecture

We use bear pie I2C1, The pin from bear pie is PB7->I2C1_SDA,PB6->I2C1_SCL

We can leave the other options by default

3)MPU6050 modular

Of course , We also need to understand MPU6050 The circuit of the module , Better configuration . Of course, we can also make our own modules

If AD0 foot (9 foot ) Grounding ,IIC The address is 0X68( Not including the lowest order ); If AD0 Foot joint V3.3, be IIC The address is 0X69( Not including the lowest order ).

You can see ,AD0 The pin has been pulled low , All default IIC The address is 0x68.

Last , Generating code is just OK 了

D. Engineering setup

Some basic settings , Including the project name 、 Storage location 、 Engineering environment 、 The composition of various documents in the project

E. The generated code

3、 ... and 、 Write the corresponding code

1. A serial port 1 Output redirection

We know printf It's a print function , The principle is to format and print the input string parameters to stdout in . We need to get printf Print to serial port , Only need usart.c Copy in the file printf Write an output function

  • Add header file
/* USER CODE BEGIN 0 */#include <stdarg.h>#include <string.h>#include <stdio.h>/* USER CODE END 0 */
  • Write the output function
/* USER CODE BEGIN 1 */void UsartPrintf(UART_HandleTypeDef *huart, char *fmt,...){

 unsigned char UsartPrintfBuf[296];
 va_list ap;
 unsigned char *pStr = UsartPrintfBuf;

 va_start(ap, fmt);
 vsprintf((char *)UsartPrintfBuf, fmt, ap); // format 
 va_end(ap);

 while(*pStr != 0)
 {
  USART1->TDR = *pStr++;
  while((USART1->ISR & 0x40)  == 0);
 }}// Be careful : stay usart.h Add void UsartPrintf(UART_HandleTypeDef *huart, char *fmt,...);// Usage method :UsartPrintf(&huart1;,"hello world\r\n");/* USER CODE END 1 */

Be careful : Add your own code , Need to be in begin and end Between

2. Write MPU6050 drive

We create two files , Namely mpu6050.h and mpu6050.c

a). First of all we have mpu6050.h The middle macro defines the corresponding register , Convenient for subsequent use .

stay main Function , We first set the initial value of the counter to 10000, Then turn on the encoder , Every time 1S Get the update value of the counter once , Judge the encoder information
Because there are so many registers , It's not listed one by one , You can check it in the attachment ~

b). Configure each register

The main function is :IIC Reading and writing 、 initialization MPU6050、 Set gyroscope and accelerometer full scale 、 Set the sampling rate 、 Reading data

There is... In the code attachment , Not one by one .

Initialization function

uint8_t MPUInit(void){
 uint8_t res;
 extern I2C_HandleTypeDef hi2c1;

 HAL_I2C_Init(&hi2c1;); // initialization IIC

 /* It's better to have a certain delay after power on , Ensure data accuracy */
 HAL_Delay(500);
 MPU_Write_Byte(MPU_PWR_MGMT1_REG,0x80); // Reset MPU6050, write in 1000 0000
 MPU_Write_Byte(MPU_PWR_MGMT1_REG,0x00);//sleep write in 0, Stay awake 

 MPU_Set_Gyro_Fsr(3);  // Gyroscope full range ±2000
 MPU_Set_Accel_Fsr(0); // Accelerometer full scale ±2g
 MPU_Set_Rate(50);     // Set the sampling rate =50Hz

 MPU_Write_Byte(MPU_INT_EN_REG,0x00);// Turn off all interrupts 
 MPU_Write_Byte(MPU_USER_CTRL_REG,0x00); // Close from iic
 MPU_Write_Byte(MPU_FIFO_EN_REG,0x00);   // close FIFO
 MPU_Write_Byte(MPU_INTBP_CFG_REG,0x80);  //INT Pin low level active 

 res = MPU_Read_Byte(MPU_DEVICE_ID_REG);  // Read address 
 UsartPrintf(&huart1;,"\r\nMPU6050:0x%2x\r\n",res);
 UsartPrintf(&huart1;,"\r\nMPU6050:0x%2x\r\n",MPU_ADDR);
 if(res == MPU_ADDR) // Determine if the address is correct 
 {
  MPU_Write_Byte(MPU_PWR_MGMT1_REG,0x01); //001,pll,x The axis is the clock source of the reference system 
  MPU_Write_Byte(MPU_PWR_MGMT2_REG,0x00); // No standby mode 
  MPU_Set_Rate(50);
 }else return 1;

 return 0;}

It needs to be noted that ,mpu6050 After power on, there needs to be a certain delay , Keep the data stable . restart mpu6050 There will be sleep patterns , Write off sleep . Finally, test and judge IIC Is the address correct

3.main function

/* USER CODE BEGIN PV */short x;short y;short z;/* USER CODE END PV */
 /* USER CODE BEGIN 2 */
 MPUInit();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 while(MPU_Get_Gyroscope(&x,&y,&z) !=0);
 HAL_Delay(1000);
   printf("\r\n gyroscope :x=-,y=-,z=-\r\n",x,y,z);
 while(MPU_Get_Accelerometer(&x,&y,&z) != 0);
 HAL_Delay(1000);
   printf("\r\n Accelerometer :x=-,y=-,z=-\r\n",x,y,z);
    /* USER CODE BEGIN 3 */
  }

Four 、 compile + download

Click compile ,0 error,0 warning

Bear pie is connected to the computer , Download the code to the development board

5、 ... and 、 Connect hardware

1. Connect the bear pie 1 Connected to the computer , Dial to AT-MUC, Press the reset key

2. Connect MPU6050

We just need to use mpu6050 Of SDA\SCL\GND\VCC.
namely :

  • SDA-->PB7
  • SCL-->PB6
  • GND-->GND
  • VCC-->5V

3. effect

Open the serial debugging assistant , We manually rotate the development board ( Including modules ), See the gyroscope and acceleration data changing , When stationary, the data remains stable .

thus , We've been able to send IIC Get the original data information of six axis sensor

This article is shared from Huawei cloud community 《 Bear school practice —— obtain MPU6050 Six axis raw data 》, Original author :ttking .

 

Click to follow , The first time to learn about Huawei's new cloud technology ~

版权声明
本文为[Huawei cloud developer community]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012071615424758.html