2018-07-13 01:46 PM
I am trying to read MPU6050 IMU sensor data with i2c communcation. Initial codes are generated with CubeMx. No matter what i do i could not solve the i2c busy bus problem. When i try to polling method and put reading function directly into while loop, i2c bus busy flag is set between 10 seconds to 1 minute after start. When i use MPU6050 boars data ready interrupt pin and call read function in EXTI interrupt nothing is changing. Interrupt and DMA methods gives only 1 reading and busy flag is set after first reading.I also tried to add 4.7k ohms parallel to boards 4.7k ohms in order to make resistor equavalent to 2k but it did not work too. Do you have any suggestions. I've tried to change another STM32F407VG board but result is still the same.
I2C2 Settings
static void MX_I2C2_Init(void)
{ hi2c2.Instance = I2C2; hi2c2.Init.ClockSpeed = 400000;
hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_16_9;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{ _Error_Handler(__FILE__, __LINE__); } }
Functions that i use.Unimportant parts are ommited.
void MPU6050_Init(void) // initialize mpu6050 and set registers
{TxBuffer[0] = 0x80;
HAL_I2C_Mem_Write(&hi2c2, MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, TxBuffer, 1, 1000);HAL_Delay(50);TxBuffer[0] = 0x00;
HAL_I2C_Mem_Write(&hi2c2, MPU6050_DEFAULT_ADDRESS, MPU6050_RA_PWR_MGMT_1, 1, TxBuffer, 1, 1000);HAL_Delay(50);..........................
}
void MPU6050_Read(void)
{HAL_I2C_Mem_Read_IT(&hi2c2, MPU6050_DEFAULT_ADDRESS, MPU6050_RA_ACCEL_XOUT_H, I2C_MEMADD_SIZE_8BIT, DataBuffer, 14);for(int i=0;i<7;i++)
{ MPU6050_Buffer[i] = (int16_t)(((uint16_t)DataBuffer[2*i]<<8) | DataBuffer[2*i + 1]); }......................// calculations}
MPU6050_Init function is called just before while loop in main function
/* USER CODE BEGIN 2 */
MPU6050_Init(); /* USER CODE END 2 */while (1)
{MPU6050_Read() while(__HAL_TIM_GET_COUNTER(&htim6)-loop_timer<loop_timer_value); // Main loop frequency is fixed to 250Hz loop_timer=__HAL_TIM_GET_COUNTER(&htim6); HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15);}
2018-07-15 04:03 PM
7bit addressing on IIC...
try MPU6050_DEFAULT_ADDRESS << 1
HAL_I2C_Mem_Write(&hi2c2, MPU6050_DEFAULT_ADDRESS << 1, MPU6050_RA_PWR_MGMT_1, 1, TxBuffer, 1, 1000);
2018-07-16 01:27 AM
,
,
thanks for fast reply T J. I've already define ,
MPU6050_DEFAULT_ADDRESS
,as , ♯ define MPU6050_ADDRESS (0x68<,<,1). So i2c is communication only for a few minutes or seconds. Afterwards busy flag is set and communication stops