如何使用 STM32CUBE HAL 库进行连续 i2c 读取

Posted

技术标签:

【中文标题】如何使用 STM32CUBE HAL 库进行连续 i2c 读取【英文标题】:How to use the STM32CUBE HAL library for a coninuouse i2c read 【发布时间】:2020-10-02 01:37:16 【问题描述】:

我正在使用实际的 HAL 库读取和写入带有 stm32f407 发现板的 AD7998 模数转换器。 如果我想从转换结果寄存器中读取几个寄存器值,手册上说应该保持SCL和SDA信号,我不能发送停止位。 AD7998 signal pattern to read several register values of one register.

我尝试过使用 Mem_Read。这似乎适用于一个 2 字节的寄存器。但现在我不确定如何访问转换结果寄存器的其他部分以读取比第一个通道更多的内容。 如有必要,您可以在此处找到手册 -> AD7998 manual,以及下面的代码。

我尝试增加要读取的字节数,但随后第一个通道的值被写入其他字节。 你们有没有人试过这个?

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "i2s.h"
#include "tim.h"
#include "usb_device.h"
#include "gpio.h"

/* USER CODE BEGIN PV */
uint16_t DeviceAdress= 0x20 << 1;
/* USER CODE END PV */

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 */
  HAL_StatusTypeDef stat = 0;
  uint8_t adcTxBuffer[16];
  uint8_t adcRxBuffer[16];
  /* 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_I2S3_Init();
  MX_SPI1_Init();
  MX_USB_DEVICE_Init();
  MX_TIM14_Init();
  /* USER CODE BEGIN 2 */

  adcTxBuffer[0] = 0x00;
  adcTxBuffer[1] = 0xF8;                                                          // setup 4 channels (0, 1, 2, 3)
  stat = HAL_I2C_Mem_Write(&hi2c1, DeviceAdress, 0x02, 2, adcTxBuffer, 2, 100);   //access the configuration register
  stat = HAL_I2C_Mem_Read(&hi2c1, DeviceAdress, 0x02, 2, adcRxBuffer, 2, 100);    // read vlaue from Configuration Register 
 HAL_GPIO_TogglePin(GPIOA, 1);                                                   // start up adc
  HAL_Delay(1);                                                                  // delay for adc power up
  HAL_GPIO_TogglePin(GPIOA, 1);                                                  // delay for sampling complete
  HAL_Delay(1);                                                                  // delay for sampling complete

  stat = HAL_I2C_Mem_Read(&hi2c1, DeviceAdress, 0x00, 2, adcRxBuffer, 16, 100);  // read conversion result register

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  
  /* USER CODE END 3 */


/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)

  RCC_OscInitTypeDef RCC_OscInitStruct = 0;
  RCC_ClkInitTypeDef RCC_ClkInitStruct = 0;
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = 0;

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 8;
  RCC_OscInitStruct.PLL.PLLN = 336;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  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_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  
    Error_Handler();
  
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
  PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
  PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  
    Error_Handler();
  


/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)

  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */


#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */

#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

【问题讨论】:

【参考方案1】:

通过在每次迭代中配置我的寄存器,找到了另一种读取数据的方法。现在工作正常。

【讨论】:

以上是关于如何使用 STM32CUBE HAL 库进行连续 i2c 读取的主要内容,如果未能解决你的问题,请参考以下文章

STM32 Cube固件库编程之新建工程

用HAL库结合STM cube编写代码控制stm32f103c8t6来驱动减速电机实现慢快逐步切换转动

STM32使用cube生成的程序后在keil5编译后首次SWD可以下载再次下载不行的解决办法。

使用HAL库函数建立STM32F2工程

使用HAL库开发STM32:ADC基础使用

HAL库系列2.STM32CubeIDE新建工程