初始化ADC1:
void Init_ADC(void)
{
GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)(GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_6), GPIO_MODE_IN_FL_NO_IT);
ADC1_DeInit();
ADC1_Cmd(ENABLE);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D18, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3,DISABLE);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_4, ADC1_PRESSEL_FCPU_D18, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4, DISABLE);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_5, ADC1_PRESSEL_FCPU_D18, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL5,DISABLE);
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D18, ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6,DISABLE);
ADC1_ScanModeCmd(ENABLE);
ADC1_DataBufferCmd(ENABLE);
ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE);//在中断中获取AD值
ADC1_StartConversion();
}
中断:
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
uint32_t tmp;
ADCValue[0]=ADC1_GetBufferValue(3);
ADCValue[1]=ADC1_GetBufferValue(4);
ADCValue[2]=ADC1_GetBufferValue(5);
ADCValue[3]=ADC1_GetBufferValue(6);
ADC1_ClearITPendingBit(ADC1_IT_EOC);
ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);//Clear the ADC1 channels
ADC1->CSR |= (uint8_t)(ADC1_CHANNEL_6); //在此处会在其它带AD功能的输出脚产生尖峰干扰,如PC4
tmp=ADCValue[0]+ADCValue[1]+ADCValue[2]+ADCValue[3];
}
由于PC4我已经配置为TIM1的PWM输出,无论是否配置PC4的GPIO与否,这个脚都是输出状态。查了有人说在切换时先关掉ADC,实测不行,那个只适用于单次采集。连续的得不到数据。估计是ADC开启后需要较多时间恢复电平,但是在中断中不可能给很多时间进行等待,实际应用也不允许。
最后的解决办法:
将PWM的时基调低,分配从不分配设置到32分频,这样PWM的输出频率就降低到越1khz左右,在该脚旁路一个0.1的电容,将那个干扰脉冲吸收,再输出去基本符合要求了。唯一美中不足的是,当PWM的定时值过低时,宽度与那个脉冲的宽度相近的时候,他也被吸收了。但是基本不影响使用,算是解决了罢。
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』