static void MX_ADC2_Init(void)
{
ADC_ChannelConfTypeDef sConfig;
/**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc2.Instance = ADC2; //配置ADC实例
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;//四分频 ,需要查文档在配置
hadc2.Init.Resolution = ADC_RESOLUTION_12B; //分辨率
//多通道转换 开启这个
hadc2.Init.ScanConvMode = ENABLE;//关闭扫描模式,只采集一个通道。扫描模式:多个通道同时采集
hadc2.Init.ContinuousConvMode = ENABLE;//连续采集模式,DISABLE :采集一次ADC就关闭了
hadc2.Init.DiscontinuousConvMode = DISABLE;//是否转换规则通道序列组,扫描模式下无效
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;//禁止外部边沿触发模式
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;//软触发模式
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;//数据右对齐
//配个多通道个数
hadc2.Init.NbrOfConversion = 3;//使用到的通道个数
//使用DMA
hadc2.Init.DMAContinuousRequests = ENABLE;//使用DMA模式传递数据
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;//是否开启传输标志位
if (HAL_ADC_Init(&hadc2) != HAL_OK)//初始化结构体
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
//配置多个通道
sConfig.Channel = ADC_CHANNEL_13;//配置通道13
sConfig.Rank = 1;//转换等级
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;//采样时间
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)//初始化通道结构体
{
_Error_Handler(__FILE__, __LINE__);
}
sConfig.Channel = ADC_CHANNEL_4;//配置通道13
sConfig.Rank = 2;//转换等级
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;//采样时间
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)//初始化通道结构体
{
_Error_Handler(__FILE__, __LINE__);
}
sConfig.Channel = ADC_CHANNEL_6;//配置通道13
sConfig.Rank = 3;//转换等级
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;//采样时间
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)//初始化通道结构体
{
_Error_Handler(__FILE__, __LINE__);
}
//HAL_ADC_Start_IT(&hadc2);//开启中断
}
static void DMA_Config(void)
{
/*## -1- Enable DMA2 clock #################################################*/
__HAL_RCC_DMA2_CLK_ENABLE();//开启时钟
/*##-2- Select the DMA functional Parameters ###############################*/
DmaHandle.Init.Channel = DMA_CHANNEL_1; /* DMA_CHANNEL_1 查数据手册 */
DmaHandle.Init.Direction = DMA_PERIPH_TO_MEMORY; /* 数据传输方向 */
DmaHandle.Init.PeriphInc = DMA_PINC_DISABLE; /* 只有一个外设 不用地址增量 */
//地址是数组 需要自增
DmaHandle.Init.MemInc = DMA_MINC_ENABLE; /* 地址固定 */
DmaHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; /* 外设数据大小为半个字 2个字节 */
DmaHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; /* 数据大小为半个字 2个字节 */
DmaHandle.Init.Mode = DMA_CIRCULAR; /* 循环模式 */
DmaHandle.Init.Priority = DMA_PRIORITY_HIGH; /* priority level : high */
DmaHandle.Init.FIFOMode = DMA_FIFOMODE_DISABLE; /* 直接传送模式 不使用FIFO */
DmaHandle.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL; //半字突发 , 与数据大小一致
DmaHandle.Init.MemBurst = DMA_MBURST_SINGLE; /* Memory burst */
DmaHandle.Init.PeriphBurst = DMA_PBURST_SINGLE; /* Peripheral burst */
/*##-3- Select the DMA instance to be used for the transfer : DMA2_Stream0 #*/
DmaHandle.Instance = DMA2_Stream3;
/*##-4- Select Callbacks functions called after Transfer complete and Transfer error */
// //DmaHandle.StreamBaseAddress = ((uint32_t)ADC2+OX4C);//ADC外设基址
// DmaHandle.XferCpltCallback = TransferComplete;
// DmaHandle.XferErrorCallback = TransferError;
//
__HAL_LINKDMA(&hadc2,DMA_Handle,DmaHandle);
// DmaHandle.Instance->PAR = ((uint32_t)ADC2 + 0X4c);//外设地址
// DmaHandle.Instance->M0AR = (uint32_t)ADC_DMA_ConvertedValue;//内存缓冲地址
// DmaHandle.Instance->NDTR =2;//数据长度
/*##-5- Initialize the DMA stream ##########################################*/
if(HAL_DMA_Init(&DmaHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
/*##-6- Configure NVIC for DMA transfer complete/error interrupts ##########*/
// HAL_NVIC_SetPriority(DMA_STREAM_IRQ, 0, 0);
// HAL_NVIC_EnableIRQ(DMA_STREAM_IRQ);
/*##-7- Start the DMA transfer using the interrupt mode ####################*/
/* Configure the source, destination and buffer size DMA fields and Start DMA Stream transfer */
/* Enable All the DMA interrupts */
//参数3 直接给数组ADC_DMA_ConvertedValue[3]的地址
if(HAL_DMA_Start_IT(&DmaHandle, (uint32_t)&ADC2->DR,(uint32_t)ADC_DMA_ConvertedValue,3) != HAL_OK)
{
/* Transfer error */
Error_Handler();
}
}
main.c
printf("test \r\n");
HAL_ADC_Start(&hadc2); //开启ADC
HAL_ADC_Start_DMA(&hadc2,(uint32_t*)&ADC_DMA_ConvertedValue,2);
//HAL_ADC_Start(&hadc2);//开始采集
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(500);
//注意这里声明的ADC_DMA_ConvertedValue类型为uint16_t
printf("values is %d \r\n",ADC_DMA_ConvertedValue[0]);
printf("values is %d \r\n",ADC_DMA_ConvertedValue[1]);
printf("values is %d \r\n",ADC_DMA_ConvertedValue[2]);
printf("===================== \r\n");
}
/*