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

How to configuration multi saadc channel with different sample interval

Dear Sir:

  i seen the proximity example,the battery level measure is init by a m_battery_timer_id ,in my project ,

i need user multi saadc channel ,and different channel has different sample interval , but the api nrf_drv_saadc_init seems only have one saadc event handler,so

how i get different channel‘s sample result?

Parents
  • Hi,

    It is not possible to set different sample interval for each channel in the SAADC peripheral, you will have to handle this manually in your application by init/uninit the channels before and after each sample. Something like this should work:

    bool sample_in_progress = false;
    uint8_t current_sample_channel;
    int16_t channel0_sample;
    int16_t channel1_sample;
    
    nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
    timer_handler_0()
    {
      if(!sample_in_progress)
      {
        current_sample_channel = 0;
        channel_config.pselp = NRF_SAADC_INPUT_AIN0;
        nrf_drv_saadc_channel_init(0,channel_config);
        sample_in_progress = true;
        nrf_drv_saadc_sample();
      }
    }
    
    timer_handler_1()
    {
      if(!sample_in_progress)
      {
        current_sample_channel = 1;
        channel_config.pselp = NRF_SAADC_INPUT_AIN1;
        nrf_drv_saadc_channel_init(0,channel_config);
        sample_in_progress = true;
        nrf_drv_saadc_sample();
      }
    }
    
    saacd_callback_handler()
    {
      if(current_sample_channel == 0)
      {
        channel0_sample = result;
      }
      else 
      {
        channel1_sample = result;
      }
      nrf_drv_saadc_channel_uninit(0);
      sample_in_progress = false;
    }

    Note that this is not intended as a complete example, it only shows how to do the switching between channels. Also note that if the other channel is sampling, this code will simply skip the sample for the channel. If you need to sample each channel every time, you need to wait for the other channel to finish before switching channel. Make sure you get your priorities correct if you need to do waiting in interrupt context.

    Best regards,
    Jørgen

Reply
  • Hi,

    It is not possible to set different sample interval for each channel in the SAADC peripheral, you will have to handle this manually in your application by init/uninit the channels before and after each sample. Something like this should work:

    bool sample_in_progress = false;
    uint8_t current_sample_channel;
    int16_t channel0_sample;
    int16_t channel1_sample;
    
    nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    
    timer_handler_0()
    {
      if(!sample_in_progress)
      {
        current_sample_channel = 0;
        channel_config.pselp = NRF_SAADC_INPUT_AIN0;
        nrf_drv_saadc_channel_init(0,channel_config);
        sample_in_progress = true;
        nrf_drv_saadc_sample();
      }
    }
    
    timer_handler_1()
    {
      if(!sample_in_progress)
      {
        current_sample_channel = 1;
        channel_config.pselp = NRF_SAADC_INPUT_AIN1;
        nrf_drv_saadc_channel_init(0,channel_config);
        sample_in_progress = true;
        nrf_drv_saadc_sample();
      }
    }
    
    saacd_callback_handler()
    {
      if(current_sample_channel == 0)
      {
        channel0_sample = result;
      }
      else 
      {
        channel1_sample = result;
      }
      nrf_drv_saadc_channel_uninit(0);
      sample_in_progress = false;
    }

    Note that this is not intended as a complete example, it only shows how to do the switching between channels. Also note that if the other channel is sampling, this code will simply skip the sample for the channel. If you need to sample each channel every time, you need to wait for the other channel to finish before switching channel. Make sure you get your priorities correct if you need to do waiting in interrupt context.

    Best regards,
    Jørgen

Children
No Data
Related