/** * @brief Initializes the ADC2 peripheral according to the specified parameters * @param ADC2_ConversionMode: specifies the conversion mode * can be one of the values of @ref ADC2_ConvMode_TypeDef. * @param ADC2_Channel: specifies the channel to convert * can be one of the values of @ref ADC2_Channel_TypeDef. * @param ADC2_PrescalerSelection: specifies the ADC2 prescaler * can be one of the values of @ref ADC2_PresSel_TypeDef. * @param ADC2_ExtTrigger: specifies the external trigger * can be one of the values of @ref ADC2_ExtTrig_TypeDef. * @param ADC2_ExtTriggerState: specifies the external trigger new state * can be one of the values of @ref FunctionalState. * @param ADC2_Align: specifies the converted data alignment * can be one of the values of @ref ADC2_Align_TypeDef. * @param ADC2_SchmittTriggerChannel: specifies the schmitt trigger channel * can be one of the values of @ref ADC2_SchmittTrigg_TypeDef. * @param ADC2_SchmittTriggerState: specifies the schmitt trigger state * can be one of the values of @ref FunctionalState. * @retval None */ void ADC2_Init(ADC2_ConvMode_TypeDef ADC2_ConversionMode, ADC2_Channel_TypeDef ADC2_Channel, ADC2_PresSel_TypeDef ADC2_PrescalerSelection, ADC2_ExtTrig_TypeDef ADC2_ExtTrigger, FunctionalState ADC2_ExtTriggerState, ADC2_Align_TypeDef ADC2_Align, ADC2_SchmittTrigg_TypeDef ADC2_SchmittTriggerChannel, FunctionalState ADC2_SchmittTriggerState) { /* Check the parameters */ assert_param(IS_ADC2_CONVERSIONMODE_OK(ADC2_ConversionMode)); assert_param(IS_ADC2_CHANNEL_OK(ADC2_Channel)); assert_param(IS_ADC2_PRESSEL_OK(ADC2_PrescalerSelection)); assert_param(IS_ADC2_EXTTRIG_OK(ADC2_ExtTrigger)); assert_param(IS_FUNCTIONALSTATE_OK(((ADC2_ExtTriggerState)))); assert_param(IS_ADC2_ALIGN_OK(ADC2_Align)); assert_param(IS_ADC2_SCHMITTTRIG_OK(ADC2_SchmittTriggerChannel)); assert_param(IS_FUNCTIONALSTATE_OK(ADC2_SchmittTriggerState)); /*-----------------CR1 & CSR configuration --------------------*/ /* Configure the conversion mode and the channel to convert respectively according to ADC2_ConversionMode & ADC2_Channel values & ADC2_Align values */ ADC2_ConversionConfig(ADC2_ConversionMode, ADC2_Channel, ADC2_Align); /* Select the prescaler division factor according to ADC2_PrescalerSelection values */ ADC2_PrescalerConfig(ADC2_PrescalerSelection); /*-----------------CR2 configuration --------------------*/ /* Configure the external trigger state and event respectively according to ADC2_ExtTrigStatus, ADC2_ExtTrigger */ ADC2_ExternalTriggerConfig(ADC2_ExtTrigger, ADC2_ExtTriggerState); /*------------------TDR configuration ---------------------------*/ /* Configure the schmitt trigger channel and state respectively according to ADC2_SchmittTriggerChannel & ADC2_SchmittTriggerNewState values */ ADC2_SchmittTriggerConfig(ADC2_SchmittTriggerChannel, ADC2_SchmittTriggerState); /* Enable the ADC2 peripheral */ ADC2->CR1 |= ADC2_CR1_ADON; }
/**************************实现函数******************************************** *函数原型: unsigned int Get_ADCCH_Value(ADC2_Channel_TypeDef ADC_Channel) *功 能: 单次采样某个ADC通道 *******************************************************************************/ unsigned int Get_ADCCH_Value(ADC2_Channel_TypeDef ADC_Channel) { unsigned int SUM = 0;//临时和 unsigned char j; //配置ADC转换通道,单次采样模式,ADC结果右对齐 ADC2_Cmd(ENABLE); ADC2_ConversionConfig(ADC2_CONVERSIONMODE_SINGLE, ADC_Channel, ADC2_ALIGN_RIGHT); for (j = 0; j < 16 ; j++) { ADC2_StartConversion(); // 开始转换 while (ADC2_GetFlagStatus() == RESET); // 等待转换完成 SUM += ADC2_GetConversionValue(); // 求和 ADC2_ClearFlag(); } SUM = SUM>>4; ADC2_Cmd(DISABLE); return(SUM); }