STM32基于STM32CubeMX硬件I2C驱动MPU6050读取数据
- ✨申明:本文章仅发表在CSDN网站,任何其他网站,未注明来源,见此内容均为盗链爬取,请多多尊重和支持原创!
- 🍁对于文中所提供的相关资源链接将作不定期更换。
- 🔰说明:本人未开通打赏功能,谨防上当受骗!
- 📍MPU6050 HAL外设驱动文件来源:
https://github.com/lamik/MPU6050_STM32_HAL
- 📍 相关篇《STM32F1基于STM32CubeMX配置移植dmp库通过串口打印MPU6050数据》
基于STM32CubeMX配置工程,当然不局限于STM32其他型号的芯片的使用,只要是stm32芯片都可以使用该源文件进行驱动,方便适配移植,减少不必要的重复开发工作。
-
🎯本工程经实物验证没有问题。
-
🔖MPU6050模块
-
📜串口打印信息
🛠STM32CubeMX配置I2C
- 🌿使能 I2C接口,可以根据个人情况选择 I2C组以及引脚。
- 🌿配置一个串口用于输出MPU6050读取的数据,也可以根据个人需求选择。
- 🔧usart.c文件中添加printf函数重映射,Keil设置中勾选
MicroLib
选项。
#include "stdio.h"
/*可调用printf*/
int fputc(int ch,FILE *f)
{
/*&huart1指的是串口1,如果用别的串口就修改数字*/
HAL_UART_Transmit(&huart1 , (uint8_t *)&ch , 1 , 1000);
return ch;
}
📓MPU6050驱动文件
- 🌿mpu6050.c文件
/*
* mpu6050.c
*
* Created on: 08.10.2018
* License: MIT
* Author: Mateusz Salamon
* Based on:
* - MPU-6000 and MPU-6050 Product Specification Revision 3.4
* - MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2
* - i2cdevlib by Jeff Rowberg on MIT license
* - SparkFun MPU-9250 Digital Motion Processor (DMP) Arduino Library on MIT License
*
* www.msalamon.pl
* mateusz@msalamon.pl
*
* Website: https://msalamon.pl/6-stopni-swobody-z-mpu6050-na-stm32/
* GitHub: https://github.com/lamik/MPU6050_STM32_HAL
*/
//#include "stm32f4xx_hal.h"
#include "stm32f1xx_hal.h"
#include "i2c.h"
#include "mpu6050.h"
#define I2C_TIMEOUT 10
I2C_HandleTypeDef *i2c;
float Acc_Scale;
float Gyr_Scale;
//
// CONFIG
//
void MPU6050_SetDlpf(uint8_t Value)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0xF8;
tmp |= (Value & 0x7);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
}
//
// PWR_MGMT_1
//
void MPU6050_DeviceReset(uint8_t Reset)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1<<MPU6050_PWR1_DEVICE_RESET_BIT);
tmp |= ((Reset & 0x1) << MPU6050_PWR1_DEVICE_RESET_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetSleepEnabled(uint8_t Enable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1<<MPU6050_PWR1_SLEEP_BIT);
tmp |= ((Enable & 0x1) << MPU6050_PWR1_SLEEP_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetCycleEnabled(uint8_t Enable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1<<MPU6050_PWR1_CYCLE_BIT);
tmp |= ((Enable & 0x1) << MPU6050_PWR1_CYCLE_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetTemperatureSensorDisbled(uint8_t Disable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1<<MPU6050_PWR1_TEMP_DIS_BIT);
tmp |= ((Disable & 0x1) << MPU6050_PWR1_TEMP_DIS_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetClockSource(uint8_t Source)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0xF8;
tmp |= (Source & 0x7);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, &tmp, 1, I2C_TIMEOUT);
}
//
// PWR_MGMT_2
//
void MPU6050_SetLowPowerWakeUpFrequency(uint8_t Frequency)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_2, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0x3F;
tmp |= (Frequency & 0x3) << MPU6050_PWR2_LP_WAKE_CTRL_BIT;
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_2, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_AccelerometerAxisStandby(uint8_t XA_Stby, uint8_t YA_Stby, uint8_t ZA_Stby)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_2, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0xC7;
tmp |= ((XA_Stby&0x1) << MPU6050_PWR2_STBY_XA_BIT)|((YA_Stby&0x1) << MPU6050_PWR2_STBY_YA_BIT)|((ZA_Stby&0x1) << MPU6050_PWR2_STBY_ZA_BIT) ;
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_2, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_GyroscopeAxisStandby(uint8_t XG_Stby, uint8_t YG_Stby, uint8_t ZG_Stby)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_2, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0xF8;
tmp |= ((XG_Stby&0x1) << MPU6050_PWR2_STBY_XG_BIT)|((YG_Stby&0x1) << MPU6050_PWR2_STBY_YG_BIT)|((ZG_Stby&0x1) << MPU6050_PWR2_STBY_ZG_BIT) ;
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_PWR_MGMT_2, 1, &tmp, 1, I2C_TIMEOUT);
}
//
// Measurement scale configuration
//
void MPU6050_SetFullScaleGyroRange(uint8_t Range)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_GYRO_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0xE7;
tmp |= ((Range & 0x7) << 3);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_GYRO_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
switch(Range)
{
case MPU6050_GYRO_FS_250:
Gyr_Scale = 0.007633;
break;
case MPU6050_GYRO_FS_500:
Gyr_Scale = 0.015267;
break;
case MPU6050_GYRO_FS_1000:
Gyr_Scale = 0.030487;
break;
case MPU6050_GYRO_FS_2000:
Gyr_Scale = 0.060975;
break;
default:
break;
}
}
void MPU6050_SetFullScaleAccelRange(uint8_t Range)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= 0xE7;
tmp |= ((Range & 0x7) << 3);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
switch(Range)
{
case MPU6050_ACCEL_FS_2:
Acc_Scale = 0.000061;
break;
case MPU6050_ACCEL_FS_4:
Acc_Scale = 0.000122;
break;
case MPU6050_ACCEL_FS_8:
Acc_Scale = 0.000244;
break;
case MPU6050_ACCEL_FS_16:
Acc_Scale = 0.0004882;
break;
default:
break;
}
}
//
// Reading data
//
int16_t MPU6050_GetTemperatureRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_TEMP_OUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
float MPU6050_GetTemperatureCelsius(void)
{
int16_t temp;
temp = MPU6050_GetTemperatureRAW();
return (float)temp / 340 + 36.53;
}
int16_t MPU6050_GetAccelerationXRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
int16_t MPU6050_GetAccelerationYRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_YOUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
int16_t MPU6050_GetAccelerationZRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_ZOUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
void MPU6050_GetAccelerometerRAW(int16_t *x, int16_t *y, int16_t *z)
{
uint8_t tmp[6];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, 1, tmp, 6, I2C_TIMEOUT);
*x = (((int16_t)tmp[0]) << 8) | tmp[1];
*y = (((int16_t)tmp[2]) << 8) | tmp[3];
*z = (((int16_t)tmp[4]) << 8) | tmp[5];
}
void MPU6050_GetAccelerometerScaled(float* x, float* y, float* z)
{
int16_t tmp_x, tmp_y, tmp_z;
MPU6050_GetAccelerometerRAW(&tmp_x, &tmp_y, &tmp_z);
*x = (float)tmp_x * Acc_Scale;
*y = (float)tmp_y * Acc_Scale;
*z = (float)tmp_z * Acc_Scale;
}
int16_t MPU6050_GetRotationXRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
int16_t MPU6050_GetRotationYRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_GYRO_YOUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
int16_t MPU6050_GetRotationZRAW(void)
{
uint8_t tmp[2];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_GYRO_ZOUT_H, 1, tmp, 2, I2C_TIMEOUT);
return (((int16_t)tmp[0]) << 8) | tmp[1];
}
void MPU6050_GetGyroscopeRAW(int16_t *x, int16_t *y, int16_t *z)
{
uint8_t tmp[6];
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_GYRO_XOUT_H, 1, tmp, 6, I2C_TIMEOUT);
*x = (((int16_t)tmp[0]) << 8) | tmp[1];
*y = (((int16_t)tmp[2]) << 8) | tmp[3];
*z = (((int16_t)tmp[4]) << 8) | tmp[5];
}
void MPU6050_GetGyroscopeScaled(float* x, float* y, float* z)
{
int16_t tmp_x, tmp_y, tmp_z;
MPU6050_GetGyroscopeRAW(&tmp_x, &tmp_y, &tmp_z);
*x = (float)tmp_x * Gyr_Scale;
*y = (float)tmp_y * Gyr_Scale;
*z = (float)tmp_z * Gyr_Scale;
}
void MPU6050_GetRollPitch(float* Roll, float* Pitch)
{
float acc_x, acc_y, acc_z;
MPU6050_GetAccelerometerScaled(&acc_x, &acc_y, &acc_z);
*Roll = atan2(acc_y, acc_z) * 180.0 / M_PI;
*Pitch = -(atan2(acc_x, sqrt(acc_y*acc_y + acc_z*acc_z))*180.0)/M_PI;
//yaw = atan2((acc_x *acc_y + acc_x * acc_z), acc_x * acc_x + acc_y*acc_y - acc_z*acc_z) *180.0)/M_PI;
}
//
// Setting INT pin
//
void MPU6050_SetInterruptMode(uint8_t Mode)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTCFG_INT_LEVEL_BIT);
tmp |= ((Mode & 0x1) << MPU6050_INTCFG_INT_LEVEL_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetInterruptDrive(uint8_t Drive)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTCFG_INT_OPEN_BIT);
tmp |= ((Drive & 0x1) << MPU6050_INTCFG_INT_OPEN_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetInterruptLatch(uint8_t Latch)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTCFG_INT_RD_CLEAR_BIT);
tmp |= ((Latch & 0x1) << MPU6050_INTCFG_INT_RD_CLEAR_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetInterruptLatchClear(uint8_t Clear)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTCFG_LATCH_INT_EN_BIT);
tmp |= ((Clear & 0x1) << MPU6050_INTCFG_LATCH_INT_EN_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_PIN_CFG, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetIntEnableRegister(uint8_t Value)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &Value, 1, I2C_TIMEOUT);
}
void MPU6050_SetIntDataReadyEnabled(uint8_t Enable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTERRUPT_DATA_RDY_BIT);
tmp |= ((Enable & 0x1) << MPU6050_INTERRUPT_DATA_RDY_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
}
uint8_t MPU6050_GetIntStatusRegister(void)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_STATUS, 1, &tmp, 1, I2C_TIMEOUT);
return tmp;
}
uint8_t MPU6050_GetDeviceID(void)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_WHO_AM_I, 1, &tmp, 1, I2C_TIMEOUT);
return tmp<<1;
}
//
// Motion functions - not included in documentation/register map
//
void MPU6050_SetDHPFMode(uint8_t Dhpf)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(0x07);
tmp |= Dhpf & 0x7;
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_ACCEL_CONFIG, 1, &tmp, 1, I2C_TIMEOUT);
}
uint8_t MPU6050_GetMotionStatusRegister(void)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_MOT_DETECT_STATUS, 1, &tmp, 1, I2C_TIMEOUT);
return tmp;
}
void MPU6050_SetIntZeroMotionEnabled(uint8_t Enable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTERRUPT_ZMOT_BIT);
tmp |= ((Enable & 0x1) << MPU6050_INTERRUPT_ZMOT_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetIntMotionEnabled(uint8_t Enable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTERRUPT_MOT_BIT);
tmp |= ((Enable & 0x1) << MPU6050_INTERRUPT_MOT_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetIntFreeFallEnabled(uint8_t Enable)
{
uint8_t tmp;
HAL_I2C_Mem_Read(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
tmp &= ~(1 << MPU6050_INTERRUPT_FF_BIT);
tmp |= ((Enable & 0x1) << MPU6050_INTERRUPT_FF_BIT);
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_INT_ENABLE, 1, &tmp, 1, I2C_TIMEOUT);
}
void MPU6050_SetMotionDetectionThreshold(uint8_t Threshold)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_MOT_THR, 1, &Threshold, 1, I2C_TIMEOUT);
}
void MPU6050_SetMotionDetectionDuration(uint8_t Duration)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_MOT_DUR, 1, &Duration, 1, I2C_TIMEOUT);
}
void MPU6050_SetZeroMotionDetectionThreshold(uint8_t Threshold)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_ZRMOT_THR, 1, &Threshold, 1, I2C_TIMEOUT);
}
void MPU6050_SetZeroMotionDetectionDuration(uint8_t Duration)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_ZRMOT_DUR, 1, &Duration, 1, I2C_TIMEOUT);
}
void MPU6050_SetFreeFallDetectionThreshold(uint8_t Threshold)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_FF_THR, 1, &Threshold, 1, I2C_TIMEOUT);
}
void MPU6050_SetFreeFallDetectionDuration(uint8_t Duration)
{
HAL_I2C_Mem_Write(i2c, MPU6050_ADDRESS, MPU6050_RA_FF_DUR, 1, &Duration, 1, I2C_TIMEOUT);
}
//
// Initialization
//
void MPU6050_Init(I2C_HandleTypeDef *hi2c)
{
i2c = hi2c;
MPU6050_DeviceReset(1);
MPU6050_SetSleepEnabled(0);
MPU6050_SetClockSource(MPU6050_CLOCK_INTERNAL);
MPU6050_SetDlpf(MPU6050_DLPF_BW_20);
MPU6050_SetFullScaleGyroRange(MPU6050_GYRO_FS_250);
MPU6050_SetFullScaleAccelRange(MPU6050_ACCEL_FS_2);
}
- 🌿mpu6050.h文件
/*
* mpu6050.h
*
* Created on: 08.10.2018
* License: MIT
* Author: Mateusz Salamon
* Based on:
* - MPU-6000 and MPU-6050 Product Specification Revision 3.4
* - MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2
* - i2cdevlib by Jeff Rowberg on MIT license
* - SparkFun MPU-9250 Digital Motion Processor (DMP) Arduino Library on MIT License
*
* www.msalamon.pl
* mateusz@msalamon.pl
*
* Website: https://msalamon.pl/6-stopni-swobody-z-mpu6050-na-stm32/
* GitHub: https://github.com/lamik/MPU6050_STM32_HAL
*/
#ifndef MPU6050_H_
#define MPU6050_H_
#include "math.h"
#define MPU6050_ADDRESS 0xD0 // AD0 low
//#define MPU6050_ADDRESS 0xD1 // AD0 high
#define M_PI 3.14159265358979323846
// Registers addresses
//
#define MPU6050_RA_SELF_TEST_X 0x0D
#define MPU6050_RA_SELF_TEST_Y 0x0E
#define MPU6050_RA_SELF_TEST_Z 0x0F
#define MPU6050_RA_SELF_TEST_A 0x10
#define MPU6050_RA_SMPLRT_DIV 0x19
#define MPU6050_RA_CONFIG 0x1A
#define MPU6050_RA_GYRO_CONFIG 0x1B
#define MPU6050_RA_ACCEL_CONFIG 0x1C
// Not in documentation
#define MPU6050_RA_FF_THR 0x1D
#define MPU6050_RA_FF_DUR 0x1E
#define MPU6050_RA_MOT_THR 0x1F
#define MPU6050_RA_MOT_DUR 0x20
#define MPU6050_RA_ZRMOT_THR 0x21
#define MPU6050_RA_ZRMOT_DUR 0x22
// Not in documentation end
#define MPU6050_RA_FIFO_EN 0x23
#define MPU6050_RA_I2C_MST_CTRL 0x24
#define MPU6050_RA_I2C_SLV0_ADDR 0x25
#define MPU6050_RA_I2C_SLV0_REG 0x26
#define MPU6050_RA_I2C_SLV0_CTRL 0x27
#define MPU6050_RA_I2C_SLV1_ADDR 0x28
#define MPU6050_RA_I2C_SLV1_REG 0x29
#define MPU6050_RA_I2C_SLV1_CTRL 0x2A
#define MPU6050_RA_I2C_SLV2_ADDR 0x2B
#define MPU6050_RA_I2C_SLV2_REG 0x2C
#define MPU6050_RA_I2C_SLV2_CTRL 0x2D
#define MPU6050_RA_I2C_SLV3_ADDR 0x2E
#define MPU6050_RA_I2C_SLV3_REG 0x2F
#define MPU6050_RA_I2C_SLV3_CTRL 0x30
#define MPU6050_RA_I2C_SLV4_ADDR 0x31
#define MPU6050_RA_I2C_SLV4_REG 0x32
#define MPU6050_RA_I2C_SLV4_DO 0x33
#define MPU6050_RA_I2C_SLV4_CTRL 0x34
#define MPU6050_RA_I2C_SLV4_DI 0x35
#define MPU6050_RA_I2C_MST_STATUS 0x36
#define MPU6050_RA_INT_PIN_CFG 0x37
#define MPU6050_RA_INT_ENABLE 0x38
// Not in documentation
#define MPU6050_RA_DMP_INT_STATUS 0x39
// Not in documentation end
#define MPU6050_RA_INT_STATUS 0x3A
#define MPU6050_RA_ACCEL_XOUT_H 0x3B
#define MPU6050_RA_ACCEL_XOUT_L 0x3C
#define MPU6050_RA_ACCEL_YOUT_H 0x3D
#define MPU6050_RA_ACCEL_YOUT_L 0x3E
#define MPU6050_RA_ACCEL_ZOUT_H 0x3F
#define MPU6050_RA_ACCEL_ZOUT_L 0x40
#define MPU6050_RA_TEMP_OUT_H 0x41
#define MPU6050_RA_TEMP_OUT_L 0x42
#define MPU6050_RA_GYRO_XOUT_H 0x43
#define MPU6050_RA_GYRO_XOUT_L 0x44
#define MPU6050_RA_GYRO_YOUT_H 0x45
#define MPU6050_RA_GYRO_YOUT_L 0x46
#define MPU6050_RA_GYRO_ZOUT_H 0x47
#define MPU6050_RA_GYRO_ZOUT_L 0x48
#define MPU6050_RA_EXT_SENS_DATA_00 0x49
#define MPU6050_RA_EXT_SENS_DATA_01 0x4A
#define MPU6050_RA_EXT_SENS_DATA_02 0x4B
#define MPU6050_RA_EXT_SENS_DATA_03 0x4C
#define MPU6050_RA_EXT_SENS_DATA_04 0x4D
#define MPU6050_RA_EXT_SENS_DATA_05 0x4E
#define MPU6050_RA_EXT_SENS_DATA_06 0x4F
#define MPU6050_RA_EXT_SENS_DATA_07 0x50
#define MPU6050_RA_EXT_SENS_DATA_08 0x51
#define MPU6050_RA_EXT_SENS_DATA_09 0x52
#define MPU6050_RA_EXT_SENS_DATA_10 0x53
#define MPU6050_RA_EXT_SENS_DATA_11 0x54
#define MPU6050_RA_EXT_SENS_DATA_12 0x55
#define MPU6050_RA_EXT_SENS_DATA_13 0x56
#define MPU6050_RA_EXT_SENS_DATA_14 0x57
#define MPU6050_RA_EXT_SENS_DATA_15 0x58
#define MPU6050_RA_EXT_SENS_DATA_16 0x59
#define MPU6050_RA_EXT_SENS_DATA_17 0x5A
#define MPU6050_RA_EXT_SENS_DATA_18 0x5B
#define MPU6050_RA_EXT_SENS_DATA_19 0x5C
#define MPU6050_RA_EXT_SENS_DATA_20 0x5D
#define MPU6050_RA_EXT_SENS_DATA_21 0x5E
#define MPU6050_RA_EXT_SENS_DATA_22 0x5F
#define MPU6050_RA_EXT_SENS_DATA_23 0x60
// Not in documentation
#define MPU6050_RA_MOT_DETECT_STATUS 0x61
// Not in documentation end
#define MPU6050_RA_I2C_SLV0_DO 0x63
#define MPU6050_RA_I2C_SLV1_DO 0x64
#define MPU6050_RA_I2C_SLV2_DO 0x65
#define MPU6050_RA_I2C_SLV3_DO 0x66
#define MPU6050_RA_I2C_MST_DELAY_CTRL 0x67
#define MPU6050_RA_SIGNAL_PATH_RESET 0x68
// Not in documentation
#define MPU6050_RA_MOT_DETECT_CTRL 0x69
// Not in documentation end
#define MPU6050_RA_USER_CTRL 0x6A
#define MPU6050_RA_PWR_MGMT_1 0x6B
#define MPU6050_RA_PWR_MGMT_2 0x6C
#define MPU6050_RA_FIFO_COUNTH 0x72
#define MPU6050_RA_FIFO_COUNTL 0x73
#define MPU6050_RA_FIFO_R_W 0x74
#define MPU6050_RA_WHO_AM_I 0x75
//
// Registers 13 to 16 ?Self Test Registers
// SELF_TEST_X, SELF_TEST_Y, SELF_TEST_Z, and SELF_TEST_A
//
#define MPU6050_SELF_TEST_XA_1_BIT 0x07
#define MPU6050_SELF_TEST_XA_1_LENGTH 0x03
#define MPU6050_SELF_TEST_XA_2_BIT 0x05
#define MPU6050_SELF_TEST_XA_2_LENGTH 0x02
#define MPU6050_SELF_TEST_YA_1_BIT 0x07
#define MPU6050_SELF_TEST_YA_1_LENGTH 0x03
#define MPU6050_SELF_TEST_YA_2_BIT 0x03
#define MPU6050_SELF_TEST_YA_2_LENGTH 0x02
#define MPU6050_SELF_TEST_ZA_1_BIT 0x07
#define MPU6050_SELF_TEST_ZA_1_LENGTH 0x03
#define MPU6050_SELF_TEST_ZA_2_BIT 0x01
#define MPU6050_SELF_TEST_ZA_2_LENGTH 0x02
#define MPU6050_SELF_TEST_XG_1_BIT 0x04
#define MPU6050_SELF_TEST_XG_1_LENGTH 0x05
#define MPU6050_SELF_TEST_YG_1_BIT 0x04
#define MPU6050_SELF_TEST_YG_1_LENGTH 0x05
#define MPU6050_SELF_TEST_ZG_1_BIT 0x04
#define MPU6050_SELF_TEST_ZG_1_LENGTH 0x05
//
// Register 26 ?Configuration
// CONFIG
//
#define MPU6050_CFG_EXT_SYNC_SET_BIT 5
#define MPU6050_CFG_EXT_SYNC_SET_LENGTH 3
#define MPU6050_CFG_DLPF_CFG_BIT 2
#define MPU6050_CFG_DLPF_CFG_LENGTH 3
#define MPU6050_EXT_SYNC_DISABLED 0x0
#define MPU6050_EXT_SYNC_TEMP_OUT_L 0x1
#define MPU6050_EXT_SYNC_GYRO_XOUT_L 0x2
#define MPU6050_EXT_SYNC_GYRO_YOUT_L 0x3
#define MPU6050_EXT_SYNC_GYRO_ZOUT_L 0x4
#define MPU6050_EXT_SYNC_ACCEL_XOUT_L 0x5
#define MPU6050_EXT_SYNC_ACCEL_YOUT_L 0x6
#define MPU6050_EXT_SYNC_ACCEL_ZOUT_L 0x7
#define MPU6050_DLPF_BW_256 0x00
#define MPU6050_DLPF_BW_188 0x01
#define MPU6050_DLPF_BW_98 0x02
#define MPU6050_DLPF_BW_42 0x03
#define MPU6050_DLPF_BW_20 0x04
#define MPU6050_DLPF_BW_10 0x05
#define MPU6050_DLPF_BW_5 0x06
//
// Register 27 ?Gyroscope Configuration
// GYRO_CONFIG
//
#define MPU6050_GCONFIG_FS_SEL_BIT 4
#define MPU6050_GCONFIG_FS_SEL_LENGTH 2
#define MPU6050_GYRO_FS_250 0x00
#define MPU6050_GYRO_FS_500 0x01
#define MPU6050_GYRO_FS_1000 0x02
#define MPU6050_GYRO_FS_2000 0x03
//
// Register 28 ?Accelerometer Configuration
// ACCEL_CONFIG
//
#define MPU6050_ACONFIG_XA_ST_BIT 7
#define MPU6050_ACONFIG_YA_ST_BIT 6
#define MPU6050_ACONFIG_ZA_ST_BIT 5
#define MPU6050_ACONFIG_AFS_SEL_BIT 4
#define MPU6050_ACONFIG_AFS_SEL_LENGTH 2
#define MPU6050_ACONFIG_ACCEL_HPF_BIT 2
#define MPU6050_ACONFIG_ACCEL_HPF_LENGTH 3
#define MPU6050_ACCEL_FS_2 0x00
#define MPU6050_ACCEL_FS_4 0x01
#define MPU6050_ACCEL_FS_8 0x02
#define MPU6050_ACCEL_FS_16 0x03
//
//
//
#define MPU6050_DHPF_RESET 0x00
#define MPU6050_DHPF_5 0x01
#define MPU6050_DHPF_2P5 0x02
#define MPU6050_DHPF_1P25 0x03
#define MPU6050_DHPF_0P63 0x04
#define MPU6050_DHPF_HOLD 0x07
//
// Register 35 ?FIFO Enable
// FIFO_EN
//
#define MPU6050_TEMP_FIFO_EN_BIT 7
#define MPU6050_XG_FIFO_EN_BIT 6
#define MPU6050_YG_FIFO_EN_BIT 5
#define MPU6050_ZG_FIFO_EN_BIT 4
#define MPU6050_ACCEL_FIFO_EN_BIT 3
#define MPU6050_SLV2_FIFO_EN_BIT 2
#define MPU6050_SLV1_FIFO_EN_BIT 1
#define MPU6050_SLV0_FIFO_EN_BIT 0
//
// Register 36 ?I2C Master Control
// I2C_MST_CTRL
//
#define MPU6050_MULT_MST_EN_BIT 7
#define MPU6050_WAIT_FOR_ES_BIT 6
#define MPU6050_SLV_3_FIFO_EN_BIT 5
#define MPU6050_I2C_MST_P_NSR_BIT 4
#define MPU6050_I2C_MST_CLK_BIT 3
#define MPU6050_I2C_MST_CLK_LENGTH 4
#define MPU6050_CLOCK_DIV_500 0x9
#define MPU6050_CLOCK_DIV_471 0xA
#define MPU6050_CLOCK_DIV_444 0xB
#define MPU6050_CLOCK_DIV_421 0xC
#define MPU6050_CLOCK_DIV_400 0xD
#define MPU6050_CLOCK_DIV_381 0xE
#define MPU6050_CLOCK_DIV_364 0xF
#define MPU6050_CLOCK_DIV_348 0x0
#define MPU6050_CLOCK_DIV_333 0x1
#define MPU6050_CLOCK_DIV_320 0x2
#define MPU6050_CLOCK_DIV_308 0x3
#define MPU6050_CLOCK_DIV_296 0x4
#define MPU6050_CLOCK_DIV_286 0x5
#define MPU6050_CLOCK_DIV_276 0x6
#define MPU6050_CLOCK_DIV_267 0x7
#define MPU6050_CLOCK_DIV_258 0x8
//
// Registers 37 to 39 ?I2C Slave 0 Control
// I2C_SLV0_ADDR, I2C_SLV0_REG, and I2C_SLV0_CTRL
//
// Registers 40 to 42 ?I2C Slave 1 Control
// I2C_SLV1_ADDR, I2C_SLV1_REG, and I2C_SLV1_CTRL
//
// Registers 43 to 45 ?I2C Slave 2 Control
// I2C_SLV2_ADDR, I2C_SLV2_REG, and I2C_SLV2_CTRL
//
// Registers 46 to 48 ?I2C Slave 3 Control
// I2C_SLV3_ADDR, I2C_SLV3_REG, and I2C_SLV3_CTRL
//
// Same structure for these registers
//
#define MPU6050_I2C_SLV_RW_BIT 7
#define MPU6050_I2C_SLV_ADDR_BIT 6
#define MPU6050_I2C_SLV_ADDR_LENGTH 7
#define MPU6050_I2C_SLV_EN_BIT 7
#define MPU6050_I2C_SLV_BYTE_SW_BIT 6
#define MPU6050_I2C_SLV_REG_DIS_BIT 5
#define MPU6050_I2C_SLV_GRP_BIT 4
#define MPU6050_I2C_SLV_LEN_BIT 3
#define MPU6050_I2C_SLV_LEN_LENGTH 4
//
// Registers 49 to 53 ?I2C Slave 4 Control
// I2C_SLV4_ADDR, I2C_SLV4_REG, I2C_SLV4_DO, I2C_SLV4_CTRL, and I2C_SLV4_DI
//
#define MPU6050_I2C_SLV4_RW_BIT 7
#define MPU6050_I2C_SLV4_ADDR_BIT 6
#define MPU6050_I2C_SLV4_ADDR_LENGTH 7
#define MPU6050_I2C_SLV4_EN_BIT 7
#define MPU6050_I2C_SLV4_INT_EN_BIT 6
#define MPU6050_I2C_SLV4_REG_DIS_BIT 5
#define MPU6050_I2C_SLV4_MST_DLY_BIT 4
#define MPU6050_I2C_SLV4_MST_DLY_LENGTH 5
//
// Register 54 ?I2C Master Status
// I2C_MST_STATUS
//
#define MPU6050_MST_PASS_THROUGH_BIT 7
#define MPU6050_MST_I2C_SLV4_DONE_BIT 6
#define MPU6050_MST_I2C_LOST_ARB_BIT 5
#define MPU6050_MST_I2C_SLV4_NACK_BIT 4
#define MPU6050_MST_I2C_SLV3_NACK_BIT 3
#define MPU6050_MST_I2C_SLV2_NACK_BIT 2
#define MPU6050_MST_I2C_SLV1_NACK_BIT 1
#define MPU6050_MST_I2C_SLV0_NACK_BIT 0
//
// Register 55 ?INT Pin / Bypass Enable Configuration
// INT_PIN_CFG
//
#define MPU6050_INTCFG_INT_LEVEL_BIT 7
#define MPU6050_INTCFG_INT_OPEN_BIT 6
#define MPU6050_INTCFG_LATCH_INT_EN_BIT 5
#define MPU6050_INTCFG_INT_RD_CLEAR_BIT 4
#define MPU6050_INTCFG_FSYNC_INT_LEVEL_BIT 3
#define MPU6050_INTCFG_FSYNC_INT_EN_BIT 2
#define MPU6050_INTCFG_I2C_BYPASS_EN_BIT 1
#define MPU6050_INTMODE_ACTIVEHIGH 0x00
#define MPU6050_INTMODE_ACTIVELOW 0x01
#define MPU6050_INTDRV_PUSHPULL 0x00
#define MPU6050_INTDRV_OPENDRAIN 0x01
#define MPU6050_INTLATCH_50USPULSE 0x00
#define MPU6050_INTLATCH_WAITCLEAR 0x01
#define MPU6050_INTCLEAR_STATUSREAD 0x00
#define MPU6050_INTCLEAR_ANYREAD 0x01
//
// Register 56 ?Interrupt Enable
// INT_ENABLE
//
// Register 58 ?Interrupt Status
// INT_STATUS
//
// Not in documentation
#define MPU6050_INTERRUPT_FF_BIT 7
#define MPU6050_INTERRUPT_MOT_BIT 6
#define MPU6050_INTERRUPT_ZMOT_BIT 5
// Not in documentation end
#define MPU6050_INTERRUPT_FIFO_OFLOW_BIT 4
#define MPU6050_INTERRUPT_I2C_MST_INT_BIT 3
#define MPU6050_INTERRUPT_DATA_RDY_BIT 0
//
// Not documented
// Register 91 - Motion Status
//
#define MPU6050_MOTION_MOT_XNEG_BIT 7
#define MPU6050_MOTION_MOT_XPOS_BIT 6
#define MPU6050_MOTION_MOT_YNEG_BIT 5
#define MPU6050_MOTION_MOT_YPOS_BIT 4
#define MPU6050_MOTION_MOT_ZNEG_BIT 3
#define MPU6050_MOTION_MOT_ZPOS_BIT 2
#define MPU6050_MOTION_MOT_ZRMOT_BIT 0
//
// Register 103 ?I2C Master Delay Control
// I2C_MST_DELAY_CTR
//
#define MPU6050_DELAYCTRL_DELAY_ES_SHADOW_BIT 7
#define MPU6050_DELAYCTRL_I2C_SLV4_DLY_EN_BIT 4
#define MPU6050_DELAYCTRL_I2C_SLV3_DLY_EN_BIT 3
#define MPU6050_DELAYCTRL_I2C_SLV2_DLY_EN_BIT 2
#define MPU6050_DELAYCTRL_I2C_SLV1_DLY_EN_BIT 1
#define MPU6050_DELAYCTRL_I2C_SLV0_DLY_EN_BIT 0
//
// Register 104 ?Signal Path Reset
// SIGNAL_PATH_RESE
//
#define MPU6050_PATHRESET_GYRO_RESET_BIT 2
#define MPU6050_PATHRESET_ACCEL_RESET_BIT 1
#define MPU6050_PATHRESET_TEMP_RESET_BIT 0
//
// Not documented
// Register 105 - Motion Detect Control
//
#define MPU6050_DETECT_ACCEL_ON_DELAY_BIT 5
#define MPU6050_DETECT_ACCEL_ON_DELAY_LENGTH 2
#define MPU6050_DETECT_FF_COUNT_BIT 3
#define MPU6050_DETECT_FF_COUNT_LENGTH 2
#define MPU6050_DETECT_MOT_COUNT_BIT 1
#define MPU6050_DETECT_MOT_COUNT_LENGTH 2
#define MPU6050_DETECT_DECREMENT_RESET 0x0
#define MPU6050_DETECT_DECREMENT_1 0x1
#define MPU6050_DETECT_DECREMENT_2 0x2
#define MPU6050_DETECT_DECREMENT_4 0x3
#define MPU6050_DELAY_3MS 0b11
#define MPU6050_DELAY_2MS 0b10
#define MPU6050_DELAY_1MS 0b01
#define MPU6050_NO_DELAY 0b00
//
// Register 106 ?User Control
// USER_CTRL
//
#define MPU6050_USERCTRL_FIFO_EN_BIT 6
#define MPU6050_USERCTRL_I2C_MST_EN_BIT 5
#define MPU6050_USERCTRL_I2C_IF_DIS_BIT 4
#define MPU6050_USERCTRL_FIFO_RESET_BIT 2
#define MPU6050_USERCTRL_I2C_MST_RESET_BIT 1
#define MPU6050_USERCTRL_SIG_COND_RESET_BIT 0
//
// Register 107 ?Power Management 1
// PWR_MGMT_1
//
#define MPU6050_PWR1_DEVICE_RESET_BIT 7
#define MPU6050_PWR1_SLEEP_BIT 6
#define MPU6050_PWR1_CYCLE_BIT 5
#define MPU6050_PWR1_TEMP_DIS_BIT 3
#define MPU6050_PWR1_CLKSEL_BIT 2
#define MPU6050_PWR1_CLKSEL_LENGTH 3
#define MPU6050_CLOCK_INTERNAL 0x00
#define MPU6050_CLOCK_PLL_XGYRO 0x01
#define MPU6050_CLOCK_PLL_YGYRO 0x02
#define MPU6050_CLOCK_PLL_ZGYRO 0x03
#define MPU6050_CLOCK_PLL_EXT32K 0x04
#define MPU6050_CLOCK_PLL_EXT19M 0x05
#define MPU6050_CLOCK_KEEP_RESET 0x07
//
// Register 108 ?Power Management 2
// PWR_MGMT_2
//
#define MPU6050_PWR2_LP_WAKE_CTRL_BIT 7
#define MPU6050_PWR2_LP_WAKE_CTRL_LENGTH 2
#define MPU6050_PWR2_STBY_XA_BIT 5
#define MPU6050_PWR2_STBY_YA_BIT 4
#define MPU6050_PWR2_STBY_ZA_BIT 3
#define MPU6050_PWR2_STBY_XG_BIT 2
#define MPU6050_PWR2_STBY_YG_BIT 1
#define MPU6050_PWR2_STBY_ZG_BIT 0
#define MPU6050_WAKE_FREQ_1P25 0x0
#define MPU6050_WAKE_FREQ_5 0x1
#define MPU6050_WAKE_FREQ_20 0x2
#define MPU6050_WAKE_FREQ_40 0x3
//
// Register 117 ?Who Am I
// WHO_AM_I
//
#define MPU6050_WHO_AM_I_BIT 6
#define MPU6050_WHO_AM_I_LENGTH 6
//***********************************//
// FUNCTIONS //
//***********************************//
void MPU6050_Init(I2C_HandleTypeDef *hi2c);
uint8_t MPU6050_GetDeviceID();
//
// CONFIG
//
void MPU6050_SetDlpf(uint8_t Value);
//
// PWR_MGMT_1
//
void MPU6050_DeviceReset(uint8_t Reset);
void MPU6050_SetClockSource(uint8_t Source);
void MPU6050_SetSleepEnabled(uint8_t Enable);
void MPU6050_SetCycleEnabled(uint8_t Enable);
void MPU6050_SetTemperatureSensorDisbled(uint8_t Disable);
//
// PWR_MGMT_2
//
void MPU6050_SetLowPowerWakeUpFrequency(uint8_t Frequency);
void MPU6050_AccelerometerAxisStandby(uint8_t XA_Stby, uint8_t YA_Stby, uint8_t ZA_Stby);
void MPU6050_GyroscopeAxisStandby(uint8_t XG_Stby, uint8_t YG_Stby, uint8_t ZG_Stby);
//
// Measurement scale configuration
//
void MPU6050_SetFullScaleGyroRange(uint8_t Range);
void MPU6050_SetFullScaleAccelRange(uint8_t Range);
//
// Reading data
//
int16_t MPU6050_GetTemperatureRAW(void);
float MPU6050_GetTemperatureCelsius(void);
int16_t MPU6050_GetAccelerationXRAW(void);
int16_t MPU6050_GetAccelerationYRAW(void);
int16_t MPU6050_GetAccelerationZRAW(void);
void MPU6050_GetAccelerometerRAW(int16_t* x, int16_t* y, int16_t* z);
void MPU6050_GetAccelerometerScaled(float* x, float* y, float* z);
int16_t MPU6050_GetRotationXRAW(void);
int16_t MPU6050_GetRotationYRAW(void);
int16_t MPU6050_GetRotationZRAW(void);
void MPU6050_GetGyroscopeRAW(int16_t* x, int16_t* y, int16_t* z);
void MPU6050_GetGyroscopeScaled(float* x, float* y, float* z);
void MPU6050_GetRollPitch(float* Roll, float* Pitch);
//
// Setting INT pin
//
// INT_PIN_CFG register
void MPU6050_SetInterruptMode(uint8_t Mode);
void MPU6050_SetInterruptDrive(uint8_t Drive);
void MPU6050_SetInterruptLatch(uint8_t Latch);
void MPU6050_SetInterruptLatchClear(uint8_t Clear);
// INT_ENABLE register
void MPU6050_SetIntEnableRegister(uint8_t Value);
void MPU6050_SetIntDataReadyEnabled(uint8_t Enable);
// INT_STATUS register
uint8_t MPU6050_GetIntStatusRegister(void);
//
// Motion functions - not included in documentation/register map
//
uint8_t MPU6050_GetMotionStatusRegister(void);
void MPU6050_SetDHPFMode(uint8_t Dhpf);
// INT_ENABLE register
void MPU6050_SetIntZeroMotionEnabled(uint8_t Enable);
void MPU6050_SetIntMotionEnabled(uint8_t Enable);
void MPU6050_SetIntFreeFallEnabled(uint8_t Enable);
void MPU6050_SetMotionDetectionThreshold(uint8_t Threshold);
void MPU6050_SetMotionDetectionDuration(uint8_t Duration);
void MPU6050_SetZeroMotionDetectionThreshold(uint8_t Threshold);
void MPU6050_SetZeroMotionDetectionDuration(uint8_t Duration);
void MPU6050_SetFreeFallDetectionThreshold(uint8_t Threshold);
void MPU6050_SetFreeFallDetectionDuration(uint8_t Duration);
#endif /* MPU6050_H_ */
main主程序代码
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <string.h>
#include "mpu6050.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
int16_t acc_x, acc_y, acc_z, gyr_x, gyr_y, gyr_z;
float ax, ay, az, gx, gy, gz, temperature, roll, pitch;
uint8_t buffer[128];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
MPU6050_Init(&hi2c1);
MPU6050_SetInterruptMode(MPU6050_INTMODE_ACTIVEHIGH);
MPU6050_SetInterruptDrive(MPU6050_INTDRV_PUSHPULL);
MPU6050_SetInterruptLatch(MPU6050_INTLATCH_WAITCLEAR);
MPU6050_SetInterruptLatchClear(MPU6050_INTCLEAR_STATUSREAD);
MPU6050_SetIntEnableRegister(0); // Disable all interrupts
// Enable Motion interrputs
MPU6050_SetDHPFMode(MPU6050_DHPF_5);
MPU6050_SetIntMotionEnabled(1);
MPU6050_SetIntZeroMotionEnabled(1);
MPU6050_SetIntFreeFallEnabled(1);
MPU6050_SetFreeFallDetectionDuration(2);
MPU6050_SetFreeFallDetectionThreshold(5);
MPU6050_SetMotionDetectionDuration(5);
MPU6050_SetMotionDetectionThreshold(2);
MPU6050_SetZeroMotionDetectionDuration(2);
MPU6050_SetZeroMotionDetectionThreshold(4);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
MPU6050_GetAccelerometerScaled(&ax, &ay, &az);
MPU6050_GetGyroscopeScaled(&gx, &gy, &gz);
temperature = MPU6050_GetTemperatureCelsius();
memset(buffer, 0, 128);
sprintf((char *)buffer, "ACC: X: %.2f Y:%.2f Z:%.2f \n\rGYR: X: %.2f Y:%.2f Z:%.2f\n\rTEMP: %.2f\n\r", ax, ay, az, gx, gy, gz, temperature);
printf("mpu6050 data:%s \r\n", buffer);
MPU6050_GetRollPitch(&roll, &pitch);
memset(buffer, 0, 128);
sprintf((char *)buffer, "RPY: Roll: %.2f Pitch: %.2f \n\r", roll, pitch);
printf("mpu6050 data2:%s \r\n", buffer);
HAL_Delay(800);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
📚程序源码
- ✨申明:本文章仅发表在CSDN网站,任何其他网站,未注明来源,见此内容均为盗链爬取,请多多尊重和支持原创!
- 🍁对于文中所提供的相关资源链接将作不定期更换。
- 🔰说明:本人未开通打赏功能,谨防上当受骗!
链接: https://pan.baidu.com/s/103ATPPwi3Tg8vRDDIoTrGA
提取码: w54p