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?