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

Saadc in sample convert

Hi All 

I'm trying to use the Adc in Sample convert mode:  "nrfx_saadc_sample_convert(0,&ADC_Val);" to sequential read two ADC pins .

I have used the code below to init the saadc with a single channel. and changing the Analog pin "nrf_saadc_channel_pos_input_set () "

My question is how to change the pin of the ADC in the code below the pin didn't change post init function. 

Also can you please explain if I need to use the" busy =nrfx_saadc_is_busy()" function, to verify that the ADC is clear for another sample?

please advise.

void saadc_init_convert(void)
{
   uint32_t err_code;

   nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(chanel_num);
   err_code = nrfx_saadc_init(NULL, saadc_callback);
   APP_ERROR_CHECK(err_code);
   channel_config.gain = NRF_SAADC_GAIN1_6;

   err_code = nrf_drv_saadc_channel_init (0, &channel_config);
   APP_ERROR_CHECK(err_code);
}

int main(void)

{

   while (1)
  {
     nrf_saadc_channel_pos_input_set (0,NRF_SAADC_INPUT_AIN0);
     err_code =nrfx_saadc_sample_convert(0,&ADC_Val[0]);
     APP_ERROR_CHECK(err_code);
    // change mux control 
     nrf_saadc_channel_pos_input_set (0,NRF_SAADC_INPUT_AIN2);
     err_code =nrfx_saadc_sample_convert(0,&ADC_Val[1]);
      APP_ERROR_CHECK(err_code);
    }

}

  • Hi,

    There is no need to use nrf_drv_saadc_is_busy()/nrfx_saadc_is_busy() or similar, since nrf_drv_saadc_sample_convert()/nrfx_saadc_sample_convert() is blocking. You can refer to this code to see a working example of what (I think) you are trying to do (overwrite the existing examples\peripheral\saadc\main.c in SDK 15.3):

    #include <stdbool.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <string.h>
    #include "nrf.h"
    #include "nrf_drv_saadc.h"
    #include "boards.h"
    #include "app_error.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_callback(nrf_drv_saadc_evt_t const * p_event)
    {
        NRF_LOG_INFO("ADC event");
    }
    
    
    void saadc_init(void)
    {
        ret_code_t err_code;
    
        err_code = nrf_drv_saadc_init(NULL, saadc_callback);
        APP_ERROR_CHECK(err_code);
    
        // Configure channel 0
        nrf_saadc_channel_config_t channel_config_0 =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0); // AIN0 = P0.02
    
        err_code = nrf_drv_saadc_channel_init(0, &channel_config_0);
        APP_ERROR_CHECK(err_code);
    
        // Configure channel 1
        nrf_saadc_channel_config_t channel_config_1 =
            NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN1); // AIN1 = P0.03
    
        err_code = nrf_drv_saadc_channel_init(1, &channel_config_1);
        APP_ERROR_CHECK(err_code);
    }
    
    
    void simple_saadc_demo(void)
    {
        ret_code_t err_code;
        nrf_saadc_value_t sample;
    
        // Sample channel 0
        err_code =nrfx_saadc_sample_convert(0, &sample);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_INFO("Channel 0 sample: %i", sample);
    
        // Sample channel 1
        err_code =nrfx_saadc_sample_convert(1, &sample);
        APP_ERROR_CHECK(err_code);
    
        NRF_LOG_INFO("Channel 1 sample: %i", sample);
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        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);
    
        saadc_init();
    
        simple_saadc_demo();
    
        NRF_LOG_INFO("Simple SAADC demo started.");
    
        while (1)
        {
            nrf_pwr_mgmt_run();
            NRF_LOG_FLUSH();
        }
    }
    

    Note that blocking operations may not be the best for a practical application that also needs to do other things. You can also use the SAADC in scan mode, so that it automatically samples both pins consecutively as fast as possible, and gives you an event when both samples have been collected.

Related