Microchip® Advanced Software Framework

Advanced use cases

In this use case the ADC module and one 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
  • Comparison event happen and interrupt handling
  • Single channel measurement
  • ADC_CHANNEL_5 as positive input

Setup steps

Example code

Add to application C-file:

void ADC_IrqHandler(void)
{
// Check the ADC conversion status
if ((adc_get_status(ADC) & ADC_ISR_COMPE) == ADC_ISR_COMPE)
{
// Get comparison mode of ADC
uint32_t ul_mode = adc_get_comparison_mode(ADC);
// Get latest digital data value from ADC and can be used by application
uint16_t us_adc = adc_get_channel_value(ADC, ADC_CHANNEL_5);
}
}
void adc_setup(void)
{
adc_init(ADC, sysclk_get_main_hz(), ADC_CLOCK, 8);
adc_set_resolution(ADC, ADC_MR_LOWRES_BITS_12);
adc_set_comparison_mode(ADC, ADC_EMR_CMPMODE_IN);
adc_set_comparison_window(ADC, MAX_DIGITAL, 0);
adc_enable_interrupt(ADC, ADC_IER_COMPE);
}

Workflow

  1. Define the interrupt service handler in the application:
    • void ADC_IrqHandler(void)
      {
      // Check the ADC conversion status
      if ((adc_get_status(ADC) & ADC_ISR_COMPE) == ADC_ISR_COMPE)
      {
      // Get comparison mode of ADC
      uint32_t ul_mode = adc_get_comparison_mode(ADC);
      // Get latest digital data value from ADC and can be used by application
      uint16_t us_adc = adc_get_channel_value(ADC, ADC_CHANNEL_5);
      }
      }
    • Note
      Get ADC status and check if comparison event occurred. If occurred, read the ADC channel value and comparison mode.
  2. Initialize the given ADC with the specified ADC clock and startup time:
    • adc_init(ADC, sysclk_get_main_hz(), ADC_CLOCK, 10);
    • 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.
  3. 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
  4. Set the ADC resolution.
    • adc_set_resolution(ADC, ADC_MR_LOWRES_BITS_12);
    • Note
      The resolution value can be set to 10 bits or 12 bits.
  5. Enable the specified ADC channel:
  6. Set the comparison ADC channel, mode and window:
  7. Enable ADC interrupts:
  8. Configure software conversion trigger:

Usage steps

Example code

Add to, e.g., main loop in application C-file:

adc_start(ADC);

Workflow

  1. Start ADC conversion on the configured channels: