Configure ADC

  1. 1. In this example, the ADC is configured in callback mode, meaning that an interrupt will be executed at a predefined event after starting the conversion. This is further emphasized when configuring the callback routine in the following section. Declare the following struct globally:
    struct adc_module adc_instance;
  2. 2.Create a function configure_adc() in your project where you configure the ADC to sample the OPAMP2 output:
    /* Configure ADC */
    void configure_adc(void)
    {
    	/* Creates a new configuration structure for the ADC */
    	struct adc_config config_adc;
    
    	adc_get_config_defaults(&config_adc);
    	
    	/* Setup ADC with OPAMP2 output as ADC input */
    	config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV8;
    	config_adc.positive_input  = ADC_POSITIVE_INPUT_OPAMP2;
    Note: The ADC clock prescaler must be set accordingly to the specifications of the ADC module and the speed of the clock source. The ADC clock speed should not exceed the maximum conversion frequency.
  3. 3. The ADC is initiated and enabled with the following functions:
    /* Initialize and enable ADC */
    adc_init(&adc_instance, ADC, &config_adc);
    adc_enable(&adc_instance);
  4. 4. The resulting configure_adc() function shows the minimum number of configurations required for sampling the OPAMP2 output:
    /* Configure ADC */
    void configure_adc(void)
    {
    	/* Creates a new configuration structure for the ADC */
    	struct adc_config config_adc;
    
    	adc_get_config_defaults(&config_adc);
    	
    	/* Setup ADC with OPAMP2 output as ADC input */
    	config_adc.clock_prescaler = ADC_CLOCK_PRESCALER_DIV8;
    	config_adc.positive_input  = ADC_POSITIVE_INPUT_OPAMP2;
    	
    	/* Initialize and enable ADC */
    	adc_init(&adc_instance, ADC, &config_adc);
    	adc_enable(&adc_instance);
    }
    Note: For the sake of simplicity, the ADC is mostly configured with default settings. The accuracy of the sampling can be increased by applying built-in software selectable features, e.g. accumulate and divide samples for averaging and gain/offset correction.