hardware_adc

Functions

void adc_init (void)
 Initialise the ADC HW.
 
static void adc_gpio_init (uint gpio)
 Initialise the gpio for use as an ADC pin. More...
 
static void adc_select_input (uint input)
 ADC input select. More...
 
static uint adc_get_selected_input (void)
 Get the currently selected ADC input channel. More...
 
static void adc_set_round_robin (uint input_mask)
 Round Robin sampling selector. More...
 
static void adc_set_temp_sensor_enabled (bool enable)
 Enable the onboard temperature sensor. More...
 
static uint16_t adc_read (void)
 Perform a single conversion. More...
 
static void adc_run (bool run)
 Enable or disable free-running sampling mode. More...
 
static void adc_set_clkdiv (float clkdiv)
 Set the ADC Clock divisor. More...
 
static void adc_fifo_setup (bool en, bool dreq_en, uint16_t dreq_thresh, bool err_in_fifo, bool byte_shift)
 Setup the ADC FIFO. More...
 
static bool adc_fifo_is_empty (void)
 Check FIFO empty state. More...
 
static uint8_t adc_fifo_get_level (void)
 Get number of entries in the ADC FIFO. More...
 
static uint16_t adc_fifo_get (void)
 Get ADC result from FIFO. More...
 
static uint16_t adc_fifo_get_blocking (void)
 Wait for the ADC FIFO to have data. More...
 
static void adc_fifo_drain (void)
 Drain the ADC FIFO. More...
 
static void adc_irq_set_enabled (bool enabled)
 Enable/Disable ADC interrupts. More...
 

Detailed Description

Analog to Digital Converter (ADC) API

The RP2040 has an internal analogue-digital converter (ADC) with the following features:

Although there is only one ADC you can specify the input to it using the adc_select_input() function. In round robin mode (adc_set_round_robin()), the ADC will use that input and move to the next one after a read.

User ADC inputs are on 0-3 (GPIO 26-29), the temperature sensor is on input 4.

Temperature sensor values can be approximated in centigrade as:

T = 27 - (ADC_Voltage - 0.706)/0.001721

The FIFO, if used, can contain up to 4 entries.

Example

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
int main() {
printf("ADC Example, measuring GPIO26\n");
// Make sure GPIO is high-impedance, no pullups etc
// Select ADC input 0 (GPIO26)
while (1) {
// 12-bit conversion, assume max value == ADC_VREF == 3.3 V
const float conversion_factor = 3.3f / (1 << 12);
uint16_t result = adc_read();
printf("Raw value: 0x%03x, voltage: %f V\n", result, result * conversion_factor);
sleep_ms(500);
}
}
void adc_init(void)
Initialise the ADC HW.
Definition: adc.c:11
static void adc_select_input(uint input)
ADC input select.
Definition: adc.h:86
static void adc_gpio_init(uint gpio)
Initialise the gpio for use as an ADC pin.
Definition: adc.h:69
static uint16_t adc_read(void)
Perform a single conversion.
Definition: adc.h:133
bool stdio_init_all(void)
Initialize all of the present standard stdio types that are linked into the binary.
Definition: stdio.c:283
void sleep_ms(uint32_t ms)
Wait for the given number of milliseconds before returning.
Definition: time.c:429

Function Documentation

◆ adc_fifo_drain()

static void adc_fifo_drain ( void  )
inlinestatic

Drain the ADC FIFO.

Will wait for any conversion to complete then drain the FIFO, discarding any results.

◆ adc_fifo_get()

static uint16_t adc_fifo_get ( void  )
inlinestatic

Get ADC result from FIFO.

Pops the latest result from the ADC FIFO.

◆ adc_fifo_get_blocking()

static uint16_t adc_fifo_get_blocking ( void  )
inlinestatic

Wait for the ADC FIFO to have data.

Blocks until data is present in the FIFO

◆ adc_fifo_get_level()

static uint8_t adc_fifo_get_level ( void  )
inlinestatic

Get number of entries in the ADC FIFO.

The ADC FIFO is 4 entries long. This function will return how many samples are currently present.

◆ adc_fifo_is_empty()

static bool adc_fifo_is_empty ( void  )
inlinestatic

Check FIFO empty state.

Returns
Returns true if the FIFO is empty

◆ adc_fifo_setup()

static void adc_fifo_setup ( bool  en,
bool  dreq_en,
uint16_t  dreq_thresh,
bool  err_in_fifo,
bool  byte_shift 
)
inlinestatic

Setup the ADC FIFO.

FIFO is 4 samples long, if a conversion is completed and the FIFO is full, the result is dropped.

Parameters
enEnables write each conversion result to the FIFO
dreq_enEnable DMA requests when FIFO contains data
dreq_threshThreshold for DMA requests/FIFO IRQ if enabled.
err_in_fifoIf enabled, bit 15 of the FIFO contains error flag for each sample
byte_shiftShift FIFO contents to be one byte in size (for byte DMA) - enables DMA to byte buffers.

◆ adc_get_selected_input()

static uint adc_get_selected_input ( void  )
inlinestatic

Get the currently selected ADC input channel.

Returns
The currently selected input channel. 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor.

◆ adc_gpio_init()

static void adc_gpio_init ( uint  gpio)
inlinestatic

Initialise the gpio for use as an ADC pin.

Prepare a GPIO for use with ADC by disabling all digital functions.

Parameters
gpioThe GPIO number to use. Allowable GPIO numbers are 26 to 29 inclusive.

◆ adc_irq_set_enabled()

static void adc_irq_set_enabled ( bool  enabled)
inlinestatic

Enable/Disable ADC interrupts.

Parameters
enabledSet to true to enable the ADC interrupts, false to disable

◆ adc_read()

static uint16_t adc_read ( void  )
inlinestatic

Perform a single conversion.

Performs an ADC conversion, waits for the result, and then returns it.

Returns
Result of the conversion.

◆ adc_run()

static void adc_run ( bool  run)
inlinestatic

Enable or disable free-running sampling mode.

Parameters
runfalse to disable, true to enable free running conversion mode.

◆ adc_select_input()

static void adc_select_input ( uint  input)
inlinestatic

ADC input select.

Select an ADC input. 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor.

Parameters
inputInput to select.

◆ adc_set_clkdiv()

static void adc_set_clkdiv ( float  clkdiv)
inlinestatic

Set the ADC Clock divisor.

Period of samples will be (1 + div) cycles on average. Note it takes 96 cycles to perform a conversion, so any period less than that will be clamped to 96.

Parameters
clkdivIf non-zero, conversion will be started at intervals rather than back to back.

◆ adc_set_round_robin()

static void adc_set_round_robin ( uint  input_mask)
inlinestatic

Round Robin sampling selector.

This function sets which inputs are to be run through in round robin mode. Value between 0 and 0x1f (bit 0 to bit 4 for GPIO 26 to 29 and temperature sensor input respectively)

Parameters
input_maskA bit pattern indicating which of the 5 inputs are to be sampled. Write a value of 0 to disable round robin sampling.

◆ adc_set_temp_sensor_enabled()

static void adc_set_temp_sensor_enabled ( bool  enable)
inlinestatic

Enable the onboard temperature sensor.

Parameters
enableSet true to power on the onboard temperature sensor, false to power off.