STM32F103 HAL ADC DMA
时间: 2025-05-22 15:08:44 浏览: 16
### STM32F103 HAL ADC DMA Example Code and Configuration Tutorial
For configuring the STM32F103 to use ADC with DMA through the HAL library, initialization of both peripherals is necessary along with proper configuration settings. The following sections provide a detailed guide on how this can be achieved.
#### Initialization of Peripherals
The first step involves initializing the ADC peripheral using `HAL_ADC_Init()`. This function initializes the ADC according to specified parameters in the `ADC_HandleTypeDef` structure[^1]. Additionally, it's important that GPIO pins connected to ADC channels are configured correctly as analog inputs before starting any conversion process.
```c
// Define ADC handle structure
ADC_HandleTypeDef hadc;
void MX_ADC_Init(void)
{
// Configure ADC channel(s), resolution, etc.
hadc.Instance = ADC1;
ADC_ChannelConfTypeDef sConfig = {0};
hadc.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set by 'Channel' below) */
hadc.Init.ContinuousConvMode= ENABLE; /* Continuous mode enabled (single shot mode disabled): continuous conversions after each trigger event */
hadc.Init.DiscontinuousConvMode= DISABLE; /* Parameter discarded because sequencer is disabled */
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external signal */
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment of converted data */
hadc.Init.NbrOfConversion = 1; /* Parameter discarded because sequencer is disabled */
if(HAL_OK != HAL_ADC_Init(&hadc))
Error_Handler();
// Channel configuration for regular group
sConfig.Channel = ADC_CHANNEL_0; /* Sampled channel number */
sConfig.Rank = 1; /* Rank of sampled channel within sequencer */
sConfig.SamplingTime= ADC_SAMPLETIME_7CYCLES_5; /* Sampling time value selected from available options defined in stm32f1xx_hal_adc.h file */
if(HAL_OK != HAL_ADC_ConfigChannel(&hadc,&sConfig))
Error_Handler();
}
```
#### Configuring DMA Stream
After setting up the ADC, configure the DMA stream responsible for transferring data between memory and peripheral automatically during an ADC conversion operation. Use functions like `HAL_DMA_Init()` followed by linking the initialized DMA handle (`DMA_HandleTypeDef`) with the corresponding ADC handle via `__HAL_LINKDMA()` macro[^2].
```c
// Define DMA handle structure
DMA_HandleTypeDef hdma_adc;
void MX_DMA_Init(void)
{
__HAL_RCC_DMA1_CLK_ENABLE(); /* Enable clock for DMA controller */
hdma_adc.Instance = DMA1_Channel1; /* Select DMA channel used for ADC transfers */
hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;/* Direction of transfer */
hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE; /* Peripheral base address does not increment */
hdma_adc.Init.MemInc = DMA_MINC_ENABLE; /* Memory destination address increments */
hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;/* Data alignment matches ADC output format */
hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;/* Same here */
hdma_adc.Init.Mode = DMA_CIRCULAR; /* Circular buffer mode enables automatic reload when end-of-transfer occurs */
hdma_adc.Init.Priority = DMA_PRIORITY_HIGH; /* Priority level assigned to DMA request */
if(HAL_OK != HAL_DMA_Init(&hdma_adc))
Error_Handler();
__HAL_LINKDMA(&hadc,DMA_Handle,hdma_adc); /* Link DMA handler to ADC instance */
}
static void SystemClock_Config(void);
static void Error_Handler(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
uint16_t adcValue[1]; /* Buffer where received values will be stored */
__HAL_DMA_CLEAR_FLAG(&hdma_adc,DMA_FLAG_TCIF1|DMA_FLAG_HTIF1|DMA_FLAG_TEIF1);/* Clear all flags prior to enabling DMA */
HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 1); /* Set interrupt priority */
HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); /* Enable interrupts associated with chosen DMA channel */
HAL_ADC_Start_DMA(&hadc,(uint32_t*)adcValue,sizeof(adcValue)/sizeof(uint16_t));/* Start ADC
阅读全文
相关推荐


















