#include "main.h" void ADCInit(void) { PORT_Init(PORTA, PIN11, PORTA_PIN11_ADC0_IN5, 0); //PA.11 => ADC.CH5 } uint16_t ADCRead(uint8_t ch) { ADC_InitStructure ADC_initStruct; PORT_Init(PORTA, PIN11, PORTA_PIN11_ADC0_IN5, 0); //PA.11 => ADC.CH5 ADC_initStruct.clk_src = ADC_CLKSRC_HRC; ADC_initStruct.clk_div = 20; ADC_initStruct.pga_ref = PGA_REF_INTERNAL; ADC_initStruct.channels = 1 << ch; ADC_initStruct.samplAvg = ADC_AVG_SAMPLE1; ADC_initStruct.trig_src = ADC_TRIGSRC_SW; ADC_initStruct.Continue = 0; //非连续模式,即单次模式 ADC_initStruct.EOC_IEn = 0; ADC_initStruct.OVF_IEn = 0; ADC_initStruct.HFULL_IEn = 0; ADC_initStruct.FULL_IEn = 0; ADC_Init(ADC0, &ADC_initStruct); //配置ADC ADC_Open(ADC0); //使能ADC ADC_Start(ADC0); while((ADC0->CH[ch].STAT & ADC_STAT_EOC_Msk) == 0) { DelayMs(1); } ADC0->CH[ch].STAT = (1 << ADC_STAT_EOC_Pos); uint16_t res = ADC_Read(ADC0, 1 << ch); // printf("ADC%d=%04d\t", ch,res); return res; }