/** ****************************************************************************** * @file ADC/ADC1_DMA/main.c * @author MCD Application Team * @version V3.5.0 * @date 08-April-2011 * @brief Main program body ****************************************************************************** * @attention * * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. * * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x.h" /** @addtogroup STM32F10x_StdPeriph_Examples * @{ */ /** @addtogroup ADC_ADC1_DMA * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define ADC1_DR_Address ((uint32_t)0x4001244C) /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; __IO uint16_t ADCConvertedValue; /* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); /* Private functions ---------------------------------------------------------*/ /** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); /* DMA1 channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); while (1) { } } /** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) /* ADCCLK = PCLK2/2 */ RCC_ADCCLKConfig(RCC_PCLK2_Div2); #else /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); #endif /* Enable peripheral clocks ------------------------------------------------*/ /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable ADC1 and GPIOC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE); } /** * @brief Configures the different GPIO ports. * @param None * @retval None */ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); } #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 can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /** * @} */ /** * @} */ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 在每一行加注释
时间: 2025-07-09 20:13:19 浏览: 5
```c
// 包含头文件,包含STM32标准外设驱动函数和宏定义
#include "stm32f4xx.h"
void ADC_DMA_Config(void) {
// 定义ADC和DMA的句柄结构体指针
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
// 使能ADC1和DMA2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
// 配置ADC1的GPIO为模拟输入模式
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; // 假设使用PA0作为ADC输入通道
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN; // 设置为模拟模式
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // 不启用上拉/下拉电阻
GPIO_Init(GPIOA, &GPIO_InitStruct); // 初始化GPIOA
// 配置DMA2通道0流0用于ADC1数据传输
DMA_InitStruct.DMA_Channel = DMA_Channel_0; // 选择DMA通道0
DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; // 外设地址为ADC1的数据寄存器
DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)&adc_buffer; // 内存地址指向adc_buffer数组
DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; // 数据方向:从外设到内存
DMA_InitStruct.DMA_BufferSize = BUFFER_SIZE; // 设置缓冲区大小
DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址不递增
DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable; // 内存地址递增
DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 外设数据宽度为半字(16位)
DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 内存数据宽度为半字(16位)
DMA_InitStruct.DMA_Mode = DMA_Mode_Normal; // 使用普通模式
DMA_InitStruct.DMA_Priority = DMA_Priority_High; // 设置DMA优先级为高
DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable; // 禁用FIFO模式
DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; // FIFO阈值设置为半满
DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single; // 单次内存突发传输
DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; // 单次外设突发传输
DMA_Init(DMA2_Stream0, &DMA_InitStruct); // 初始化DMA2流0
// 使能ADC1的DMA请求
ADC_DMACmd(ADC1, ENABLE); // 启用ADC1的DMA功能[^2]
// 配置ADC1参数
ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b; // 设置ADC分辨率为12位
ADC_InitStruct.ADC_ScanConvMode = DISABLE; // 禁用扫描模式(单通道采集)
ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; // 禁用连续转换模式
ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // 不使用外部触发
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐
ADC_InitStruct.ADC_NbrOfConversion = 1; // 只转换一个通道
ADC_Init(ADC1, &ADC_InitStruct); // 初始化ADC1
// 选择ADC1的通道0,并设置采样时间
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_3Cycles); // 配置通道0为规则组第1个转换通道,采样时间为3个周期
// 开始ADC1校准
ADC_StartCalibration(ADC1); // 启动ADC1校准过程[^2]
while(ADC_GetCalibrationStatus(ADC1)); // 等待校准完成
// 启动ADC1转换
ADC_Cmd(ADC1, ENABLE); // 使能ADC1
// 启动DMA传输
DMA_Cmd(DMA2_Stream0, ENABLE); // 启动DMA2流0
}
void Start_ADC_Conversion(void) {
// 软件触发一次ADC转换
ADC_SoftwareStartConv(ADC1); // 触发ADC1开始一次软件转换
}
```
阅读全文
相关推荐

















