nRF5 SDK v12.1.0
ADC
This information applies to the nRF51 Series only.

The ADC driver includes two layers: the hardware access layer (HAL) and the driver layer (DRV).

The hardware access layer (HAL) provides basic APIs for accessing the registers of the analog-to-digital converter (ADC). See the API documentation for the ADC driver for details.

The driver layer provides APIs on a higher level than the HAL. The driver can perform software emulated scanning, buffer handling, and has two modes of data transfer: blocking and non-blocking. See the API documentation for the ADC driver for details.

The ADC Example provides sample code that you can use to quickly get started.

Initialization

Initialize the driver by calling nrf_drv_adc_init. The configuration includes the interrupt priority level. An event handler can be provided during the initialization. If no event handler is provided, the driver operates in blocking mode.

Using the ADC driver

The driver works with channels. Channels are objects with a conversion configuration and pin details. Multiple channels can be enabled at once; if more than one channel is enabled, they are linked together and scanned. The channel object content is manipulated by the driver, so it cannot be an automatic variable.

Use the function nrf_drv_adc_sample_convert to get a single sample on a single channel. This function configures ADC, starts sampling, and blocks until the conversion is completed. The channel does not need to be enabled to perform sampling on it.

Use nrf_drv_adc_buffer_convert to perform multiple conversions on multiple channels. This function sets up ADC but does not trigger a conversion. Conversions can be triggered either by calling nrf_drv_adc_sample or with PPI. The task address for PPI can be retrieved through the driver by calling nrf_drv_adc_start_task_get.

When a single sample is triggered, the driver starts a conversion on all enabled channels in a chain. In the ADC interrupt context, ADC is reconfigured and conversion is triggered until the last channel in a chain is reached or the buffer is full. The application must adjust the sampling frequency, taking into account the ADC conversion time and interrupt latency, because the ADC interrupt must be handled before the next conversion can be started.

If no event handler was provided during initialization, nrf_drv_adc_buffer_convert works in blocking mode. This means that it will not return until the buffer is full. If nrf_drv_adc_sample is used to trigger sampling, it must be called from a different context.

Channels are enabled by calling nrf_drv_adc_channel_enable and disabled by calling nrf_drv_adc_channel_disable. Use the macro NRF_DRV_ADC_DEFAULT_CHANNEL to initialize a channel object to its default settings.

The following example shows how to initialize the channel and start conversion to a buffer. Conversion is non-blocking because an event handler is provided during initialization. Channel variables are static. After conversion is set up, nrf_drv_adc_sample is called and a conversion chain is triggered. Only two samples are requested, so the user handler is called when conversion on the second channel is completed.

void adc_event_handler(​nrf_drv_adc_evt_t const * p_event)
{
}
int main(void)
{
nrf_adc_value_t buffer[2];
ret_code_t ret = nrf_drv_adc_init(NULL, adc_event_handler);
static nrf_drv_adc_channel_t channel1 = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_1); //note keyword 'static'
static nrf_drv_adc_channel_t channel2 = NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_2); //note keyword 'static'
// Start sampling to buffer.
ret = nrf_drv_adc_buffer_convert(buffer,2);
// Trigger conversion on both enabled channels.
}

At any time, you can call nrf_drv_adc_is_busy to check if the driver is busy.


Documentation feedback | Developer Zone | Subscribe | Updated