This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

SAADC gives different results for nrfx and nrf_drv

Hello all,

I have been testing the SAADC example from the peripheral directory.

The goal is to acquire a single sample from SAADC using nrf_drv_saadc_sample_convert() function.

I read over the internet and heard from that nrf_drv will be depricated, so I want to switch to nrfx library.

The issue is that, when I use nrf_drv library I get an ADC output from 0-1023, but nrfx is only from 0-255.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
{
// Do nothing, as we use blocking mode
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Above is the code with nrf_drv library (Desired)

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void saadc_event_handler(nrf_drv_saadc_evt_t const * p_event)
{
// Do nothing, as we use blocking mode
}
int saadc_init()
{
int ret = nrfx_saadc_init(NULL, saadc_event_handler);
if (ret) return ret;
const nrf_saadc_channel_config_t saadc_config = {
.resistor_p = NRF_SAADC_RESISTOR_DISABLED,
.resistor_n = NRF_SAADC_RESISTOR_DISABLED,
.gain = NRF_SAADC_GAIN1_4,
.reference = NRF_SAADC_REFERENCE_VDD4,
.acq_time = NRF_SAADC_ACQTIME_10US,
.mode = NRF_SAADC_MODE_SINGLE_ENDED,
.burst = NRF_SAADC_BURST_DISABLED,
.pin_p = NRF_SAADC_INPUT_AIN0,
.pin_n = NRF_SAADC_INPUT_DISABLED
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Above is the code for SAADC with nrfx which gives a range of 0-255 (Not desired)

I am not able to figure out about how to get SAADC working with nrfx library.

May be I am missing out on something. Could you help me out?

Thanks!

Best regards,

Navin

  • Hi,

    nrfx_saadc_init does not allow you to pass NULL to the config-argument. To get similar behavior to nrf_drv_saad_init, you need to pass a pointer to a config-struct that is initialized with the default config:

    Fullscreen
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Best regards,
    Jørgen

  • Thanks Jorgen! It works now..