STM32F405的内部ADC采集
1. ADC的初始化部分基本一致,下面是引脚复用配置
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc) { GPIO_InitTypeDef GPIO_InitStruct; // static DMA_HandleTypeDef hdma_adc; /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* ADC1 Periph clock enable */ ADCx_CLK_ENABLE(); /* Enable GPIO clock ****************************************/ ADCx_CHANNEL_GPIO_CLK_ENABLE(); /* Enable DMA2 clock */ // DMAx_CLK_ENABLE(); /*##-2- Configure peripheral GPIO ##########################################*/ /* ADC Channel GPIO pin configuration */ GPIO_InitStruct.Pin = ADCx_CHANNEL_PIN; GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(ADCx_CHANNEL_GPIO_PORT, &GPIO_InitStruct);
}
2. 下面是功能初始化
void adc_ac712_init(void)
{
ADC_ChannelConfTypeDef sConfig;
/*##-1- Configure the ADC peripheral #######################################*/
AdcHandle.Instance = ADCx;
AdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
AdcHandle.Init.ContinuousConvMode = ENABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */
AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
AdcHandle.Init.NbrOfDiscConversion = 0;
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Conversion start trigged at each external event */
AdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
AdcHandle.Init.NbrOfConversion = 1;
AdcHandle.Init.DMAContinuousRequests = DISABLE;
AdcHandle.Init.EOCSelection = DISABLE;
if (HAL_ADC_Init(&AdcHandle) != HAL_OK)
{
/* ADC initialization Error */
//Error_Handler();
}
/*##-2- Configure ADC regular channel ######################################*/
sConfig.Channel = ADC_CHANNEL_1;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&AdcHandle, &sConfig) != HAL_OK)
{
/* Channel Configuration Error */
//Error_Handler();
}
/*##-3- Start the conversion process #######################################*/
if (HAL_ADC_Start(&AdcHandle) != HAL_OK)
{
/* Start Conversation Error */
//Error_Handler();
}
}
3. ADC的数据采集有3中方式,中断,DMA,正常查询模式,下面是正常查询模式的代码
HAL_ADC_PollForConversion(&AdcHandle, 10);
//if(HAL_ADC_GetState(&AdcHandle) == HAL_ADC_STATE_EOC_REG)
if((HAL_ADC_GetState(&AdcHandle) & HAL_ADC_STATE_EOC_REG) == HAL_ADC_STATE_EOC_REG)
{
/*##-5- Get the converted value of channel ##*/
uhADCxConvertedValue = HAL_ADC_GetValue(&AdcHandle);
}