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

Simpler ADC read function doesn't port to the right channel?

Hello there,

I'm trying to learn how to use some of the perepherials of the nrf52832 with the DK,

trying to learn a little about the SAADC I found some example modified it a little to work but in there it takes 8 lines of code to preform a single Analog read now looking a little at the API reference of info center I found this funstion called: nrf_drv_saadc_sample_convert();

which says there that it preforms a single ADC read but when trying it the values don't seem to change corresponding to the probed voltage on the pin here is my code:

#include <nrf.h>

int main(void)
{

   volatile unsigned int VDD = 0;
  volatile unsigned int X = 0;
  volatile unsigned int Y = 0;
  volatile float precise_result = 0;

  // Start HFCLK from crystal oscillator, this will give the SAADC higher accuracy
 // NRF_CLOCK->TASKS_HFCLKSTART = 1;
  //while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  //NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;

  // Configure SAADC singled-ended channel, Internal reference (0.6V) and 1/6 gain.
  NRF_SAADC->CH[0].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);

  // Configure the SAADC channel with VDD as positive input, no negative input(single ended).
 
  NRF_SAADC->CH[0].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos;

  // Configure the SAADC resolution.
  NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_10bit << SAADC_RESOLUTION_VAL_Pos;

  // Configure result to be put in RAM at the location of "result" variable.
  NRF_SAADC->RESULT.MAXCNT = 1;
 

  // No automatic sampling, will trigger with TASKS_SAMPLE.
  NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;

  // Enable SAADC (would capture analog pins if they were used in CH[0].PSELP)
  NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;

  // Calibrate the SAADC (only needs to be done once in a while)
  NRF_SAADC->TASKS_CALIBRATEOFFSET = 1;
  while (NRF_SAADC->EVENTS_CALIBRATEDONE == 0);
  NRF_SAADC->EVENTS_CALIBRATEDONE = 0;
  while (NRF_SAADC->STATUS == (SAADC_STATUS_STATUS_Busy <<SAADC_STATUS_STATUS_Pos));

  


  while (1)
  {

  /*

  NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_AnalogInput1 << SAADC_CH_PSELP_PSELP_Pos;
  NRF_SAADC->RESULT.PTR = (int)&X;
  // Start the SAADC and wait for the started event.
  NRF_SAADC->TASKS_START = 1;
  while (NRF_SAADC->EVENTS_STARTED == 0);
  NRF_SAADC->EVENTS_STARTED = 0;

  // Do a SAADC sample, will put the result in the configured RAM buffer.
  NRF_SAADC->TASKS_SAMPLE = 1;
  while (NRF_SAADC->EVENTS_END == 0);
  NRF_SAADC->EVENTS_END = 0;

  NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_AnalogInput2 << SAADC_CH_PSELP_PSELP_Pos;
  NRF_SAADC->RESULT.PTR = (int)&Y;
  // Start the SAADC and wait for the started event.
  NRF_SAADC->TASKS_START = 1;
  while (NRF_SAADC->EVENTS_STARTED == 0);
  NRF_SAADC->EVENTS_STARTED = 0;

  // Do a SAADC sample, will put the result in the configured RAM buffer.
  NRF_SAADC->TASKS_SAMPLE = 1;
  while (NRF_SAADC->EVENTS_END == 0);
  NRF_SAADC->EVENTS_END = 0;


  // Stop the SAADC, since it's not used anymore.
// NRF_SAADC->TASKS_STOP = 1;
//  while (NRF_SAADC->EVENTS_STOPPED == 0);
//  NRF_SAADC->EVENTS_STOPPED = 0;
    //__WFE();
*/
    
  
  
   // nrf_drv_saadc_sample_convert(SAADC_CH_PSELN_PSELN_VDD, &VDD);
    nrf_drv_saadc_sample_convert(1, &X);
    nrf_drv_saadc_sample_convert(2, &Y);

    

  }
}

in the main function the code in the comment seem to work maybe I'm doing something wrong.

Parents Reply Children
Related