本帖最后由 megaf 于 2011-9-21 14:46 编辑
void ADC_HardInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC|RCC_AHBPeriph_DMA1, ENABLE);
//DMA_Configuration();
/* Configure PC.0 (ADC Channel 12) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel12 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
// ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
u8 ADC_GetChannel(u8 channel, u16* value)
{
switch (channel)
{
case 0x12:
channel = ADC_Channel_12;
break;
case 0x16:
channel = ADC_Channel_16;
ADC_TempSensorVrefintCmd(ENABLE);
break;
default:
channel = ADC_Channel_16;
break;
}
/* ADC1 regular channel12 configuration */
ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_55Cycles5);
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
ADC_Delay(2);
if(IS_ADC_GET_FLAG(ADC_FLAG_EOC))
{
*value = ADC_GetConversionValue(ADC1);
return 0;
}
return 1;
}
u16 ad_value;
void BatteryCtl(void)
{
float out,voltage;
u16 out2;
ADC_GetChannel(0x12,&ad_value);
printf("\r\n ad_value %d",ad_value);
ADC_GetChannel(0x16,&ad_value);
printf("\r\n TempSensorVrefintCmd %d",ad_value);
}
源代码如上,只测一个pc0--in12测得结果老是4095,但是用表测量电压时2.2v,我把pco接地测得结果就是0.郁闷中..... |