I m working on a "self-balancing robot" with STM32F303RE.I m using STCubeMX to generate the MCU configuration code and then I added the main() to get the values from MPU6050 from the following 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 */
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
/* 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(GPIOA, GPIO_PIN_5);
break;
}
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
i2cBuff[0]= 0x3B;
HAL_I2C_Master_Transmit(&hi2c1, mpu6050Address, i2cBuff, 1, 10);
i2cBuff[1] = 0x00;
HAL_I2C_Master_Receive(&hi2c1, mpu6050Address, &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 */
}
But, when I add ax,ay,az or Xaccel,Yaccel,Zaccel variables to watch it is always showing 0.What can be the problem?