我正在尝试通过i2c读取加速度计数据。加速度计的i2c地址为0x19(用于读取)和0x18(用于写入)。为了检查与加速度计的通讯是否正确,我应该读取寄存器0x00,并且其结果必须返回0x12。但是我无法从加速度计获得认可。我正在使用HAL库和使用CubeMX生成的代码。这是代码:

#include "main.h"
#include "stm32l0xx_hal.h"

I2C_HandleTypeDef hi2c1;

void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);


uint8_t data[2];
uint8_t result;


int main(void)
{
    HAL_Init();

    /* Configure the system clock */
    SystemClock_Config();

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();

    while (1)
    {

          //    HAL_I2C_Mem_Read_DMA(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size)
                HAL_I2C_Mem_Read_DMA(&hi2c1, 0x19, 0x00, 1, &result, 1);
                HAL_Delay(100);
     }
}

 void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

  /**Configure the main internal regulator output voltage  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

   /**Initializes the CPU, AHB and APB busses clocks  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  RCC_OscInitStruct.MSICalibrationValue = 0;
  RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_5;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

    /**Initializes the CPU, AHB and APB busses clocks */

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                          |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
     Error_Handler();
  }

  /**Configure the Systick interrupt time   */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

   /**Configure the Systick    */
   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* I2C1 init function */
static void MX_I2C1_Init(void)
{

   hi2c1.Instance = I2C1;
   hi2c1.Init.Timing = 0x00000708;
   hi2c1.Init.OwnAddress1 = 0x18;
   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
   hi2c1.Init.OwnAddress2 = 0;
   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
   {
     Error_Handler();
   }

     /**Configure Analogue filter  */
   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
   {
     Error_Handler();
   }

 }

 static void MX_GPIO_Init(void)
 {

   /* GPIO Ports Clock Enable */
   __HAL_RCC_GPIOC_CLK_ENABLE();
   __HAL_RCC_GPIOA_CLK_ENABLE();

 }


使用2个4.7kOhms的外部上拉电阻。

mc引脚:
https://developer.mbed.org/platforms/ST-Nucleo-L011K4/

引脚D4和D5分别连接到SDA和SCL。 GND和3.3V分别连接至加速度计的电源连接。

最佳答案

评估板的SDA引脚为PB7,SCL引脚为PB6。但是,您无需初始化这些引脚的时钟。您应该在MX_GPIO_Init功能中启用端口B时钟。

static void MX_GPIO_Init(void)
 {

   /* GPIO Ports Clock Enable */
   __HAL_RCC_GPIOC_CLK_ENABLE();
   __HAL_RCC_GPIOA_CLK_ENABLE();
   __HAL_RCC_GPIOB_CLK_ENABLE();

 }


您应该在MX_I2C1_Init函数中将OwnAddress1设置为0。

hi2c1.Init.OwnAddress1 = 0;


HAL_I2C_Mem_Read_DMAHAL_I2C_Mem_Read函数中,读取的地址也应为0x18。我不确定时间是否正确。大多数I2C设备的时钟速度在100-400Khz之间。您的I2C设备时钟速度是多少?您可以尝试100或400Khz。

hi2c1.Init.Timing = 400000;

关于c - stm32 Nucleo-L011K4 i2c确认失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42855475/

10-11 22:33