This is the quickstart guide for the SAM ADC driver, with step-by-step instructions on how to configure and use the driver in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
Basic use case
In this basic use case, the ADC module and single channel are configured for:
- 12-bit, unsigned conversions
- Internal bandgap as 3.3 V reference
- ADC clock rate of at most 6.4 MHz and maximum sample rate is 1 MHz
- Software triggering of conversions
- Interrupt-based conversion handling
- Single channel measurement
- ADC_CHANNEL_5 as positive input
Prerequisites
- System Clock Management (Sysclock)
- Power Management Controller (PMC)
Setup steps
Example code
Add to application C-file:
void ADC_IrqHandler(void)
{
{
}
}
void adc_setup(void)
{
}
#define ADC_MR_LOWRES_BITS_12
(ADC_MR) 12-bit resolution
#define ADC_IER_DRDY
(ADC_IER) Data Ready Interrupt Enable
#define ADC_ISR_DRDY
(ADC_ISR) Data Ready
void adc_configure_trigger(Adc *p_adc, const enum adc_trigger_t trigger, const uint8_t uc_freerun)
Configure conversion trigger and free run mode.
uint32_t adc_get_latest_value(const Adc *p_adc)
Read the last ADC result data.
void adc_configure_timing(Adc *p_adc, const uint8_t uc_tracking, const enum adc_settling_time_t settling, const uint8_t uc_transfer)
Configure ADC timing.
void adc_set_resolution(Adc *p_adc, const enum adc_resolution_t resolution)
Configure the conversion resolution.
uint32_t adc_get_status(const Adc *p_adc)
Get ADC interrupt and overrun error status.
uint32_t adc_init(Adc *p_adc, const uint32_t ul_mck, const uint32_t ul_adc_clock, const enum adc_startup_time startup)
Initialize the given ADC with the specified ADC clock and startup time.
void adc_enable_interrupt(Adc *p_adc, const uint32_t ul_source)
Enable ADC interrupts.
void adc_enable_channel(Adc *p_adc, const enum adc_channel_num_t adc_ch)
Enable the specified ADC channel.
#define ADC
(ADC ) Base Address
Workflow
- Define the interrupt service handler in the application:
void ADC_IrqHandler(void)
{
{
}
}
- Note
- Get ADC status and check if the conversion is finished. If done, read the last ADC result data.
- Initialize the given ADC with the specified ADC clock and startup time:
- Note
- The ADC clock range is between master clock / 2 and master clock / 512. The function sysclk_get_main_hz() is used to get the master clock frequency while ADC_CLOCK gives the ADC clock frequency.
- Configure ADC timing:
- Note
- Tracking Time = (0 + 1) * ADC Clock period Settling Time = ADC_SETTLING_TIME_3 * ADC Clock period Transfer Time = (1 * 2 + 3) * ADC Clock period
- Set the ADC resolution.
- Note
- The resolution value can be set to 10 bits or 12 bits.
- Enable the specified ADC channel:
- Enable ADC interrupts:
- Configure software conversion trigger:
Usage steps
Example code
Add to, e.g., main loop in application C-file:
void adc_start(Adc *p_adc)
Start analog-to-digital conversion.
Workflow
- Start ADC conversion on channel:
Advanced use cases
For more advanced use of the ADC driver, see the following use cases: