一、实验效果 显示mpu6050三轴加速度原始数据,三轴角速度原始数据。 硬件:stm32f427vit6单片机 、mpu6050模块 软件:STM32CubeIDE 1.8.0 C语言 二、硬件连接:
PB8(IC21_SCL)------SCL AD0----GND(确定地址) VCC—3.3V PB9(IC21_SDA)-----SDA GND—GND
三、过程 1、打开STM32CubeIDE 1.8.0新建工程 2、设置系统时钟
3、设置下载方式设置s-link下载
4、配置时钟树
5、配置IC2通讯串口通讯 在这里插入图片描述
6、单击工程新建源文件夹mycode(在工程名右击选择New—source
7、在源文件夹新建文件夹mpu6050然后在文件新建两个文件mpu6050.c 与 mpu6050.h mpu6050.c 代码如下: #include “main.h” #include “mpu6050.h” #include “i2c.h” #include “usart.h” void mpu6050_init(void) { uint8_t temp; temp=0x00; HAL_I2C_Mem_Write(&hi2c1,ADDRESS_Write,PWR_MGMT_1, I2C_MEMADD_SIZE_8BIT, &temp, 1, 0x10); temp=0x07; HAL_I2C_Mem_Write(&hi2c1,ADDRESS_Write,SMPLRT_DIV, I2C_MEMADD_SIZE_8BIT, &temp, 1, 0x10); temp=0x06; HAL_I2C_Mem_Write(&hi2c1,ADDRESS_Write,CONFIG, I2C_MEMADD_SIZE_8BIT, &temp, 1, 0x10); temp=0x18; HAL_I2C_Mem_Write(&hi2c1,ADDRESS_Write,RA_GYRO_CONFIG, I2C_MEMADD_SIZE_8BIT, &temp, 1, 0x10); temp=0x01; HAL_I2C_Mem_Write(&hi2c1,ADDRESS_Write,RA_ACCEL_CONFIG, I2C_MEMADD_SIZE_8BIT, &temp, 1, 0x10);
}
//获取三个轴的加速度原始值
void mpu6050_getaccel(int16_t *aacx,int16_t *aacy,int16_t *aacz) { uint8_t value[2];
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,ACCEL_XOUT_L, I2C_MEMADD_SIZE_8BIT, &value[0], 1, 0x10);
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,ACCEL_XOUT_H, I2C_MEMADD_SIZE_8BIT, &value[1], 1, 0x10);
*aacx=(value[1]<<8)+value[0];
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,ACCEL_YOUT_L, I2C_MEMADD_SIZE_8BIT, &value[0], 1, 0x10);
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,ACCEL_YOUT_H, I2C_MEMADD_SIZE_8BIT, &value[1], 1, 0x10);
*aacy=(value[1]<<8)+value[0];
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,ACCEL_ZOUT_L, I2C_MEMADD_SIZE_8BIT, &value[0], 1, 0x10);
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,ACCEL_ZOUT_H, I2C_MEMADD_SIZE_8BIT, &value[1], 1, 0x10);
*aacy=(value[1]<<8)+value[0];
printf("ACCEL value:%d %d %d\r\n",*aacx,*aacy,*aacz);
}
//获取三个轴的角速度原始值 void mpu6050_getGYRO(int16_t *gyrox,int16_t *gyroy,int16_t *gyroz) { uint8_t value[2];
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,GYRO_XOUT_L, I2C_MEMADD_SIZE_8BIT, &value[0], 1, 0x10);
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,GYRO_XOUT_H, I2C_MEMADD_SIZE_8BIT, &value[1], 1, 0x10);
*gyrox=(value[1]<<8)+value[0];
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,GYRO_YOUT_L, I2C_MEMADD_SIZE_8BIT, &value[0], 1, 0x10);
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,GYRO_YOUT_H, I2C_MEMADD_SIZE_8BIT, &value[1], 1, 0x10);
*gyroy=(value[1]<<8)+value[0];
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,GYRO_ZOUT_L, I2C_MEMADD_SIZE_8BIT, &value[0], 1, 0x10);
HAL_I2C_Mem_Read(&hi2c1, ADDRESS_Read,GYRO_ZOUT_H, I2C_MEMADD_SIZE_8BIT, &value[1], 1, 0x10);
*gyroz=(value[1]<<8)+value[0];
printf("GYRO value:%d %d %d\r\n",*gyrox,*gyroy,*gyroz);
}
mpu6050.h 代码如下:
#define SlaveAddress (MPU6050_ADDRESS_AD0_LOW<<1) void mpu6050_init(void); void mpu6050_getaccel(int16_t *aacx,int16_t *aacy,int16_t *aacz); void mpu6050_getGYRO(int16_t *gyrox,int16_t gyroy,int16_t gyroz); #endif 在主函数加入 #include “…/…/mycode/mpu6050/mpu6050.h” 加入串口打印代码 / USER CODE BEGIN 0 / #include “stdio.h” #ifdef GNUC #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) PUTCHAR_PROTOTYPE { HAL_UART_Transmit(&huart1, (uint8_t)&ch, 1, HAL_MAX_DELAY); return ch; } #endif int main(void) / USER CODE END 0 / int main(void) { / USER CODE BEGIN 1 / int16_t x,y,z; int16_t a,b,c; / USER CODE END 1 / … MX_GPIO_Init(); MX_I2C1_Init(); MX_USART1_UART_Init(); MX_TIM6_Init(); / USER CODE BEGIN 2 / mpu6050_init(); printf(“this is mpu6050 test\r\n”); / USER CODE END 2 / / USER CODE BEGIN WHILE */ while (1) { mpu6050_getaccel(&x,&y,&z); mpu6050_getGYRO(&a,&b,&c); HAL_Delay(1000); } 最后效果图
|