ADC

The Voltix Module has two analog pins that can be sampled with the 12-bit successive approximation ADC on-board the nRF52. Additionally, the ADC can be used to sample the supply voltage and the capacitor voltage.

The API supports simple reading of single samples with a default configuration and more sophisticated periodic sampling with custom configuration.

Important

Always check the return code of voltix_adc_sample(...) to ensure that sampling has actually completed (VOLTIX_SUCCESS) before working with the data.

Example usage

 1
 2#include "voltix.h"
 3#include "voltix_timing.h"
 4#include "voltix_adc.h"
 5#include "printf.h"
 6
 7void lateinit(void) {
 8  voltix_adc_init();
 9}
10
11int main(void) {
12  for (;;) {
13    int16_t val = voltix_adc_read(VOLTIX_ADC_INPUT_A0);
14    printf("ADC value: %d\r\n", val);
15    voltix_sleep_ms(1000);
16  }
17}

API reference

enum voltix_adc_input_t

Values:

enumerator VOLTIX_ADC_INPUT_NC

Input not connected.

enumerator VOLTIX_ADC_INPUT_VCAP

Capacitor voltage.

enumerator VOLTIX_ADC_INPUT_A0

Analog input A0

enumerator VOLTIX_ADC_INPUT_A1

Analog input A1

enum voltix_adc_resolution_t

Values:

enumerator VOLTIX_ADC_RESOLUTION_8BIT

8 bit resolution

enumerator VOLTIX_ADC_RESOLUTION_10BIT

10 bit resolution

enumerator VOLTIX_ADC_RESOLUTION_12BIT

12 bit resolution

enumerator VOLTIX_ADC_RESOLUTION_14BIT

14 bit resolution

enum voltix_adc_oversample_t

Values:

enumerator VOLTIX_ADC_OVERSAMPLE_DISABLED

No oversampling.

enumerator VOLTIX_ADC_OVERSAMPLE_2X

Oversample 2x.

enumerator VOLTIX_ADC_OVERSAMPLE_4X

Oversample 4x.

enumerator VOLTIX_ADC_OVERSAMPLE_8X

Oversample 8x.

enumerator VOLTIX_ADC_OVERSAMPLE_16X

Oversample 16x.

enumerator VOLTIX_ADC_OVERSAMPLE_32X

Oversample 32x.

enumerator VOLTIX_ADC_OVERSAMPLE_64X

Oversample 64x.

enumerator VOLTIX_ADC_OVERSAMPLE_128X

Oversample 128x.

enumerator VOLTIX_ADC_OVERSAMPLE_256X

Oversample 256x.

enum voltix_adc_gain_t

Values:

enumerator VOLTIX_ADC_GAIN1_6

Gain factor 1/6.

enumerator VOLTIX_ADC_GAIN1_5

Gain factor 1/5.

enumerator VOLTIX_ADC_GAIN1_4

Gain factor 1/4.

enumerator VOLTIX_ADC_GAIN1_3

Gain factor 1/3.

enumerator VOLTIX_ADC_GAIN1_2

Gain factor 1/2.

enumerator VOLTIX_ADC_GAIN1

Gain factor 1.

enumerator VOLTIX_ADC_GAIN2

Gain factor 2.

enumerator VOLTIX_ADC_GAIN4

Gain factor 4.

enum voltix_adc_reference_t

Values:

enumerator VOLTIX_ADC_REFERENCE_INTERNAL

Internal reference (0.6 V).

enumerator VOLTIX_ADC_REFERENCE_VDD4

VDD/4 reference.

enum voltix_adc_acqtime_t

Values:

enumerator VOLTIX_ADC_ACQTIME_3US

3us acquisition time.

enumerator VOLTIX_ADC_ACQTIME_5US

5us acquisition time.

enumerator VOLTIX_ADC_ACQTIME_10US

10us acquisition time.

enumerator VOLTIX_ADC_ACQTIME_15US

15us acquisition time.

enumerator VOLTIX_ADC_ACQTIME_20US

20us acquisition time.

enumerator VOLTIX_ADC_ACQTIME_40US

40us acquisition time.

enum voltix_adc_res_t

Values:

enumerator VOLTIX_ADC_RES_NOPULL

No pullup on ADC input.

enumerator VOLTIX_ADC_RES_PULLDOWN

Pulldown on ADC input.

enumerator VOLTIX_ADC_RES_PULLUP

Pullup on ADC input.

enumerator VOLTIX_ADC_RES_VDD1_2

Pullup and pulldown (VDD/2) on ADC input.

void voltix_adc_init(void)

Initializes ADC. Must be called once after reset before ADC can be used.

voltix_rc_t voltix_adc_sample(int16_t *dst, voltix_adc_cfg_t *cfg)

Reads multiple samples from the ADC.

Periodically samples the ADC with a specified sampling interval storing the samples in the provided buffer. Blocks until all samples are taken or until the operation is aborted due to low energy.

Parameters:
  • dst – Buffer where samples are stored.

  • cfg – ADC and sampling configuration.

Return values:
  • VOLTIX_SUCCESS – Sampling completed.

  • VOLTIX_ERR_RESET – Reset occured while sampling.

  • VOLTIX_ERR_TEARDOWN – Teardown occured while sampling.

int16_t voltix_adc_read(voltix_adc_input_t in)

Reads a sample from the ADC.

Reads one sample from the ADC with a preconfigured configuration. Blocks until sample is taken. Gain and reference settings are chosen such that the full 12-bit input range equals the supply voltage.

Parameters:

in – Analog input.

Returns:

int16_t ADC sample as 12-bit value w.r.t. the supply voltage.

float voltix_adc_adc2vadc(int16_t adc, voltix_adc_cfg_t *cfg)

Converts a raw binary value sampled from the ADC to a voltage value.

Parameters:
  • adc – 12-bit ADC sample.

  • cfg – ADC configuration that was used for taking the sample.

Returns:

float Voltage value.

static inline float voltix_adc_vadc2vcap(float v_adc)

Converts ADC input voltage to capacitor voltage based on amplifier gain.

Parameters:

v_adc – ADC input voltage.

Returns:

float Capacitor voltage.

static inline voltix_rc_t voltix_adc_pin2input(voltix_adc_input_t *input, unsigned int pin)

Translates digital pin to analog input channel.

Parameters:
  • input – Pointer where ADC input gets stored.

  • pin – Pin number.

Return values:
  • VOLTIX_SUCCESS – Successfully converted.

  • VOLTIX_ERR_INVALIDARG – Pin is not an ADC input.

struct voltix_adc_cfg_t