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

nrf52832 adc wrong value

Hi,

I try to read analog voltage over P28. Digital value(result variable in the code) always 238 as decimal. 

What is the missed thing in the code? 

Voltages supplied with power supply(as 0.5V,1V,2V)  

  volatile int16_t result = 0;
  volatile float precise_result = 0;
  
   //HFCLK
  NRF_CLOCK->TASKS_HFCLKSTART = 1;
  while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  
  NRF_SAADC->CH[4].CONFIG = (SAADC_CH_CONFIG_GAIN_Gain1_6    << SAADC_CH_CONFIG_GAIN_Pos) |
                            (SAADC_CH_CONFIG_MODE_SE         << SAADC_CH_CONFIG_MODE_Pos) |
                            (SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) |
                            (SAADC_CH_CONFIG_RESN_Bypass     << SAADC_CH_CONFIG_RESN_Pos) |
                            (SAADC_CH_CONFIG_RESP_Bypass     << SAADC_CH_CONFIG_RESP_Pos) |
                            (SAADC_CH_CONFIG_TACQ_3us        << SAADC_CH_CONFIG_TACQ_Pos);
                            
  
  NRF_SAADC->CH[4].PSELP = SAADC_CH_PSELP_PSELP_VDD << SAADC_CH_PSELP_PSELP_Pos;
  NRF_SAADC->CH[4].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos;
  
  NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_8bit << SAADC_RESOLUTION_VAL_Pos;
  
  NRF_SAADC->RESULT.MAXCNT = 1;
  NRF_SAADC->RESULT.PTR = (uint32_t)&result;
  
  NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;
  NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;
  
  NRF_SAADC->TASKS_START = 1;
  while (NRF_SAADC->EVENTS_STARTED == 0);
  NRF_SAADC->EVENTS_STARTED = 0;
  
  NRF_SAADC->TASKS_SAMPLE = 1;
  while (NRF_SAADC->EVENTS_END == 0);
  NRF_SAADC->EVENTS_END = 0;
  
  NRF_SAADC->TASKS_STOP = 1;
  while (NRF_SAADC->EVENTS_STOPPED == 0);
  NRF_SAADC->EVENTS_STOPPED = 0;
  
  result;
  precise_result = (float)result / 4551.1f;
  precise_result; 
  
  while(1) //breakpoint here
  
  

Thanks

Parents
  • Hi,

    It seems that you have mistaken the SAADC channels for analog inputs. Any analog input can be assigned to any channels, i.e. channel 4 is not hardwired to AIN4 (P0.28). What you are actually reading is the VDD voltage that you have assigned as the positive input for channel 4 on line 17: SAADC_CH_PSELP_PSELP_VDD.

    If you want to measure AIN4 (P0.28), you need to change this to SAADC_CH_PSELP_PSELP_AnalogInput4.

    Best regards,
    Jørgen

Reply
  • Hi,

    It seems that you have mistaken the SAADC channels for analog inputs. Any analog input can be assigned to any channels, i.e. channel 4 is not hardwired to AIN4 (P0.28). What you are actually reading is the VDD voltage that you have assigned as the positive input for channel 4 on line 17: SAADC_CH_PSELP_PSELP_VDD.

    If you want to measure AIN4 (P0.28), you need to change this to SAADC_CH_PSELP_PSELP_AnalogInput4.

    Best regards,
    Jørgen

Children
Related