Академический Документы
Профессиональный Документы
Культура Документы
MAIN.C
#include "main.h"
/* USER CODE BEGIN Includes */
#include "TJ_MPU6050.h"
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN 0 */
RawData_Def myAccelRaw, myGyroRaw;
ScaledData_Def myAccelScaled, myGyroScaled;
/* USER CODE END 0 */
int main(void)
{
MPU_ConfigTypeDef myMpuConfig;
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
//1. Initialise the MPU6050 module and I2C
MPU6050_Init(&hi2c1);
//2. Configure Accel and Gyro parameters
myMpuConfig.Accel_Full_Scale = AFS_SEL_4g;
myMpuConfig.ClockSource = Internal_8MHz;
myMpuConfig.CONFIG_DLPF = DLPF_184A_188G_Hz;
myMpuConfig.Gyro_Full_Scale = FS_SEL_500;
myMpuConfig.Sleep_Mode_Bit = 0; //1: sleep mode, 0: normal mode
MPU6050_Config(&myMpuConfig);
while (1)
{ /* USER CODE BEGIN 3 */
//Raw data
MPU6050_Get_Accel_RawData(&myAccelRaw);
MPU6050_Get_Gyro_RawData(&myGyroRaw);
//Scaled data
MPU6050_Get_Accel_Scale(&myAccelScaled);
MPU6050_Get_Gyro_Scale(&myGyroScaled);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
HAL_Delay(10);
}
}
DRIVERS DE MPU60-50
//Header files
#include "TJ_MPU6050.h"
//Library Variable
//1- I2C Handle
static I2C_HandleTypeDef i2cHandler;
//2- Accel & Gyro Scaling Factor
static float accelScalingFactor, gyroScalingFactor;
//3- Bias varaibles
static float A_X_Bias = 0.0f;
static float A_Y_Bias = 0.0f;
static float A_Z_Bias = 0.0f;
//Fucntion Definitions
//1- i2c Handler
void MPU6050_Init(I2C_HandleTypeDef *I2Chnd)
{
//Copy I2C CubeMX handle to local library
memcpy(&i2cHandler, I2Chnd, sizeof(*I2Chnd));
}
//2- i2c Read
void I2C_Read(uint8_t ADDR, uint8_t *i2cBif, uint8_t NofData)
{ uint8_t i2cBuf[2];
uint8_t MPUADDR;
//Need to Shift address to make it proper to i2c operation
MPUADDR = (MPU_ADDR<<1);
i2cBuf[0] = ADDR;
HAL_I2C_Master_Transmit(&i2cHandler, MPUADDR, i2cBuf, 1, 10);
HAL_I2C_Master_Receive(&i2cHandler, MPUADDR, i2cBif, NofData, 100);
}
//3- i2c Write
void I2C_Write8(uint8_t ADDR, uint8_t data)
{ uint8_t i2cData[2];
i2cData[0] = ADDR;
i2cData[1] = data;
uint8_t MPUADDR = (MPU_ADDR<<1);
HAL_I2C_Master_Transmit(&i2cHandler, MPUADDR, i2cData, 2,100);
}