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

Sampling ADC with two sampling frequency alternately....

Hi,

I am working in one application in which I need SAADC to sample two channels at 1ksps and 500 samples per seconds.

I am sending 1 and 2 to switch the sampling rate i.e. if I send 1 then SAADC should sample at 1ksps and for 2 it should sample at 500 samples per second.

I am trying like below:

               int main(void)
{
    
    bool erase_bonds;

    // Initialize.
    gpio_init();
    app_trace_init();
    timers_init();
    buttons_leds_init(&erase_bonds);
    uart_int();
    ble_stack_init();
    device_manager_init(erase_bonds);
    gap_params_init();
    services_init();
    advertising_init();
    sensor_simulator_init();
    conn_params_init();
    
    
    
    // Start execution.
    application_timers_start();
    err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
    APP_ERROR_CHECK(err_code);
    
//    saadc_sampling_event_init();
    saadc_init();
    
    //saadc_sampling_event_enable();
                
    printf("\n\rStart: \n\r");
                
    // Enter main loop.
    for (;;)
    {
        if (data_ready == true)
        {
            data_ready = false;
            hr_algorithm();
        }
        
        if (abc == 0x01)
        {
          abc = 0x00;
          freq = 1000;
          saadc_sampling_event_init();
          nrf_delay_ms(10);
          saadc_sampling_event_enable();
          mode_2_flag = false;
        }
        if (abc == 0x02)
        {
          abc = 0x00;
          freq = 500;
          saadc_sampling_event_init();
          nrf_delay_ms(10);
          saadc_sampling_event_enable();
          mode_2_flag = true;
        }
        power_manage();
    }
}

Here abc is the variable used which receives data from APP. Freq is variable which holds sampling frequency.

saadc_sampling_event_init(); function is standard function that is used in sample codes.

While I am trying this, SAADC is not getting initialized. Complete device gets restarted when I send data from APP.

Can anyone suggest whether is correct way of doing this?

Parents
  • Calling saadc_sampling_event_init() multiple times will not work. Every time you call saadc_sampling_event_init() you will try to initialize ppi and timer module again which will return an error code, which will make the application stop because of APP_ERROR_CHECK(err_code). Even if you fix this you will also allocate new PPI channel every time, and eventually you will run out of PPI channels. So don't call saadc_sampling_event_init() multiple times (unless you implement a proper uninit function).

    What you can do is to change the compare value of the timer with the nrf_drv_timer_extended_compare(..) function. Be aware though that if you don't stop and clear the timer first, the timer may miss the compare event (the current value of the timer may be larger than the new compare value) and count until it overflows. You can avoid this by updating with stop, clear and resume like this (sets the compare value to 400ms or 2.5Hz):

    nrf_drv_timer_pause(&timer);
    nrf_drv_timer_extended_compare(&timer, (nrf_timer_cc_channel_t)0, 400 * 1000UL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
    nrf_drv_timer_clear(&timer);
    nrf_drv_timer_resume(&timer);
    

    Or you can do it with disable and enable:

    nrf_drv_timer_disable(&timer);
    nrf_drv_timer_extended_compare(&timer, (nrf_timer_cc_channel_t)0, 400 * 1000UL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
    nrf_drv_timer_enable(&timer);
    
Reply Children
No Data
Related