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

saadc sample convert issue

Hi,

I want to get Battery ADC Value.

So. I simply used nrf_drv_saadc_sample_convert() function.

At first I thought it was the right value,

Later, I found that the wrong value was coming out.

If you supply 1.5v, the value should be over 400, but less than 100.

CODE:

// ADC setting
void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    //ret_code_t err_code;
	//err_code =  nrf_drv_saadc_sample_convert(0, &m_battery);
	//APP_ERROR_CHECK(err_code);
}

static void bat_timeout_handler(void * p_context)
{
	UNUSED_PARAMETER(p_context);
	ret_code_t err_code;
	// get ADV value
	err_code = nrf_drv_saadc_sample_convert(0, &m_battery);
	APP_ERROR_CHECK(err_code);

	// (%)percent change formula
	uint8_t percent = ((m_battery-MIN_ADC)/DIF_ADC)*100;
	if(percent >= 100)
	{
		percent = 100;
	}
	NRF_LOG_INFO("ADC : %d%",m_battery);
	NRF_LOG_INFO("battery : %d%",percent);
	// send (%)percent value
	err_code = ble_bas_battery_level_update(&m_bas, percent, BLE_CONN_HANDLE_ALL);
	
	if ((err_code != NRF_SUCCESS) &&
        (err_code != NRF_ERROR_INVALID_STATE) &&
        (err_code != NRF_ERROR_RESOURCES) &&
        (err_code != NRF_ERROR_BUSY) &&
        (err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING))
    {
        APP_ERROR_HANDLER(err_code);
    }
}


static void saadc_init(void)
{
	ret_code_t err_code;
	
#if TEST 
	nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(ARDUINO_A0_PIN);
#else
	// SAADC configuration struct and pin setting
	nrf_saadc_channel_config_t channel_config = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(BAT_ADC_IN);
#endif
	// SAADC struct init and register SAADC evnet handler	
	err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);
	
	// SAADC channel init
	err_code = nrf_drv_saadc_channel_init(0, &channel_config);
	APP_ERROR_CHECK(err_code);
	
}

Let me know what's wrong.

Thank you.

(softdevice Ver : 6.1.1, SDK  Ver : 15.2.0, Testboard :  nRF52-DK)

  • Hi,

    Is TEST defined/set or not? If not, what is BAT_ADC_IN defined as?

    Note that NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE() expects the parameter to be of type nrf_saadc_input_t, which are defined as follows:

    #define SAADC_CH_PSELP_PSELP_NC (0UL) /*!< Not connected */
    #define SAADC_CH_PSELP_PSELP_AnalogInput0 (1UL) /*!< AIN0 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput1 (2UL) /*!< AIN1 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput2 (3UL) /*!< AIN2 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput3 (4UL) /*!< AIN3 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput4 (5UL) /*!< AIN4 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput5 (6UL) /*!< AIN5 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput6 (7UL) /*!< AIN6 */
    #define SAADC_CH_PSELP_PSELP_AnalogInput7 (8UL) /*!< AIN7 */
    #define SAADC_CH_PSELP_PSELP_VDD (9UL) /*!< VDD */

    ARDUINO_A0_PIN is defined as 3 in pca10040.h, which will correspond to AIN2 on the board. GPIO P0.03 is actually AIN1. I recommend using NRF_SAADC_INPUT_AINx when configuring the pins used for sampling.

    Best regards,
    Jørgen

Related