GD32107C-EVAL的内部资源灰常滴丰富啊,这个ADC资源用处也非常广泛。看到板子上有个电位器就总是手痒去扭扭,那干脆做个实验来感受一下。
PC3是ADC的13通道。采用连续转换模式。这次实验作为初步体验ADC,下一个实验准备结合通道16来测试温度,并用上DMA功能。
void AdcInit() { GPIO_InitTypeDef GPIO_InitStruct; ADC_InitTypeDef ADC_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; RCC_APB2PeriphClockCmd (RCC_APB2Periph_ADC1, ENABLE); RCC_ADCCLKConfig (RCC_PCLK2_Div6); RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOC,ENABLE); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN ; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz ; GPIO_Init (GPIOC,&GPIO_InitStruct); ADC_InitStruct.ADC_ContinuousConvMode = ENABLE ; ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStruct.ADC_Mode = ADC_Mode_Independent; ADC_InitStruct.ADC_NbrOfChannel = 1; ADC_InitStruct.ADC_ScanConvMode = ENABLE ; ADC_Init (ADC1,&ADC_InitStruct); ADC_Cmd (ADC1,ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1) == SET) { ; } ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) == SET) { ; } ADC_RegularChannelConfig(ADC1,ADC_Channel_13,1,ADC_SampleTime_239Cycles5); ADC_ITConfig (ADC1,ADC_IT_EOC,ENABLE); NVIC_InitStruct.NVIC_IRQChannel = ADC1_2_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2; NVIC_Init(&NVIC_InitStruct); ADC_SoftwareStartConvCmd (ADC1,ENABLE); }
ADC初始化配置了PC3的模式为模拟量输入,并配置了ADC1,转换结束后触发中断,在中断服务函数中读取AD值。
void ADC1_2_IRQHandler() { if(ADC_GetITStatus(ADC1,ADC_IT_EOC) == SET) { gAdcVal = ADC_GetConversionValue(ADC1); ADC_ClearITPendingBit (ADC1,ADC_IT_EOC); } }
结合DMA后应该会有更好的效果,这部分的工程在调试完整后共享在导航帖中。