本帖最后由 wu317417 于 2019-1-22 19:44 编辑
ADC 作为模拟量检测有很多应用场合,最近用来做一个电压检测。
比较麻烦的是网上好像没有STM32F303VE这方面的详细资料。只好自己研究做个简单的应用。
PC4,PC5作为输入检测脚。对应的ADC通道是PC4----ADC2_IN5,PC5----ADC2_IN11
直接来代码吧,有空再整理下:
void ADC_Initpc4_pc5(void){
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitSt;
GPIO_InitTypeDef GPIO_InitStructure;
//初始化始终和 GPIO
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12|RCC_AHBPeriph_GPIOC, ENABLE);
// RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC 时钟配置为 72M/8 */
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div8);
ADC_StructInit(&ADC_InitStructure);
/* Calibration procedure */
ADC_VoltageRegulatorCmd(ADC2, ENABLE);
ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC2);
while(ADC_GetCalibrationStatus(ADC2) != RESET);
ADC_GetCalibrationValue(ADC2);
ADC_CommonInitSt.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitSt.ADC_Clock = ADC_Clock_AsynClkMode;
ADC_CommonInitSt.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitSt.ADC_DMAMode = ADC_DMAMode_OneShot;
ADC_CommonInitSt.ADC_TwoSamplingDelay = 0;
ADC_CommonInit(ADC2, &ADC_CommonInitSt);
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
ADC_InitStructure.ADC_NbrOfRegChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
ADC_Cmd(ADC2, ENABLE);
/* wait for ADRDY */
while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));
}
|