gossamer 0.0.1
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
Analog to Digital Converter

Functions for using the ADC peripheral. More...

Functions

void adc_init (void)
 Initializes the ADC peripheral, but does not enable it.
 
void adc_enable (void)
 Enables the ADC peripheral.
 
void adc_set_sampling_length (uint8_t length)
 Sets the sampling length for the ADC.
 
uint16_t adc_get_analog_value (uint8_t pin)
 Gets the analog value on the given ADC pin.
 
uint16_t adc_get_analog_value_for_channel (uint8_t channel)
 Gets the analog value on the given ADC channel.
 
void adc_disable (void)
 Disables the ADC peripheral.
 

Detailed Description

Functions for using the ADC peripheral.

Function Documentation

◆ adc_get_analog_value()

uint16_t adc_get_analog_value ( uint8_t  pin)

Gets the analog value on the given ADC pin.

Parameters
pinThe ADC pin you wish to read (i.e. HAL_GPIO_A0_pin())
Returns
The analog value of the given ADC pin, from 0-65535 by default. If the pin is not an ADC pin, returns 0.

◆ adc_get_analog_value_for_channel()

uint16_t adc_get_analog_value_for_channel ( uint8_t  channel)

Gets the analog value on the given ADC channel.

Parameters
channelThe ADC channel you wish to read (this will come from the datasheet)
Returns
The analog value of the given ADC channel, from 0-65535 by default.

◆ adc_init()

void adc_init ( void  )

Initializes the ADC peripheral, but does not enable it.

This function sets up some sensible defaults for basic use cases:

  • Clocks the ADC with the main oscillator divided down to a 500 kHz ADC clock.
  • Sets sampling length to one clock cycle, which is appropriate for inputs with impedance up to 28 kOhm.
  • Sets oversampling of 16 samples, resulting in a conversion time of 32 microseconds.
  • Sets the ADC up for single-ended measurement (you are measuring the voltage between the analog input pin and ground, not between two analog input pins)
  • On SAM L21 and L22, sets the reference voltage to VDDANA, which allows use of the full 0-3.3V range.
  • On SAM D11 and D21, sets the reference voltage to VDDANA / 2 and the gain to 1/2, which also allows use of the full 0-3.3V range.

◆ adc_set_sampling_length()

void adc_set_sampling_length ( uint8_t  length)

Sets the sampling length for the ADC.

The sampling length sets the number of ADC clock cycles for which the ADC will sample the input, less 1 (so 0 represents 1 clock cycle). The default is 0, which is a good choice for inputs with impedance up to 28 kOhm. Each additional clock cycle of sampling time adds about 32 kOhm to the maximum input impedance.

See also
https://blog.thea.codes/getting-the-most-out-of-the-samd21-adc/