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

difference value of mbed analog read and keil adc

FormerMember
FormerMember

Hello,

I am using keil uvision 5.17, sdk 9.0.0 and sd 130.

Once I programmed whole application through mbed,

Then I moved into keil and implemented all function.

but only ADC is not implemented.

in mbed :

analogIn btt(p5);
printf("%d",btt.read_u16());

result = about 740

in keil :

void adc_config(void)
{
  const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG_DEFAULT;

  // Initialize and configure ADC
  nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config);
  nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_6);
  nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos);
  NVIC_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_HIGH);
  NVIC_EnableIRQ(ADC_IRQn);
}

void ADC_IRQHandler(void)
{
  nrf_adc_conversion_event_clean();
  adc_result = nrf_adc_result_get();
  nrf_adc_stop();
  printf("%d",adc_result);
}

result = about 510

I got this value same sensor, chip and environment.

How can I fix this difference?

I wanna get same value with mbed or the fomular of 510 to 740.

Thanks,

TY

Parents
  • Because of the difference of the ADC reference. The mbed ADC API uses NRF_ADC_CONFIG_REF_SUPPLY_ONE_THIRD(1/3 of power supply) as a reference, and your adc_config() uses the default configuration of nRF5x SDK(1.2 V reference). Check the values of NRF_ADC_CONFIG_DEFAULT and void analogin_init(analogin_t *obj, PinName pin) of the mbed ADC API.

    nrf_adc.h (nRF5x SDK):

    /** Default ADC configuration. */
    #define NRF_ADC_CONFIG_DEFAULT { NRF_ADC_CONFIG_RES_10BIT, \
    				 NRF_ADC_CONFIG_SCALING_INPUT_ONE_THIRD,   \
    				 NRF_ADC_CONFIG_REF_VBG }
    

    analogin_api.c (mbed):

    void analogin_init(analogin_t *obj, PinName pin)
    {
        ...
        NRF_ADC->CONFIG = (ADC_CONFIG_RES_10bit << ADC_CONFIG_RES_Pos) |
                          (ADC_CONFIG_INPSEL_AnalogInputOneThirdPrescaling << ADC_CONFIG_INPSEL_Pos) |
                          (ADC_CONFIG_REFSEL_SupplyOneThirdPrescaling << ADC_CONFIG_REFSEL_Pos) |
                          (analogInputPin << ADC_CONFIG_PSEL_Pos) |
                          (ADC_CONFIG_EXTREFSEL_None << ADC_CONFIG_EXTREFSEL_Pos);
    }
    
Reply Children
No Data
Related