[求助] 求救,想用HAL的I2C來讀取BMA250E的ChipID。到底是哪裡寫錯了呢?

Elecbuggy   2016-7-6 16:55 楼主
大家好,我用的版子是STM32F429I DISCOVERY,目前在學習HAL庫的I2C用法。

想要讀取BOSCH 的BMA250E 這個G-sensor的 CHIP_ID,請幫我看一下我哪裡寫錯了呢??  資料完全無法讀寫的感覺。
chip id 是一個固定的值0XF9
BMA250E的I2C位置是0X18
CHIP_ID的唯讀暫存器位置是0X00

  1. #include "stm32f4xx_hal.h"
  2. static void SystemClock_Config(void);

  3. #define I2C_ADDRESS 0x18

  4. uint8_t aTxBuffer[2],aRxBuffer;
  5. int main(){

  6.   HAL_Init();

  7.   SystemClock_Config();
  8.    
  9.    
  10.   I2C_HandleTypeDef I2cHandle;
  11.   /*##-1- Configure the I2C peripheral #######################################*/
  12.   I2cHandle.Instance             = I2C1;
  13.   
  14.   I2cHandle.Init.AddressingMode  = I2C_ADDRESSINGMODE_7BIT;
  15.   I2cHandle.Init.ClockSpeed      = 400000;
  16.   I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  17.   I2cHandle.Init.DutyCycle       = I2C_DUTYCYCLE_16_9;
  18.   I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  19.   I2cHandle.Init.NoStretchMode   = I2C_NOSTRETCH_DISABLE;
  20.   I2cHandle.Init.OwnAddress1     = 0;
  21.   I2cHandle.Init.OwnAddress2     = 0;
  22.   HAL_I2C_Init(&I2cHandle);
  23.    



  24.         aTxBuffer[0]=0x00;
  25.         HAL_I2C_Master_Transmit(&I2cHandle, I2C_ADDRESS,aTxBuffer, 1, 10000);
  26.         HAL_I2C_Master_Receive(&I2cHandle,I2C_ADDRESS|0x01 ,&aRxBuffer, 1, 10000);
  27.         HAL_Delay(1000);

  28.    
  29. }


  30. static void SystemClock_Config(void)
  31. {
  32.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  33.   RCC_OscInitTypeDef RCC_OscInitStruct;
  34.   
  35.   /* The voltage scaling allows optimizing the power consumption when the device is
  36.      clocked below the maximum system frequency, to update the voltage scaling value
  37.      regarding system frequency refer to product datasheet.  */
  38.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  39.   
  40.   /* Enable HSE Oscillator and activate PLL with HSE as source */
  41.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  42.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  43.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  44.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  45.   RCC_OscInitStruct.PLL.PLLM = 8;
  46.   RCC_OscInitStruct.PLL.PLLN = 360;
  47.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  48.   RCC_OscInitStruct.PLL.PLLQ = 7;
  49.   HAL_RCC_OscConfig(&RCC_OscInitStruct);

  50.   /* Activate the Over-Drive mode */
  51.   HAL_PWREx_EnableOverDrive();

  52.   /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  53.      clocks dividers */
  54.   RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  55.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  56.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  57.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;  
  58.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;  
  59.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
  60.    
  61. }
  62. #define I2Cx_CLK_ENABLE()                __HAL_RCC_I2C1_CLK_ENABLE()
  63. #define I2Cx_SDA_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()
  64. #define I2Cx_SCL_GPIO_CLK_ENABLE()       __HAL_RCC_GPIOB_CLK_ENABLE()

  65. #define I2Cx_FORCE_RESET()               __HAL_RCC_I2C1_FORCE_RESET()
  66. #define I2Cx_RELEASE_RESET()             __HAL_RCC_I2C1_RELEASE_RESET()

  67. /* Definition for I2Cx Pins */
  68. #define I2Cx_SCL_PIN                    GPIO_PIN_6
  69. #define I2Cx_SCL_GPIO_PORT              GPIOB
  70. #define I2Cx_SCL_AF                     GPIO_AF4_I2C1
  71. #define I2Cx_SDA_PIN                    GPIO_PIN_9
  72. #define I2Cx_SDA_GPIO_PORT              GPIOB
  73. #define I2Cx_SDA_AF                     GPIO_AF4_I2C1



  74. void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  75. {  
  76.   GPIO_InitTypeDef  GPIO_InitStruct;
  77.   
  78.   /*##-1- Enable peripherals and GPIO Clocks #################################*/
  79.   /* Enable GPIO TX/RX clock */
  80.   I2Cx_SCL_GPIO_CLK_ENABLE();
  81.   I2Cx_SDA_GPIO_CLK_ENABLE();
  82.   /* Enable I2C1 clock */
  83.   I2Cx_CLK_ENABLE();
  84.   
  85.   /*##-2- Configure peripheral GPIO ##########################################*/  
  86.   /* I2C TX GPIO pin configuration  */
  87.   GPIO_InitStruct.Pin       = I2Cx_SCL_PIN;
  88.   GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
  89.   GPIO_InitStruct.Pull      = GPIO_PULLUP;
  90.   GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
  91.   GPIO_InitStruct.Alternate = I2Cx_SCL_AF;
  92.   
  93.   HAL_GPIO_Init(I2Cx_SCL_GPIO_PORT, &GPIO_InitStruct);
  94.    
  95.   /* I2C RX GPIO pin configuration  */
  96.   GPIO_InitStruct.Pin = I2Cx_SDA_PIN;
  97.   GPIO_InitStruct.Alternate = I2Cx_SDA_AF;
  98.    
  99.   HAL_GPIO_Init(I2Cx_SDA_GPIO_PORT, &GPIO_InitStruct);
  100. }

  101. /**
  102.   * @brief I2C MSP De-Initialization
  103.   *        This function frees the hardware resources used in this example:
  104.   *          - Disable the Peripheral's clock
  105.   *          - Revert GPIO, DMA and NVIC configuration to their default state
  106.   * @param hi2c: I2C handle pointer
  107.   * @retval None
  108.   */
  109. void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  110. {
  111.   /*##-1- Reset peripherals ##################################################*/
  112.   I2Cx_FORCE_RESET();
  113.   I2Cx_RELEASE_RESET();

  114.   /*##-2- Disable peripherals and GPIO Clocks ################################*/
  115.   /* Configure I2C Tx as alternate function  */
  116.   HAL_GPIO_DeInit(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_PIN);
  117.   /* Configure I2C Rx as alternate function  */
  118.   HAL_GPIO_DeInit(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_PIN);
  119. }


回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复