I have an STM32F303 Discovery and I want to use the I2C bus to receive the x,y,z data from an accelerometer MPU6050. Does anyone have source code for this? The problem is that the address of the buffers (i2cBuff) are 0, and the Xaccel, Yaccel, Zaccel too.
Here is my code:
#include "main.h"
#include "stm32f3xx_hal.h"
/* Private variables-----*/
I2C_HandleTypeDef hi2c1;
uint8_t i;
uint8_t i2cBuff[8];
uint16_t ax,ay,az;
float Xaccel,Yaccel,Zaccel;
#define mpu6050Address 0xD0
int main(void)
{
HAL_Init();
/* Configure the system clock */
/* USER CODE BEGIN 2 */
for(uint8_t i=0 ; i <255;i++)
{
if(HAL_I2C_IsDeviceReady(&hi2c1,i,1 ,10) == HAL_OK )
{
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
break;
}
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_I2C_Mem_Read(&hi2c1, mpu6050Address, 0x3B, I2C_MEMADD_SIZE_8BIT, &i2cBuff[1], 6, 10);
ax = -(i2cBuff[1]<<8 | i2cBuff[2]);
ay = -(i2cBuff[3]<<8 | i2cBuff[4]);
az = -(i2cBuff[5]<<8 | i2cBuff[6]);
Xaccel = ax/8192.0;
Yaccel = ay/8192.0;
Zaccel = az/8192.0;
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}