Configure the ADC

  1. 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.
  2. Configure the ADC module.
    1. 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;
      
    2. 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.
    3. 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;
      
    4. Set ADC configurations.
      #if (SAMC21)
          adc_init(&adc_instance, ADC1, &config_adc);
      #else
          adc_init(&adc_instance, ADC, &config_adc);
      #endif
      
    5. Enable the ADC module so that conversions can be made.
      adc_enable(&adc_instance);