cancel
Showing results for 
Search instead for 
Did you mean: 

I am trying to read the measurement registers from MPU6050 via i2c, the MCU was able to detect the MPU6050, but i am not able to read the measurement registers, i am using the Hal i2c drivers and i have pasted my code ?

TGhau.2088
Associate II

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();

 MX_USART2_UART_Init();

 MX_USART3_UART_Init();

 /* USER CODE BEGIN 2 */

if(HAL_I2C_IsDeviceReady(&hi2c1,0xD0,5,10)==HAL_OK)

{

HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);

HAL_Delay(300);

}

unsigned int raw;

float AccX;

 /* USER CODE END 2 */

uint8_t i2cdata[4];

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

i2cdata[0]=0x3B;

HAL_I2C_Master_Transmit(&hi2c1,0xD0,i2cdata,1,10);

HAL_Delay(500);

HAL_I2C_Master_Receive(&hi2c1,0xD0,&i2cdata[1],2,10);

raw = (i2cdata[1]<<8|i2cdata[2]);

AccX=((float)raw/16384.0);

HAL_UART_Transmit(&huart2,(unsigned char*)&AccX,5,100);

HAL_Delay(1000);

//HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

2 REPLIES 2

Perhaps print the values as ASCII numbers you can read?

Try reading the WHO AM I register, confirm you can do that correctly.

I'd perhaps use the memory read form to pull register content:

uint8_t Value[2]; // XOUT

status = HAL_I2C_Mem_Read(&I2CHandle, 0xD0, 0x3B, I2C_MEMADD_SIZE_8BIT, &Value, 2, 100);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
TGhau.2088
Associate II

Thank you that worked.