//-----------------------------------------------------------------// // Setup ADC // ADC_CLK = 4 MHz // ADC single conversion time is ADC_StartDelay + // (29 to 36 ADC clock cycles depending on ADC_DelayGo) // ADC clock = 4MHz / 128 = 31.250kHz (T = 32uS) //-----------------------------------------------------------------// void HW_ADCInit(void) { ADC_InitTypeDef sADC; ADCx_InitTypeDef sADCx; // ADC Configuration // Reset all ADC settings ADC_DeInit(); ADC_StructInit(&sADC); sADC.ADC_SynchronousMode = ADC_SyncMode_Independent; sADC.ADC_StartDelay = 10; sADC.ADC_TempSensor = ADC_TEMP_SENSOR_Enable; sADC.ADC_TempSensorAmplifier = ADC_TEMP_SENSOR_AMPLIFIER_Enable; sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable; sADC.ADC_IntVRefConversion = ADC_VREF_CONVERSION_Enable; sADC.ADC_IntVRefTrimming = 1; ADC_Init (&sADC); // ADC1 Configuration ADCx_StructInit (&sADCx); sADCx.ADC_ClockSource = ADC_CLOCK_SOURCE_ADC; sADCx.ADC_SamplingMode = ADC_SAMPLING_MODE_SINGLE_CONV; sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable; sADCx.ADC_ChannelNumber = ADC_CH_TEMP_SENSOR; sADCx.ADC_Channels = 0; sADCx.ADC_LevelControl = ADC_LEVEL_CONTROL_Disable; sADCx.ADC_LowLevel = 0; sADCx.ADC_HighLevel = 0; sADCx.ADC_VRefSource = ADC_VREF_SOURCE_EXTERNAL; sADCx.ADC_IntVRefSource = ADC_INT_VREF_SOURCE_EXACT; sADCx.ADC_Prescaler = ADC_CLK_div_128; sADCx.ADC_DelayGo = 0; // CHECKME ADC1_Init (&sADCx); ADC2_Init (&sADCx); // Disable ADC interupts ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), DISABLE); ADC2_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), DISABLE); // ADC1 enable ADC1_Cmd (ENABLE); ADC2_Cmd (ENABLE); //-------------------// /* ADC1_SetChannel(ADC_CH_TEMP_SENSOR); ADC1_Start(); while( ADC_GetFlagStatus(ADC1_FLAG_END_OF_CONVERSION)==RESET ); temp_adc = ADC1_GetResult(); ADC1_Start(); while( ADC_GetFlagStatus(ADC1_FLAG_END_OF_CONVERSION)==RESET ); temp_adc = ADC1_GetResult(); temp_adc = temp_adc; */ }
/**************************实现函数******************************************** *函数原型: 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); }