分享一个STM32F030 多通道ADC采集并用DMA传输的例程,希望可以帮到都需要的TX。
#include "bsp_cpu_adc.h"
#include "usart.h"
/*
PA0 --10脚
PA4 --14脚
PA5 --15脚
PA6 --16脚
AN16-----内部温度传感器
AN17----内部参考电压
*/
__IO uint16_t ADC_ConvertedValue[NOFCHANEL]={0,0,0,0};
DMA_HandleTypeDef hdma_adcx;
ADC_HandleTypeDef ADC_Handle;
ADC_ChannelConfTypeDef ADC_Config;
static void Rheostat_ADC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 使能 GPIO 时钟
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_ADC1_CLK_ENABLE(); //开ADC时钟
// 配置 IO
GPIO_InitStructure.Pin = GPIO_PIN_0|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; //配置为模拟模式
GPIO_InitStructure.Pull = GPIO_NOPULL ; //不上拉不下拉
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static void Rheostat_ADC_Mode_Config(void)
{
// ------------------DMA Init 结构体参数 初始化--------------------------
//DMA_HandleTypeDef hdma_adcx;
// 开启DMA时钟
__HAL_RCC_DMA1_CLK_ENABLE();
// 数据传输通道
hdma_adcx.Instance = DMA1_Channel1; //通道1
hdma_adcx.Init.Direction=DMA_PERIPH_TO_MEMORY; //存储器到外设
hdma_adcx.Init.PeriphInc=DMA_PINC_DISABLE; //外设非增量模式
hdma_adcx.Init.MemInc=DMA_MINC_ENABLE; //存储器增量模式
hdma_adcx.Init.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD;//外设数据长度:16位
hdma_adcx.Init.MemDataAlignment=DMA_MDATAALIGN_HALFWORD; //存储器数据长度:16位
hdma_adcx.Init.Mode= DMA_CIRCULAR; //外设普通模式
hdma_adcx.Init.Priority=DMA_PRIORITY_MEDIUM; //中等优先级
HAL_DMA_Init(&hdma_adcx); //根据指定的参数初始化DMA
__HAL_LINKDMA( &ADC_Handle,DMA_Handle,hdma_adcx); //DMA句柄关联到
//---------------------------------------------------------------------------
// 开启ADC时钟
// __HAL_RCC_ADC1_CLK_ENABLE();
ADC_Handle.Instance = ADC1;
if (HAL_ADC_DeInit(&ADC_Handle) != HAL_OK)
{
/* ADC de-initialization Error */
// Error_Handler();
}
ADC_Handle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2; /* 同步时钟模式,带prscaler 2的输入ADC时钟 */
ADC_Handle.Init.Resolution = ADC_RESOLUTION_12B; /* //12位精度 */
ADC_Handle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* //右对齐 */
ADC_Handle.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
ADC_Handle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */
ADC_Handle.Init.LowPowerAutoPowerOff = DISABLE;
ADC_Handle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */
ADC_Handle.Init.ContinuousConvMode = ENABLE; /* Continuous mode enabled (automatic conversion restart after each conversion) */
ADC_Handle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
ADC_Handle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* 软件触发 */
ADC_Handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
ADC_Handle.Init.DMAContinuousRequests = ENABLE; /* ADC DMA continuous request to match with DMA circular mode */
ADC_Handle.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; /* DR register is overwritten with the last conversion result in case of overrun */
ADC_Handle.Init.SamplingTimeCommon = ADC_SAMPLETIME_55CYCLES_5;
/* Initialize ADC peripheral according to the passed parameters */
if (HAL_ADC_Init(&ADC_Handle) != HAL_OK) //初始化
{
//Error_Handler();
printf("初始化失A\r\n");
}
//
// /* ADC 校准 ################### */
if (HAL_ADCEx_Calibration_Start(&ADC_Handle) != HAL_OK)
{
//Error_Handler();
printf("校准失败\r\n");
}
//---------------------------------------------------------------------------
ADC_Config.Channel = ADC_CHANNEL_0;
ADC_Config.Rank = ADC_RANK_CHANNEL_NUMBER;
// 采样时间间隔
ADC_Config.SamplingTime = ADC_SAMPLETIME_55CYCLES_5 ;
if (HAL_ADC_ConfigChannel(&ADC_Handle, &ADC_Config) != HAL_OK)
{
//Error_Handler();
}
//---------------------------------------------------------------------------
ADC_Config.Channel = ADC_CHANNEL_4;
// ADC_Config.Rank = 1;
// 采样时间间隔
ADC_Config.SamplingTime = ADC_SAMPLETIME_55CYCLES_5 ;
HAL_ADC_ConfigChannel(&ADC_Handle, &ADC_Config);
//---------------------------------------------------------------------------
ADC_Config.Channel = ADC_CHANNEL_5;
// 采样时间间隔
ADC_Config.SamplingTime = ADC_SAMPLETIME_55CYCLES_5 ;
HAL_ADC_ConfigChannel(&ADC_Handle, &ADC_Config);
//---------------------------------------------------------------------------
ADC_Config.Channel = ADC_CHANNEL_6;
// 采样时间间隔
ADC_Config.SamplingTime = ADC_SAMPLETIME_55CYCLES_5 ;
HAL_ADC_ConfigChannel(&ADC_Handle, &ADC_Config);
//---------------------------------------------------------------------------
ADC_Config.Channel = ADC_CHANNEL_16;
// 采样时间间隔
ADC_Config.SamplingTime = ADC_SAMPLETIME_55CYCLES_5 ;
HAL_ADC_ConfigChannel(&ADC_Handle, &ADC_Config);
//---------------------------------------------------------------------------
ADC_Config.Channel = ADC_CHANNEL_17;
// 采样时间间隔
ADC_Config.SamplingTime = ADC_SAMPLETIME_55CYCLES_5 ;
HAL_ADC_ConfigChannel(&ADC_Handle, &ADC_Config);
/* ### - 4 - Start conversion in DMA mode ################################# */
if (HAL_ADC_Start_DMA(&ADC_Handle,
(uint32_t *)ADC_ConvertedValue,
NOFCHANEL
) != HAL_OK)
{
while(1);
}
代码下载:
FreeRTOS_多通道ADC-DMA _HAL.7z
(680.76 KB, 下载次数: 162)
|