177 lines
4.7 KiB
C
177 lines
4.7 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file adc.c
|
|
* @brief This file provides code for the configuration
|
|
* of the ADC instances.
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2023 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "adc.h"
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
ADC_HandleTypeDef hadc1;
|
|
DMA_HandleTypeDef hdma_adc1;
|
|
|
|
/* ADC1 init function */
|
|
void MX_ADC1_Init(void)
|
|
{
|
|
|
|
/* USER CODE BEGIN ADC1_Init 0 */
|
|
|
|
/* USER CODE END ADC1_Init 0 */
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
|
|
/* USER CODE BEGIN ADC1_Init 1 */
|
|
|
|
/* USER CODE END ADC1_Init 1 */
|
|
|
|
/** Common config
|
|
*/
|
|
hadc1.Instance = ADC1;
|
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
|
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;//ADC_SCAN_DISABLE;
|
|
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
hadc1.Init.LowPowerAutoWait = ENABLE;
|
|
hadc1.Init.ContinuousConvMode = ENABLE;//DISABLE;
|
|
hadc1.Init.NbrOfConversion = 2;
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
|
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T6_TRGO;
|
|
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
|
|
// DMA ENABLE tim7不停止就继续采集数据 DISABLE dma缓冲器满了就停止采样。
|
|
hadc1.Init.DMAContinuousRequests = ENABLE;
|
|
hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN;
|
|
hadc1.Init.OversamplingMode = DISABLE;
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_1;
|
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5;
|
|
sConfig.SingleDiff = ADC_SINGLE_ENDED;
|
|
sConfig.OffsetNumber = ADC_OFFSET_NONE;
|
|
sConfig.Offset = 0;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Configure Regular Channel
|
|
*/
|
|
sConfig.Channel = ADC_CHANNEL_2;
|
|
sConfig.Rank = ADC_REGULAR_RANK_2;
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
/* USER CODE BEGIN ADC1_Init 2 */
|
|
|
|
/* USER CODE END ADC1_Init 2 */
|
|
|
|
}
|
|
|
|
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
|
if(adcHandle->Instance==ADC1)
|
|
{
|
|
/* USER CODE BEGIN ADC1_MspInit 0 */
|
|
|
|
/* USER CODE END ADC1_MspInit 0 */
|
|
|
|
/** Initializes the peripherals clock
|
|
*/
|
|
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
|
|
PeriphClkInit.AdcClockSelection = RCC_ADCCLKSOURCE_SYSCLK;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/* ADC1 clock enable */
|
|
__HAL_RCC_ADC_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
/**ADC1 GPIO Configuration
|
|
PC0 ------> ADC1_IN1
|
|
*/
|
|
GPIO_InitStruct.Pin = ADC1_CH1_Pin;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG_ADC_CONTROL;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
HAL_GPIO_Init(ADC1_CH1_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
/* ADC1 DMA Init */
|
|
/* ADC1 Init */
|
|
hdma_adc1.Instance = DMA1_Channel1;
|
|
hdma_adc1.Init.Request = DMA_REQUEST_0;
|
|
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
|
|
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
|
|
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
|
|
hdma_adc1.Init.Mode = DMA_CIRCULAR;
|
|
hdma_adc1.Init.Priority = DMA_PRIORITY_HIGH;
|
|
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
|
|
|
|
/* USER CODE BEGIN ADC1_MspInit 1 */
|
|
|
|
/* USER CODE END ADC1_MspInit 1 */
|
|
}
|
|
}
|
|
|
|
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
|
{
|
|
|
|
if(adcHandle->Instance==ADC1)
|
|
{
|
|
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
|
|
|
/* USER CODE END ADC1_MspDeInit 0 */
|
|
/* Peripheral clock disable */
|
|
__HAL_RCC_ADC_CLK_DISABLE();
|
|
|
|
/**ADC1 GPIO Configuration
|
|
PC0 ------> ADC1_IN1
|
|
*/
|
|
HAL_GPIO_DeInit(ADC1_CH1_GPIO_Port, GPIO_PIN_0|GPIO_PIN_1);
|
|
|
|
/* ADC1 DMA DeInit */
|
|
HAL_DMA_DeInit(adcHandle->DMA_Handle);
|
|
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
|
|
|
/* USER CODE END ADC1_MspDeInit 1 */
|
|
}
|
|
}
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
/* USER CODE END 1 */
|