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.

#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
}

int saadc_init()
{
  int ret = nrf_drv_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
      };

  ret = nrf_drv_saadc_channel_init(0, &saadc_config);
  return ret;
}

int saadc_measure()
{
  nrf_saadc_value_t value;
  nrf_drv_saadc_sample_convert(0, &value);
  return value;
  
}

double convertAdc(int16_t value)
{ 
  return value * (3.3/1023);
}


int main() 
{
  uint32_t err_code = NRF_LOG_INIT(NULL);
  APP_ERROR_CHECK(err_code);

  NRF_LOG_DEFAULT_BACKENDS_INIT();

  ret_code_t ret_code = nrf_pwr_mgmt_init();
  APP_ERROR_CHECK(ret_code);

  NRF_LOG_INFO("SAADC Start");

  saadc_init();
  int16_t measurement;
   
  while (1)
  {
      nrf_delay_ms(100);
      measurement = saadc_measure();
      float voltage = convertAdc(measurement);
      //NRF_LOG_INFO("Measured value = %ld", measurement);

      NRF_LOG_INFO("Voltage:         " NRF_LOG_FLOAT_MARKER "V\r\n", NRF_LOG_FLOAT(voltage));

      nrf_pwr_mgmt_run();
      NRF_LOG_FLUSH();
  }
}

Above is the code with nrf_drv library (Desired)

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
      };

  ret = nrfx_saadc_channel_init(0, &saadc_config);
  return ret;
}

int saadc_measure()
{
  nrf_saadc_value_t value;
  nrfx_saadc_sample_convert(0, &value);
  return value;
  
}

double convertAdc(int16_t value)
{ 
  return value * (3.3/1023);
}


int main() 
{
  uint32_t err_code = NRF_LOG_INIT(NULL);
  APP_ERROR_CHECK(err_code);

  NRF_LOG_DEFAULT_BACKENDS_INIT();

  ret_code_t ret_code = nrf_pwr_mgmt_init();
  APP_ERROR_CHECK(ret_code);

  NRF_LOG_INFO("SAADC Start");

  saadc_init();
  int16_t measurement;
   
  while (1)
  {
      nrf_delay_ms(100);
      measurement = saadc_measure();
      float voltage = convertAdc(measurement);
      //NRF_LOG_INFO("Measured value = %ld", measurement);

      NRF_LOG_INFO("Voltage:         " NRF_LOG_FLOAT_MARKER "V\r\n", NRF_LOG_FLOAT(voltage));

      nrf_pwr_mgmt_run();
      NRF_LOG_FLUSH();
  }
}

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

Parents Reply Children
No Data
Related