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

NRF52832, NRF_SAADC negative values

I played around with the SAADC sampling A4 pin. When I connect to GND, I see negative values sometimes. If I connect to 3.3V I see value 235/236 which is, what I expect.

Further I see a high negative value at first sample.

Since I set the ADC to 8 Bit, I expect a 8 Bit value, -128....127 or 0...255.

Is there an error in my declaration?

What can I do to avoid the strange value of sample 0?

Code:

/**
 * Test of analog Input capabilities
 * infocenter.nordicsemi.com/index.jsp
 *
 */

int ANALOG_SENSE_PIN = A4;  // select the input pin for the battery sense point

unsigned long PAUSE_TIME = 100;  // sleep time between reads (seconds * 1000 milliseconds)
uint16_t counter=0;
int16_t sampleval=0;

int16_t anaVal()
{
  // Serial.println("Inside hwCPUVoltage function.");
#if defined(NRF_SAADC)
  // NRF52:
  // Settings: 8Bit, AnalogInput4, 3us Time, internal ref., 1/6 gain, no oversample
  // Serial.println("This is an NRF_SAADC.");
  int32_t sample;
  NRF_SAADC->ENABLE = SAADC_ENABLE_ENABLE_Enabled << SAADC_ENABLE_ENABLE_Pos;
  NRF_SAADC->RESOLUTION = SAADC_RESOLUTION_VAL_8bit << SAADC_RESOLUTION_VAL_Pos;
  NRF_SAADC->CH[0].PSELP = SAADC_CH_PSELP_PSELP_AnalogInput4 << SAADC_CH_PSELP_PSELP_Pos;
  NRF_SAADC->CH[0].PSELN = SAADC_CH_PSELN_PSELN_NC << SAADC_CH_PSELN_PSELN_Pos;
  NRF_SAADC->CH[0].CONFIG = (SAADC_CH_CONFIG_BURST_Disabled << SAADC_CH_CONFIG_BURST_Pos) |
                            (SAADC_CH_CONFIG_MODE_SE << SAADC_CH_CONFIG_MODE_Pos) |
                            (SAADC_CH_CONFIG_TACQ_3us << SAADC_CH_CONFIG_TACQ_Pos) |
                            (SAADC_CH_CONFIG_REFSEL_Internal << SAADC_CH_CONFIG_REFSEL_Pos) |
                            (SAADC_CH_CONFIG_GAIN_Gain1_6 << SAADC_CH_CONFIG_GAIN_Pos) |
                            (SAADC_CH_CONFIG_RESN_Bypass << SAADC_CH_CONFIG_RESN_Pos) |
                            (SAADC_CH_CONFIG_RESP_Bypass << SAADC_CH_CONFIG_RESP_Pos);
  NRF_SAADC->OVERSAMPLE = SAADC_OVERSAMPLE_OVERSAMPLE_Bypass << SAADC_OVERSAMPLE_OVERSAMPLE_Pos;
  NRF_SAADC->SAMPLERATE = SAADC_SAMPLERATE_MODE_Task << SAADC_SAMPLERATE_MODE_Pos;
  NRF_SAADC->RESULT.MAXCNT = 1;
 

  NRF_SAADC->EVENTS_STARTED = 0;
  NRF_SAADC->TASKS_START = 1;
  while (!NRF_SAADC->EVENTS_STARTED);
  NRF_SAADC->EVENTS_STARTED = 0;

  NRF_SAADC->EVENTS_END = 0;
  NRF_SAADC->TASKS_SAMPLE = 1;
  while (!NRF_SAADC->EVENTS_END);
  NRF_SAADC->EVENTS_END = 0;

  NRF_SAADC->EVENTS_STOPPED = 0;
  NRF_SAADC->TASKS_STOP = 1;
  while (!NRF_SAADC->EVENTS_STOPPED);
  NRF_SAADC->EVENTS_STOPPED = 1;
  NRF_SAADC->RESULT.PTR = (int32_t)&sample;
//  NRF_SAADC->ENABLE = (SAADC_ENABLE_ENABLE_Disabled << SAADC_ENABLE_ENABLE_Pos);

  return sample;
#else
  Serial.println("Unknown MCU!!");
  // unknown MCU
  return 0;
#endif
}


void setup()
{
  Serial.begin(115200);
  Serial.println("Setup procedure beginning.");

  }


void loop() {
   while (true) {
    sampleval=anaVal();
    Serial.print("Sample:");
    Serial.print(counter++);
    Serial.print(" value:");
    Serial.println(sampleval);
    delay(PAUSE_TIME);
   }
 
  }

Output:

18:16:40.944 -> Setup procedure beginning.
18:16:40.944 -> Sample:0 value:-23131
18:16:41.045 -> Sample:1 value:0
18:16:41.145 -> Sample:2 value:0
18:16:41.245 -> Sample:3 value:0
18:16:41.346 -> Sample:4 value:0
18:16:41.446 -> Sample:5 value:0
18:16:41.546 -> Sample:6 value:0
18:16:41.647 -> Sample:7 value:0
18:16:41.747 -> Sample:8 value:0
18:16:41.847 -> Sample:9 value:-1
18:16:41.948 -> Sample:10 value:-1
18:16:42.048 -> Sample:11 value:0
18:16:42.148 -> Sample:12 value:0
18:16:42.249 -> Sample:13 value:0
18:16:42.349 -> Sample:14 value:0
18:16:42.449 -> Sample:15 value:0
18:16:42.550 -> Sample:16 value:0
18:16:42.650 -> Sample:17 value:0
18:16:42.750 -> Sample:18 value:0
18:16:42.850 -> Sample:19 value:0
18:16:42.951 -> Sample:20 value:0
18:16:43.051 -> Sample:21 value:0
18:16:43.151 -> Sample:22 value:-1
18:16:43.252 -> Sample:23 value:0
18:16:43.352 -> Sample:24 value:0
18:16:43.452 -> Sample:25 value:0
18:16:43.552 -> Sample:26 value:-1
18:16:43.653 -> Sample:27 value:0
18:16:43.753 -> Sample:28 value:0
18:16:43.853 -> Sample:29 value:-1

Parents Reply Children
No Data
Related