编译环境IAR ,CPU STM8S903K ,通道PB0 -- AN0, 采集交流信号
void ADC_Init(void)
{
GPIO_Init(GPIOB, GPIO_PIN_0, GPIO_MODE_IN_PU_NO_IT);
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_0,
ADC1_PRESSEL_FCPU_D12, ADC1_EXTTRIG_TIM, DISABLE,
ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL0, DISABLE);
/* Enable the ADC1 peripheral */
ADC1_Cmd(ENABLE);
/* Enable EOC interrupt */
#ifdef __ADCInt_
ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE); //ADC 中断
#endif
}
#define __ADCInt_
main()
{
ADC_Init();
while(1)
{
#ifndef __ADCInt_
Delay_us(Time);
ADC1_StartConversion();
while((ADC1->CSR & 0x80)!=0x80); // 等待转换结束
ADBuf[ADC_Index].Uac = ADC1_GetConversionValue();
ADC_Index++;
#endif
}
}
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
{
ADC1_StartConversion(); /*Start Conversion */
TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
return;
}
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{
static vu8 ADC_Index;
ADBuf[ADC_Index].Uac = ADC1_GetConversionValue();
ADC_Index++;
ADC1_ClearITPendingBit(ADC1_IT_EOC);
}
当定义__ADCInt_ 则AN0为单次中断采样模式,在中断中读取值,TIM1定时开启AN0;
没有定义__ADCInt_时,每延时Delay_us(Time),在主程序里进行一次采样;
在主程序里采样读值时是对的,但在中断了读值却是一个稳定的值,我这是247,不知为什么……求解
|