Jump to main content
2.8.2.1.3 Workflow
Create a module software instance structure for the ADC module to store the ADC driver state while in use.
struct
adc_module adc_instance;
Note: This should never go out of scope as long as the module is in use. In most cases, this should be global.
Create a buffer for the ADC samples to be stored in by the driver asynchronously.
#define ADC_SAMPLES 128
uint16_t adc_result_buffer[ADC_SAMPLES];
Create a callback function that will be called each time the ADC completes an asynchronous read job.
volatile
bool
adc_read_done =
false
;
void
adc_complete_callback(
struct
adc_module *
const
module)
{
adc_read_done =
true
;
}
Configure the ADC module.
Create an ADC module configuration struct, which can be filled out to adjust the configuration of a physical ADC peripheral.
struct
adc_config config_adc;
Initialize the ADC configuration struct with the module's default values.
adc_get_config_defaults(&config_adc);
Note: This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
Change the ADC module configuration to suit the application.
#if (!SAML21) && (!SAML22) && (!SAMC21)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
#endif
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV8;
config_adc.reference = ADC_REFERENCE_INTVCC1;
#if (SAMC21)
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN5;
#else
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN6;
#endif
config_adc.resolution = ADC_RESOLUTION_12 BIT;
Set ADC configurations.
#if (SAMC21)
adc_init(&adc_instance, ADC1, &config_adc);
#else
adc_init(&adc_instance, ADC, &config_adc);
#endif
Enable the ADC module so that conversions can be made.
adc_enable(&adc_instance);
Register and enable the ADC Read Buffer Complete callback handler.
Register the user-provided Read Buffer Complete callback function with the driver, so that it will be run when an asynchronous buffer read job completes.
adc_register_callback(&adc_instance,
adc_complete_callback, ADC_CALLBACK_READ_BUFFER);
Enable the Read Buffer Complete callback so that it will generate callbacks.
adc_enable_callback(&adc_instance, ADC_CALLBACK_READ_BUFFER);
The online versions of the documents are provided as a courtesy. Verify all content and data in the device’s PDF documentation found on the device product page.