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

SAADC in blocking mode doesn't return a value.

I'm trying to use the SAADC to return one value so I initialize it, try to take the reading, then unitize it so I can use it for taking readings through the  PPI. The SAADC is working great using it with PPI and interrupts to return readings, but I can't get it to work in blocking mode. The value I initialize "results" as stays there. I've checked the registers and everything looks fine, and the RESULTS.PTR does point to my "results" variable.

Here's my initialization:

void battery_adc_init(void){

    ret_code_t err_code;

    nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    channel_config.acq_time = NRF_SAADC_ACQTIME_40US;

    nrfx_saadc_config_t default_config = NRFX_SAADC_DEFAULT_CONFIG; 
    default_config.oversample = NRF_SAADC_OVERSAMPLE_32X;
        
    err_code = nrfx_saadc_init(&default_config, battery_adc_callback);
    APP_ERROR_CHECK(err_code);

    err_code = nrfx_saadc_channel_init(0, &channel_config);
    APP_ERROR_CHECK(err_code);

    //nrfx_saadc_calibrate_offset();
    
    printf("battery_adc_init\r\n");
}

Here's where I'm trying to use nrfx_saadc_sample_convert:

static void send_battery (void){

    ret_code_t err_code;
    uint32_t battery_voltage;
    uint8_t battery_percent;
    nrf_saadc_value_t result = 60;

    //initilize battery adc.
    nrfx_saadc_uninit();
    battery_adc_init();
    
    //enable battery measurment voltage divider
    nrf_gpio_pin_write(GPIO_BATT_AN_EN, 1);
    nrf_delay_ms(5);

    //take the reading
    err_code = nrfx_saadc_sample_convert(0, &result);

    printf("err_code = %d\r\n", err_code);

    //turn off the voltage divider
    nrf_gpio_pin_write(GPIO_BATT_AN_EN, 0);

    //uninitialize saadc
    nrfx_saadc_uninit();

    //convert results
    battery_voltage = (result * 3600 * 100);
    battery_voltage /= 4096;

    if (battery_voltage >= MAX_BATTERY){
      battery_percent = 100;
    }
    else if (battery_voltage <= MIN_BATTERY){
      battery_percent = 0;
    }
    else {
      battery_percent = (((battery_voltage - MIN_BATTERY) * 100)/(MAX_BATTERY - MIN_BATTERY));        
    }

    printf("Batt: %dmV, %d%\r\n", battery_voltage, battery_percent);
    
    //Send new battery over BLE
    ble_bas_battery_level_update(&m_bas, battery_percent, BLE_CONN_HANDLE_ALL);  

    printf("send_battery\r\n");

}

err_code is returned as 0

Any ideas?

Parents
  • Hi,

    Have you tried to disable oversampling? The function nrfx_saadc_sample_convert will only trigger a single SAMPLE task, while the END event will not be generated until you have triggered this task 2^OVERSAMPLE times. The exception is if BURST mode is enabled for the channel, then it will be sufficient to trigger the sample task once:

    channel_config.burst = NRF_SAADC_BURST_ENABLED;

    Best regards,
    Jørgen

Reply
  • Hi,

    Have you tried to disable oversampling? The function nrfx_saadc_sample_convert will only trigger a single SAMPLE task, while the END event will not be generated until you have triggered this task 2^OVERSAMPLE times. The exception is if BURST mode is enabled for the channel, then it will be sufficient to trigger the sample task once:

    channel_config.burst = NRF_SAADC_BURST_ENABLED;

    Best regards,
    Jørgen

Children
Related