每次转换,结果会转到下一次转换通道的。第一次转换的结果到了第二次里的。
u8 Init_ADC(void)
{
//if(Adc_Init_flag==0)Adc_Init_flag=1;else return 0;
u8 i;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
//ADC_RegularChannelConfig(ADC1,3, (3+1), ADC_SampleTime_15Cycles);
ADC1->SMPR1=SampleTime_15Cycles&0x00FFFFFF;
ADC1->SMPR2=SampleTime_15Cycles;
ADC_Cmd(ADC1, ENABLE);
ADC_SoftwareStartConv(ADC1);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
}
u16 GetAdcResult(u8 ch)
{
u16 temp;
//设置转换序列
//ADC1->SQR3&=0XFFFFFFE0;//规则序列1 通道ch
//ADC1->SQR3|=ch;
//设置指定ADC的规则组通道,设置它们的转化顺序和采样时间
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_15Cycles);//ADC1,ADC通道3,规则采样顺序值为1,采样时间为239.5周期
//ADC1->CR2|=1<<22; //启动规则转换通道
// Start ADC1 Software Conversion//
ADC_SoftwareStartConv(ADC1); //使能指定的ADC1的软件转换启动功能
//while(!(ADC1->SR&1<<1));//等待转换结束
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
temp = ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
return temp;
}
|