RCC初始函数中
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4);
void AdcInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_DeInit(ADC1);
// RA0 - analog input
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = (GPIOSpeed_TypeDef)0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init (GPIOA, &GPIO_InitStructure);
// ADC Structure Initialization
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
// Enable the ADC
ADC_Cmd(ADC1, ENABLE);
// ADC calibration
// Enable ADC1 reset calibration register
ADC_ResetCalibration(ADC1);
// Check the end of ADC1 reset calibration register
while(ADC_GetResetCalibrationStatus(ADC1) == SET);
// Start ADC1 calibration
ADC_StartCalibration(ADC1);
// Check the end of ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1) == SET);
// Configure channel
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 1, ADC_SampleTime_55Cycles5);
// ADC_ITConfig(ADC1,ADC_IT_EOC,ENABLE);
// Start the conversion
// ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
main(void)
{
...
if (g_1s==1)//1s时间到
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while ((ADC1->SR&ADC_FLAG_EOC)==0);
g_u16ADC=ADC_GetConversionValue(ADC1);
}
...
} |