- 以16脚和29脚ADC为例
static void *g_ADCHandle;
const am_hal_gpio_pincfg_t g_AM_PIN_16_ADCSE0 =
{
.uFuncSel = AM_HAL_PIN_16_ADCSE0,
};
const am_hal_gpio_pincfg_t g_AM_PIN_29_ADCSE1 =
{
.uFuncSel = AM_HAL_PIN_29_ADCSE1,
};
void adc_config(void)
{
am_hal_adc_config_t ADCConfig;
am_hal_adc_slot_config_t ADCSlotConfig;
// Initialize the ADC and get the handle.
if ( AM_HAL_STATUS_SUCCESS != am_hal_adc_initialize(0, &g_ADCHandle) )
{
am_util_stdio_printf("Error - reservation of the ADC instance failed.\n");
}
// Power on the ADC.
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_power_control(g_ADCHandle,
AM_HAL_SYSCTRL_WAKE,
false) )
{
am_util_stdio_printf("Error - ADC power on failed.\n");
}
// Set up the ADC configuration parameters. These settings are reasonable
// for accurate measurements at a low sample rate.
ADCConfig.eClock = AM_HAL_ADC_CLKSEL_HFRC;
ADCConfig.ePolarity = AM_HAL_ADC_TRIGPOL_RISING;
ADCConfig.eTrigger = AM_HAL_ADC_TRIGSEL_SOFTWARE;
ADCConfig.eReference = AM_HAL_ADC_REFSEL_INT_2P0;
ADCConfig.eClockMode = AM_HAL_ADC_CLKMODE_LOW_POWER;
ADCConfig.ePowerMode = AM_HAL_ADC_LPMODE0;
ADCConfig.eRepeat = AM_HAL_ADC_REPEATING_SCAN;
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_configure(g_ADCHandle, &ADCConfig))
{
am_util_stdio_printf("Error - configuring ADC failed.\n");
}
// Set up an ADC slot
ADCSlotConfig.eMeasToAvg = AM_HAL_ADC_SLOT_AVG_1;
ADCSlotConfig.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;
ADCSlotConfig.eChannel = AM_HAL_ADC_SLOT_CHSEL_SE0;
ADCSlotConfig.bWindowCompare = false;
ADCSlotConfig.bEnabled = true;
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_configure_slot(g_ADCHandle, 0, &ADCSlotConfig))
{
am_util_stdio_printf("Error - configuring ADC Slot 0 failed.\n");
}
ADCSlotConfig.eChannel = AM_HAL_ADC_SLOT_CHSEL_SE1;
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_configure_slot(g_ADCHandle, 1, &ADCSlotConfig))
{
am_util_stdio_printf("Error - configuring ADC Slot 0 failed.\n");
}
// For this example, the samples will be coming in slowly. This means we
// can afford to wake up for every conversion.
am_hal_adc_interrupt_enable(g_ADCHandle, AM_HAL_ADC_INT_CNVCMP );
// Enable the ADC.
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_enable(g_ADCHandle))
{
am_util_stdio_printf("Error - enabling ADC failed.\n");
}
}
//*****************************************************************************
//
// ADC反初时化.
//
//*****************************************************************************
void adc_deconfig(void)
{
//
// Disable the ADC.
//
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_disable(g_ADCHandle))
{
am_util_stdio_printf("Error - disable ADC failed.\n");
}
//
// Enable the ADC power domain.
//
if (AM_HAL_STATUS_SUCCESS != am_hal_pwrctrl_periph_disable(AM_HAL_PWRCTRL_PERIPH_ADC))
{
am_util_stdio_printf("Error - disabling the ADC power domain failed.\n");
}
//
// Initialize the ADC and get the handle.
//
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_deinitialize(g_ADCHandle))
{
am_util_stdio_printf("Error - return of the ADC instance failed.\n");
}
}
//*****************************************************************************
//
// Interrupt handler for the ADC.
//
//*****************************************************************************
void am_adc_isr(void)
{
uint32_t ui32IntMask;
am_hal_adc_sample_t Sample[2];
//
// Read the interrupt status.
//
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_interrupt_status(g_ADCHandle, &ui32IntMask, false))
{
am_util_stdio_printf("Error reading ADC interrupt status\n");
}
//
// Clear the ADC interrupt.
//
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_interrupt_clear(g_ADCHandle, ui32IntMask))
{
am_util_stdio_printf("Error clearing ADC interrupt status\n");
}
//
// If we got a conversion completion interrupt (which should be our only
// ADC interrupt), go ahead and read the data.
//
if (ui32IntMask & AM_HAL_ADC_INT_CNVCMP)
{
uint32_t ui32NumSamples = 2;
if (AM_HAL_STATUS_SUCCESS != am_hal_adc_samples_read(g_ADCHandle, false,
NULL,
&ui32NumSamples,
Sample))
{
am_util_stdio_printf("Error - ADC sample read from FIFO failed.\n");
}
am_util_stdio_printf("ADC Slot 0= %d\n", Sample[0].ui32Slot);
am_util_stdio_printf("ADC Value 0= %8.8X\n", Sample[0].ui32Sample);
am_util_stdio_printf("ADC Slot 1= %d\n", Sample[1].ui32Slot);
am_util_stdio_printf("ADC Value 1= %8.8X\n", Sample[1].ui32Sample);
}
adc_deconfig();
}
设置ADC输入
am_hal_gpio_pinconfig(16, g_AM_PIN_16_ADCSE0);
开中断NVIC_EnableIRQ(ADC_IRQn); am_hal_interrupt_master_enable();
调用初时化并触发ADC
adc_config(); // Trigger the ADC
am_hal_adc_sw_trigger(g_ADCHandle);