HAL库 && MPU6050
HAL库 MPU6050的使用:今天在本教程中,我们将使用STM32接口MPU6050(GY-521)加速度计陀螺仪。同时,我将在PC上利用串口显示这些值,陀螺仪将通过I2C1连接。我用的是STM32F411RE NUCLEO板。您将用到CubeMX && KEIL5
我们将用到:
- LED用于闪烁判断程序正常运行
- UART2用于发送数据至PC
- I2C用于读取MPU6050数据
代码结构
- 我们将重定义 fputc 函数,用于串口显示
- MPU6050初始化函数,加速度、角速度、温度初始值读取及处理函数
- 主程序调用以上函数并向PC发送,以及LED状态显示
一些链接
- 做了一个全流程视频置于B站,涉及CubeMX的配置、代码编写以及一些寄存器地址的获取,如有需要请前往观看。链接:https://www.bilibili.com/video/BV1qf4y1r73k/ 点我直达B站
- 整体工程已打包至BaiDu网盘,链接:链接:https://pan.baidu.com/s/1mZcVfZNuphs1Had6ijaRfw 点我直达网盘
提取码:5zx4
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V3的分享
主程序
#include "main.h"
#include "stdio.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);
#define MPU6050_ADDR 0xD0
#define SMPLRT_DIV_REG 0x19
#define GYRO_CONFIG_REG 0x1B
#define ACCEL_CONFIG_REG 0x1c
#define ACCEL_XOUT_H_REG 0x3B
#define TEMP_OUT_H_REG 0x41
#define GYRO_XOUT_H_REG 0x43
#define PWR_MGMT_1_REG 0x6B
#define WHO_AM_I_REG 0x75
int16_t Accel_X_RAW = 0;
int16_t Accel_Y_RAW = 0;
int16_t Accel_Z_RAW = 0;
int16_t Gyro_X_RAW = 0;
int16_t Gyro_Y_RAW = 0;
int16_t Gyro_Z_RAW = 0;
int16_t Temp_RAW = 0;
float Ax,Ay,Az,Gx,Gy,Gz,Temp;
void MPU6050_Init(void )
{
uint8_t check,Data;
// check device ID WHO_AM_I
HAL_I2C_Mem_Read (&hi2c1 ,MPU6050_ADDR,WHO_AM_I_REG,1,&check ,1,1000);
if(check == 104) //if the device is present
{
//power management register 0x6B we should write all 0's to wake the sensor up
Data = 0;
HAL_I2C_Mem_Write (&hi2c1 ,MPU6050_ADDR ,PWR_MGMT_1_REG ,1,&Data ,1,1000);
//Set DATA RATE of 1KHz by writing SMPLRT_DIV register
Data = 0x07;
HAL_I2C_Mem_Write (&hi2c1 ,MPU6050_ADDR ,SMPLRT_DIV_REG ,1 ,&Data,1,1000);
// Set accelerometer configuration in ACCEL_CONFIG Register
// XA_ST=0,YA_ST=0,ZA_ST=0, FS_SEL=0 ->±2g
Data = 0x00;
HAL_I2C_Mem_Write (&hi2c1 ,MPU6050_ADDR, ACCEL_CONFIG_REG, 1, &Data, 1, 1000);
// Set Gyroscopic configuration in GYRO_CONFIG Register
// XG_ST=0,YG_ST=0, FS_SEL=0 ->± 250 °/s
Data = 0x00;
HAL_I2C_Mem_Write (&hi2c1 ,MPU6050_ADDR, GYRO_CONFIG_REG, 1, &Data, 1, 1000);
}
}
void MPU6050_Read_Accel(void)
{
uint8_t Rec_Data[6];
//Read 6 BYTES of data starting from ACCEL_XOUT_H register
HAL_I2C_Mem_Read (&hi2c1 ,MPU6050_ADDR ,ACCEL_XOUT_H_REG ,1,Rec_Data ,6,1000);
Accel_X_RAW = (int16_t )(Rec_Data [0] <<8 | Rec_Data [1]);
Accel_Y_RAW = (int16_t )(Rec_Data [2] <<8 | Rec_Data [3]);
Accel_Z_RAW = (int16_t )(Rec_Data [4] <<8 | Rec_Data [5]);
Ax = Accel_X_RAW/16384.0;
Ay = Accel_Y_RAW/16384.0;
Az = Accel_Z_RAW/16384.0;
}
void MPU6050_Read_Gyro(void )
{
uint8_t Rec_Data[6];
// Read 6 BYTES of data staring from GYRO_XOUT_H register
HAL_I2C_Mem_Read (&hi2c1, MPU6050_ADDR ,GYRO_XOUT_H_REG ,1,Rec_Data ,6 ,1000);
Gyro_X_RAW = (int16_t )(Rec_Data [0] << 8 | Rec_Data [1]);
Gyro_Y_RAW = (int16_t )(Rec_Data [2] << 8 | Rec_Data [3]);
Gyro_Z_RAW = (int16_t )(Rec_Data [4] << 8 | Rec_Data [5]);
Gx = Gyro_X_RAW/131.0;
Gy = Gyro_Y_RAW/131.0;
Gz = Gyro_Z_RAW/131.0;
}
void MPU6050_Read_Temp(void )
{
uint8_t Rec_Data[2];
HAL_I2C_Mem_Read (&hi2c1 ,MPU6050_ADDR ,TEMP_OUT_H_REG ,1 ,Rec_Data ,2 ,1000);
Temp_RAW = (int16_t )(Rec_Data [0]<<8)|Rec_Data [1];
Temp = 36.53 + (Temp_RAW ) / 340;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MX_USART2_UART_Init();
// for(uint8_t i=0;i<255;i++)
// {
// if(HAL_I2C_IsDeviceReady (&hi2c1 ,i ,1 ,1000)== HAL_OK )
// {
// HAL_GPIO_TogglePin (LD2_GPIO_Port ,LD2_Pin );
// printf("%d\r\n",i);
// break;
// }
// }
MPU6050_Init ();
printf ("****** MPU6050 Test 3s ******\r\n");
HAL_Delay (1000);
printf ("****** MPU6050 Test 2s ******\r\n");
HAL_Delay (1000);
printf ("****** MPU6050 Test 1s ******\r\n");
HAL_Delay (1000);
while (1)
{
HAL_GPIO_TogglePin (LD2_GPIO_Port ,LD2_Pin );
MPU6050_Read_Accel ();
MPU6050_Read_Gyro ();
MPU6050_Read_Temp ();
// 发送至PC
printf("Ax=%.2f,Ay=%.2f,Az=%.2f\r\n",Ax,Ay,Az);
printf("Gx=%.2f,Gy=%.2f,Gz=%.2f\r\n",Gx,Gy,Gz);
printf ("Temperature=%.2f,\r\n",Temp );
HAL_Delay (500);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = { 0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 100;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
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_3) != HAL_OK)
{
Error_Handler();
}
}
static void MX_I2C1_Init(void)
{
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = { 0};
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
GPIO_InitStruct.Pin = LD2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
}
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit (&huart2 ,(uint8_t *)&ch,1,HAL_MAX_DELAY );
return ch;
}
void Error_Handler(void)
{
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
}
#endif