简体   繁体   English

stm32 Nucleo-L011K4 i2c确认失败

[英]stm32 Nucleo-L011K4 i2c acknowledgement failure

I am trying to read accelerometer data over i2c. 我正在尝试通过i2c读取加速度计数据。 The i2c address of the accelerometer is 0x19 for reading and 0x18 for writing. 加速度计的i2c地址为0x19(用于读取)和0x18(用于写入)。 To check for correct communication with the accelerometer, I should read register 0x00 and it must return 0x12 as the result. 为了检查与加速度计的通讯是否正确,我应该读取寄存器0x00,并且其结果必须返回0x12。 But I am not able to get acknowledgement from the accelerometer. 但是我无法从加速度计获得认可。 I am using the HAL libraries and code generated using CubeMX. 我正在使用HAL库和使用CubeMX生成的代码。 Here is the code: 这是代码:

#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 external pull up resistors of 4,7kOhms are used. 使用了2个4.7kOhms的外部上拉电阻。

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

pins D4 and D5 to SDA and SCL respectively. 引脚D4和D5分别连接到SDA和SCL。 GND and 3.3V are connected to accelerometers respective power connections. GND和3.3V分别连接至加速度计的电源连接。

The SDA pin of board is PB7 and SCL pin is PB6. 评估板的SDA引脚为PB7,SCL引脚为PB6。 However, you don't initialize these pins clock. 但是,您无需初始化这些引脚的时钟。 You should enable port B clock in MX_GPIO_Init function. 您应该在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();

 }

You should set OwnAddress1 to 0 in MX_I2C1_Init function. 您应该在MX_I2C1_Init函数中将MX_I2C1_Init设置为0。

hi2c1.Init.OwnAddress1 = 0;

Also read address should be 0x18 in HAL_I2C_Mem_Read_DMA or HAL_I2C_Mem_Read function. HAL_I2C_Mem_Read_DMAHAL_I2C_Mem_Read函数中,读取地址也应为0x18。 I am not sure Timing is correct. 我不确定时间是否正确。 Most of I2C devices clock speed is between 100-400Khz. 大多数I2C设备的时钟速度在100-400Khz之间。 What is your I2C device clock speed? 您的I2C设备时钟速度是多少? You can try at 100 or 400Khz. 您可以尝试100或400Khz。

hi2c1.Init.Timing = 400000;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM