从 Arduino IDE 切换到 STM32CubeIDE ,已经基于 HAL 库在 CubeIDE 中编写了 MPU6050 的驱动代码。但是当我对代码进行调试并查看传感器采集的数值时,能明显发现 I2C 的通信连接并没有正常建立。为了排除开发板硬件故障的可能,我把旧的 Arduino 程序烧录后测试,传感器在该环境下能正常工作。
我使用的硬件是STM32F401CCU6 ,搭配 ST-Link V2 仿真器调试。目前这套硬件 + 软件环境下,我能正常运行简单的 LED 闪烁程序,但唯独 I2C 外设与 MPU6050 的硬件对接一直无法成功。想请教各位,我是否在 CubeIDE 的 **.ioc 配置文件 ** 中遗漏了某些 I2C 相关的配置项?
#include "main.h"
I2C_HandleTypeDef hi2c1;
#define MPU_ADDR 0x68
int initcheck = 0;
unsigned char dx, dy, dz, da;
uint32_t LoopTimer;
float ax, ay, az, gx, gy, gz;
float gcx, gcy, gcz; //gyro calibration
int n_gc = 0; //gyro calib counter
//kalman vars
float roll, pitch;
float K_1D[] = {0,0};
float K_Roll = 0, KU_Roll = 4;
float K_Pitch = 0, KU_Pitch = 4;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
void MPU_Init()//initialize MPU6050
{
uint8_t check;
uint8_t data;
HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR, 0x75, 1, &check, 1, 1000); //0x75 is WHO_AM_I register
if(check == 104)//0x68 is 104
//if(1)
{
initcheck = 11;
data=0x00;
HAL_I2C_Mem_Write(&hi2c1, MPU_ADDR, 0x6B, 1, &data, 1, 1000);//PWR_MGMT
data=0x07;
HAL_I2C_Mem_Write(&hi2c1, MPU_ADDR, 0x19, 1, &data, 1, 1000);//SMPLRT_DIV
data=0x00;
HAL_I2C_Mem_Write(&hi2c1, MPU_ADDR, 0x1C, 1, &data, 1, 1000);//ACCEL_CONFIG set to 8g
data=0x00;
HAL_I2C_Mem_Write(&hi2c1, MPU_ADDR, 0x1A, 1, &data, 1, 1000);//GYRO_CONFIG set to 500dps
}
else initcheck = 99;
}
void readAcc()
{
uint8_t Rec_Data[6];
HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR, 0x3B, 1, Rec_Data, 6, 1000);//3B is ACCEL_XOUT, Requesting 6 bytes (each axis reading is 2 bytes)
ax = ((int16_t)(Rec_Data[0] << 8 | Rec_Data[1]))/16384.0;
ay = ((int16_t)(Rec_Data[2] << 8 | Rec_Data[3]))/16384.0;
az = ((int16_t)(Rec_Data[4] << 8 | Rec_Data[5]))/16384.0;
}
void readGyro()
{
uint8_t Rec_Data[6];
HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR, 0x43, 1, Rec_Data, 6, 1000);//43 is GYRO_XOUT, Requesting 6 bytes (each axis reading is 2 bytes)
gx = ((int16_t)(Rec_Data[0] << 8 | Rec_Data[1]))/131.0;
gy = ((int16_t)(Rec_Data[2] << 8 | Rec_Data[3]))/131.0;
gz = ((int16_t)(Rec_Data[4] << 8 | Rec_Data[5]))/131.0;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
MPU_Init();
for(n_gc = 0; n_gc<2000;n_gc++)
{
readGyro();
gcx += gx;
gcy += gy;
gcz += gz;
HAL_Delay(10);
}
gcx /= 2000;
gcy /= 2000;
gcz /= 2000;
//LoopTimer = micros();
while (1)
{
readAcc();
readGyro();
gx -= gcx;
gy -= gcy;
gz -= gcz;
}
/* USER CODE END 3 */
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
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_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
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();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* 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 */
|
|