Configure the ADC
- Create a module software instance structure for the ADC module to store the ADC driver state while it is 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. - 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. - Set extra configurations.
#if !(SAML21)
#if !(SAMC21)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
#endif
config_adc.resolution = ADC_RESOLUTION_10BIT;
#endif
config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16;
config_adc.reference = ADC_REFERENCE_INTVCC1;
config_adc.positive_input = ADC_POSITIVE_INPUT_PIN4;
config_adc.freerunning =
true
;
config_adc.left_adjust =
false
;
- 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);