STM32 HAL库ADC采样简记

CubeMX

通道0使用规则,通道1使用注入
STM32 HAL库ADC采样简记_第1张图片
启用DMA,
STM32 HAL库ADC采样简记_第2张图片
开启中断
STM32 HAL库ADC采样简记_第3张图片

编码

main.h

/* USER CODE BEGIN EFP */
#define ADC_BUF_LEN		1
extern uint16_t adc_reg_buf[ADC_BUF_LEN];
extern uint16_t adc_inj_buf[ADC_BUF_LEN];
/* USER CODE END EFP */

main.c

/* USER CODE BEGIN PV */
uint16_t adc_reg_buf[ADC_BUF_LEN];
uint16_t adc_inj_buf[ADC_BUF_LEN];
/* USER CODE END PV */

/**
  * @brief  The application entry point.
  * @retval int
  */
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_DMA_Init();
  MX_ADC1_Init();
  /* USER CODE BEGIN 2 */
	HAL_ADC_Start_DMA(&hadc1, (uint32_t *)adc_reg_buf, ADC_BUF_LEN);
	HAL_ADCEx_InjectedStart(&hadc1);
	HAL_ADC_Start_IT(&hadc1);
  /* USER CODE END 2 */

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

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

stm32f1xx_it.c

/**
  * @brief This function handles ADC1 and ADC2 global interrupts.
  */
void ADC1_2_IRQHandler(void)
{
  /* USER CODE BEGIN ADC1_2_IRQn 0 */
	if (__HAL_ADC_GET_FLAG(&hadc1, ADC_FLAG_JEOC))
	{
		adc_inj_buf[0] = HAL_ADCEx_InjectedGetValue(&hadc1, ADC_INJECTED_RANK_1);
	}
  /* USER CODE END ADC1_2_IRQn 0 */
  HAL_ADC_IRQHandler(&hadc1);
  /* USER CODE BEGIN ADC1_2_IRQn 1 */

  /* USER CODE END ADC1_2_IRQn 1 */
}

你可能感兴趣的:(工作日志(二),#,嵌入式,stm32,单片机,嵌入式硬件)