gossamer 0.0.0
a very lightweight app framework for SAMD and SAML chips
Loading...
Searching...
No Matches
Functions
adc.h File Reference

Analog to Digital Converter. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Functions

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

Detailed Description

Analog to Digital Converter.

Function Documentation

◆ adc_get_analog_value()

uint16_t adc_get_analog_value ( uint16_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/