SAM4SD32 (SAM4S-EK2)
Loading...
Searching...
No Matches
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
{
// 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
}
}
@ ADC_CHANNEL_5
Definition adc.h:147
#define ADC_ISR_COMPE
(ADC_ISR) Comparison Error
uint32_t adc_get_status(const Adc *p_adc)
Get ADC interrupt and overrun error status.
Definition adc.c:676
uint32_t adc_get_channel_value(const Adc *p_adc, const enum adc_channel_num_t adc_ch)
Read the ADC result data of the specified channel.
Definition adc.c:413
uint32_t adc_get_comparison_mode(const Adc *p_adc)
Get comparison mode.
Definition adc.c:513
#define ADC
(ADC ) Base Address
Definition sam4sd32c.h:430
void adc_setup(void)
{
adc_init(ADC, sysclk_get_main_hz(), ADC_CLOCK, 8);
adc_set_comparison_window(ADC, MAX_DIGITAL, 0);
}
@ ADC_TRIG_TIO_CH_0
Definition adc.h:96
@ ADC_SETTLING_TIME_3
Definition adc.h:191
#define ADC_MR_LOWRES_BITS_12
(ADC_MR) 12-bit resolution
#define ADC_IER_COMPE
(ADC_IER) Comparison Event Interrupt Enable
#define ADC_EMR_CMPMODE_IN
(ADC_EMR) Generates an event when the converted data is in the comparison window.
void adc_configure_trigger(Adc *p_adc, const enum adc_trigger_t trigger, const uint8_t uc_freerun)
Configure conversion trigger and free run mode.
Definition adc.c:168
void adc_set_comparison_channel(Adc *p_adc, const enum adc_channel_num_t channel)
Configure comparison selected channel.
Definition adc.c:538
void adc_configure_timing(Adc *p_adc, const uint8_t uc_tracking, const enum adc_settling_time_t settling, const uint8_t uc_transfer)
Configure ADC timing.
Definition adc.c:261
void adc_set_resolution(Adc *p_adc, const enum adc_resolution_t resolution)
Configure the conversion resolution.
Definition adc.c:134
uint32_t adc_init(Adc *p_adc, const uint32_t ul_mck, const uint32_t ul_adc_clock, const enum adc_startup_time startup)
Initialize the given ADC with the specified ADC clock and startup time.
Definition adc.c:70
void adc_set_comparison_mode(Adc *p_adc, const uint8_t uc_mode)
Configure comparison mode.
Definition adc.c:500
void adc_enable_interrupt(Adc *p_adc, const uint32_t ul_source)
Enable ADC interrupts.
Definition adc.c:652
void adc_enable_channel(Adc *p_adc, const enum adc_channel_num_t adc_ch)
Enable the specified ADC channel.
Definition adc.c:347
void adc_set_comparison_window(Adc *p_adc, const uint16_t us_low_threshold, const uint16_t us_high_threshold)
Configure ADC compare window.
Definition adc.c:525

Workflow

  1. Define the interrupt service handler in the application:
  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:
  4. Set the ADC resolution.
  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:

void adc_start(Adc *p_adc)
Start analog-to-digital conversion.
Definition adc.c:326

Workflow

  1. Start ADC conversion on the configured channels: