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

How to read out SAADC values with multiple channel enabled?

Hi Devzone:

I have 1 differential pair and 1 single ended pin I want to read, so I wrote code below into saadc_init function:

            nrf_saadc_channel_config_t channel_config_IMON =
                NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN4);  //SINGLE END

            nrf_saadc_channel_config_t channel_config_VBAT
                NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_DIFFERENTIAL(NRF_SAADC_INPUT_AIN3, NRF_SAADC_INPUT_AIN2); //DIFFERENTIAL

In this case AIN4 is the single ended pin and AIN2 and AIN3 are the differential pair.

I'm not quite sure how to get ADC values respectively in saadc_callback function.

Here is the function code from the example project.

       void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
        {
            if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
            {
                ret_code_t err_code;

                err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
                APP_ERROR_CHECK(err_code);

                int i;
                NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);

                for (i = 0; i < SAMPLES_IN_BUFFER; i++)
                {                   
                    NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]);
                }
                m_adc_evt_counter++;
            }
        }

My question is:

1. what value do I need to set for SAMPLES_IN_BUFFER since we have one differential pair and one single ended pin? 2 or 3?

2. in the for loop 

for (i = 0; i < SAMPLES_IN_BUFFER; i++)

what value is being printed out by 

NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]);

?

Is it with i=0, it is printing out the differential channel and when i=1 the single ended channel is being printed out?

Thank you.

  • Hi,

    1. Both single-ended and differential sampling will generate a single sample per channel (single-ended is internally differential mode with the negative input connected to GND). You should set it to a multiple of the number of enabled channels. If you want to get the callback after the two channels have been sampled once, you should set the SAMPLES_IN_BUFFER define to 2.

    2. This depends on what channel you have used for each of the configs. If you used the single-ended config for channel 0, the sample for this will be in p_buffer[0]. The samples are stored in the buffer as described in the SAADC peripheral documentation.

    Best regards,
    Jørgen

Related