Analog to Digital Converter (ADC) API.
More...
Analog to Digital Converter (ADC) API.
The RP2040 has an internal analogue-digital converter (ADC) with the following features:
- SAR ADC
- 500 kS/s (Using an independent 48MHz clock)
- 12 bit (8.7 ENOB)
- 5 input mux:
- 4 inputs that are available on package pins shared with GPIO[29:26]
- 1 input is dedicated to the internal temperature sensor
- 4 element receive sample FIFO
- Interrupt generation
- DMA interface
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
int main() {
printf("ADC Example, measuring GPIO26\n");
while (1) {
const float conversion_factor = 3.3f / (1 << 12);
printf("Raw value: 0x%03x, voltage: %f V\n", result, result * conversion_factor);
}
}
◆ 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
-
en | Enables write each conversion result to the FIFO |
dreq_en | Enable DMA requests when FIFO contains data |
dreq_thresh | Threshold for DMA requests/FIFO IRQ if enabled. |
err_in_fifo | If enabled, bit 15 of the FIFO contains error flag for each sample |
byte_shift | Shift 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
-
gpio | The 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
-
enabled | Set 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
-
run | false 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
-
◆ 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
-
clkdiv | If 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_mask | A 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
-
enable | Set true to power on the onboard temperature sensor, false to power off. |