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

nrf9160 adc acquisition changes relatively large

 Source code
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
*/

#include <net/socket.h>
#include <nrf9160.h>
#include <stdio.h>
#include <string.h>
#include <drivers/uart.h>
#include <drivers/adc.h>
#include <zephyr.h>
#include <drivers/gpio.h>
#include <hal/nrf_saadc.h>

struct device *adc_dev;
#define ADC_DEVICE_NAME DT_ADC_0_NAME
#define ADC_RESOLUTION 12 //鍒嗚鲸鐜鐨0娆℃柟-1
#define ADC_GAIN ADC_GAIN_1_6 //閲囨牱鏈€澶х數鍘媣 = 0.6*6V
#define ADC_REFERENCE ADC_REF_INTERNAL
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 3) //閲囨牱鏃堕棿3us 10k=3us 40k=5us 100k=10us 200k=15us 400k=20us 800k=40us
#define ADC_1ST_CHANNEL_ID 0
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0 //姝e悜閲囨牱杈撳叆閫氶亾涓篈DC0 0閫氶亾
#define ADC_2ND_CHANNEL_ID 2
#define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2 //鍙嶅悜閲囨牱杈撳叆閫氶亾涓篈DC0 2閫氶亾

static const struct adc_channel_cfg m_1st_channel_cfg =
{
.gain = ADC_GAIN,
.reference = ADC_REFERENCE,
.acquisition_time = ADC_ACQUISITION_TIME, //ADC_ACQ_TIME_DEFAULT,
.channel_id = ADC_1ST_CHANNEL_ID,
#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS)
.input_positive = ADC_1ST_CHANNEL_INPUT,
#endif
};

#define BUFFER_SIZE 1
static s16_t m_sample_buffer[BUFFER_SIZE];
static double adc_value;
static int adc_sample(void)
{
int ret;
const struct adc_sequence sequence =
{
.channels = BIT(ADC_1ST_CHANNEL_ID),
.buffer = m_sample_buffer,
.buffer_size = sizeof(m_sample_buffer),
.resolution = ADC_RESOLUTION,
.oversampling = 0,
};

if (!adc_dev)
{
return -1;
}

ret = adc_read(adc_dev, &sequence);
if(ret)
{
printk("ADC read err: %d\n", ret);
}

/* Print the AIN0 values */
for (int i = 0; i < BUFFER_SIZE; i++)
{
adc_value = m_sample_buffer[i];
adc_value = adc_value*3600;
adc_value = adc_value/4095.0d; //璁$畻鐢靛帇锛屾墿澶000鍊
adc_value = adc_value/1000.0d; //璁$畻鐪熷疄鐢靛帇
printk("ADC raw value: %d\n", m_sample_buffer[i]);
printf("Measured voltage: %06f mV\n", adc_value); //max = 2.912V min = 0.001v
}

return ret;
}

int main(void)
{
int err;
printk("nrf91 saadc sampling AIN0 (P0.13)\n");
printk("Example requires secure_boot to have ");

adc_dev = device_get_binding("ADC_0");
if (!adc_dev)
{
printk("device_get_binding ADC_0 failed\n");
}
err = adc_channel_setup(adc_dev, &m_1st_channel_cfg);
if (err)
{
printk("Error in adc setup: %d\n", err);
}

/* Trigger offset calibration
* As this generates a _DONE and _RESULT event
* the first result will be incorrect.
*/
NRF_SAADC_NS->TASKS_CALIBRATEOFFSET = 1; //娓╁樊鏍″噯
while (1)
{
err = adc_sample();
if (err)
{
printk("Error in adc sampling: %d\n", err);
}
k_sleep(2000);
}
}
2v voltage acquisition,Multimeter test is correct,
this is log
nrf91 saadc sampling AIN0 (P0.13)
Example requires secure_boot to have ADC raw value: -36
Measured voltage: -0.031648 mV
ADC raw value: 104
Measured voltage: 0.091429 mV
ADC raw value: 121
Measured voltage: 0.106374 mV
ADC raw value: 126
Measured voltage: 0.110769 mV
ADC raw value: 93
Measured voltage: 0.081758 mV
ADC raw value: 61
Measured voltage: 0.053626 mV
ADC raw value: 73
Measured voltage: 0.064176 mV
ADC raw value: 2146
Measured voltage: 1.886593 mV
ADC raw value: 2148
Measured voltage: 1.888352 mV
ADC raw value: 2063
Measured voltage: 1.813626 mV
ADC raw value: 2228
Measured voltage: 1.958681 mV
ADC raw value: 2099
Measured voltage: 1.845275 mV
ADC raw value: 2030
Measured voltage: 1.784615 mV
ADC raw value: 2175
Measured voltage: 1.912088 mV
ADC raw value: 2088
Measured voltage: 1.835604 mV
ADC raw value: 2128
Measured voltage: 1.870769 mV
ADC raw value: 2059
Measured voltage: 1.810110 mV
ADC raw value: 2200
Measured voltage: 1.934066 mV
ADC raw value: 2073
Measured voltage: 1.822418 mV
ADC raw value: 2044
Measured voltage: 1.796923 mV
ADC raw value: 2192
Measured voltage: 1.927033 mV
ADC raw value: 2168
Measured voltage: 1.905934 mV
ADC raw value: 2189
Measured voltage: 1.924396 mV
ADC raw value: 2228
Measured voltage: 1.958681 mV
ADC raw value: 2231
Measured voltage: 1.961319 mV
ADC raw value: 2055
Measured voltage: 1.806593 mV
ADC raw value: 2048
Measured voltage: 1.800440 mV
ADC raw value: 2187
Measured voltage: 1.922637 mV
ADC raw value: 1665
Measured voltage: 1.463736 mV
Related