stm32 f469 用i2c 讀取 mpu6050 一直無法正確讀取數據

#include "main.h"

#include "stm32f4xx_hal.h"


/* USER CODE BEGIN Includes */


/* USER CODE END Includes */


/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;


UART_HandleTypeDef huart3;


/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/


uint16_t timeout = 100;

uint16_t len = 1;


uint8_t test;


uint16_t inlen = 14;

uint8_t infor[14];


uint16_t daddress = 0x68;


uint16_t raddress1 = 0x6B;

uint16_t raddress2 = 0x1B;

uint16_t raddress3 = 0x1C;

uint16_t raddress4 = 0x3B;


uint8_t data = 0x80;

uint8_t data2 = 0x00;

uint8_t data3 = 0x00;


uint8_t ax, ay, az, gx, gy, gz;


/* USER CODE END PV */


/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);

static void MX_USART3_UART_Init(void);


/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/


/* USER CODE END PFP */


/* USER CODE BEGIN 0 */


/* USER CODE END 0 */


/**

  * @brief  The application entry point.

  *

  * @retval None

  */

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

  /* USER CODE BEGIN 2 */

/*

GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;

GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

*/

HAL_I2C_Mem_Write(&hi2c1, daddress , raddress1, len, &data, len, timeout);

HAL_Delay(100);

HAL_I2C_Mem_Write(&hi2c1, daddress , raddress1, len, &data2, len, timeout);

HAL_Delay(100);

HAL_I2C_Mem_Write(&hi2c1, daddress , raddress2, len, &data3, len, timeout);

HAL_Delay(100);

HAL_I2C_Mem_Write(&hi2c1, daddress , raddress3, len, &data3, len, timeout);

HAL_Delay(100);


  /* USER CODE END 2 */


  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {


  /* USER CODE END WHILE */


  /* USER CODE BEGIN 3 */

HAL_Delay(500);

HAL_I2C_Mem_Read(&hi2c1, daddress , raddress4, len, infor, inlen, timeout);

ax = (infor[0] << 8) | infor[1];

ay = (infor[2] << 8) | infor[3];

az = (infor[4] << 8) | infor[5];

gx = (infor[6] << 8) | infor[7];

gy = (infor[10] << 8) | infor[11];

gz = (infor[12] << 8) | infor[13];

printf("   ax   ay   az\n\r");

printf("%5d%5d%5d\n\r", ax, ay, az);


  }

  /* USER CODE END 3 */


}


------------------------------------------------------------------------------------------------------------------------------------------------------------------------

我想要读取mpu6050里的六轴资料

我一开始先reset( 寄存器: 0x6B -> 0x80)

然后设定震盪器( 寄存器: 0x6B -> 0x00)


接着设定  gyroscope   为  +- 250( 寄存器 : 0x1B -> 0x00)

                acceleration 为 +- 2g   ( 寄存器: 0x1C -> 0x00)

接着就开始读取资料 ( 寄存器: 0x3B ~ 0x48) 


但是读出来的 ax, ay, az 一直都是0

看了许多前辈的文章,都可以正常执行HAL_I2C_Mem_Read 和HAL_I2C_Mem_Write

想请教各位前辈们,我是哪一部分写错了







已邀请:

FXX

赞同来自:

这也太.... 我用的HAL库,MPU6050 ,自带卡尔曼滤波,中断功能。

要回复问题请先登录注册