代码如下:
void ADC_Init(void)
{
P1->OFFD = (GPIO_OFFD_ENABLE(4)|GPIO_OFFD_ENABLE(5)|GPIO_OFFD_ENABLE(6)|GPIO_OFFD_ENABLE(7));//Disable digital input path to avoid the leakage current
SYS->P1_MFP = SYS_MFP_P14_AIN4 | SYS_MFP_P15_AIN5 | SYS_MFP_P16_AIN6 | SYS_MFP_P17_AIN7 ;//Configure ADC analog input pins
P1->PMD = 0;
// Set the ADC operation mode as single, input mode as single-end and enable the ADC converter
ADC->ADCR = (ADC_ADCR_ADMD_SINGLE_CYCLE | ADC_ADCR_DIFFEN_SINGLE_END | ADC_ADCR_ADEN_CONVERTER_ENABLE | ADC_ADCR_TRGEN_DISABLE);//
// Enable analog input channel 0 ~ 3
ADC->ADCHER = 0xF0;
// clear the A/D interrupt flag for safe
ADC->ADSR = ADC_ADSR_ADF_Msk;
// Enable the ADC interrupt
_ADC_ENABLE_ADC_INT();
NVIC_EnableIRQ(ADC_IRQn);
}
INT16U wAdcResut = 0;
/*---------------------------------------------------------------------------------------------------------*/
/* ADC interrupt handler */
/*---------------------------------------------------------------------------------------------------------*/
void ADC_IRQHandler(void)
{
if(_ADC_IS_DATA_VALID(4)) // Check the VALID bits
{
wAdcResut = (uint16_t)_ADC_GET_CONVERSION_DATA(4);
printf("Conversion result0: 0x%X\n", wAdcResut);
}
if(_ADC_IS_DATA_VALID(5)) // Check the VALID bits
{
wAdcResut = (uint16_t)_ADC_GET_CONVERSION_DATA(5);
printf("Conversion result1: 0x%X\n", wAdcResut);
}
if(_ADC_IS_DATA_VALID(6)) // Check the VALID bits
{
wAdcResut = (uint16_t)_ADC_GET_CONVERSION_DATA(6);
printf("Conversion result2: 0x%X\n", wAdcResut);
}
if(_ADC_IS_DATA_VALID(7)) // Check the VALID bits
{
wAdcResut = (uint16_t)_ADC_GET_CONVERSION_DATA(7);
printf("Conversion result3: 0x%X\n", wAdcResut);
}
ADC->ADSR = ADC_ADSR_ADF_Msk; // clear the A/D conversion flag
}
在中断中读取转换的4个结果,全是0xFFF,AIN5我接入的是一个2.5V的电压
麻烦大家看一下设置有没有问题? |