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

nrf9160 adc

Dear Sir

I am trying to work with the ADC .

Attached a code 

I get the lines

00> Adc sample 13772
00> Adc sample 13772
00> Adc sample 13772
00> Adc sample 13772
00> Adc sample 13772
00> Adc sample 13772
00> Adc sample 13772
00> Adc sample 13772

The a2d input is wired to NTC , Voltage on NTC is 1.62V , VDD-GPIO = 3.3V

Please Advise

#define ADC_DEVICE_NAME DT_ADC_0_NAME
#define ADC_RESOLUTION 10
#define ADC_GAIN ADC_GAIN_1_6
#define ADC_REFERENCE ADC_REF_INTERNAL
#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10)
#define ADC_1ST_CHANNEL_ID 0
#define ADC_1ST_CHANNEL_INPUT NRF_SAADC_INPUT_AIN0
#define ADC_2ND_CHANNEL_ID 2
#define ADC_2ND_CHANNEL_INPUT NRF_SAADC_INPUT_AIN2

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



#define BUFFER_SIZE 1
static u16_t m_sample_buffer[BUFFER_SIZE];
int adc_init(void)
{
     int err = true;

      adc_dev = device_get_binding("ADC_0");
 //     if (!adc_dev)
      if (adc_dev == NULL)
      {
         printk("efi device_get_binding ADC_0 failed\n");
         err = false;
      }
      else
      {
         err = adc_channel_setup(adc_dev, &m_1st_channel_cfg);
         if (err)
         {
              printk("Error in adc setup: %d\n", err);
              err = false;       
         }
         else
         {
           printk("adc setup is OK \n" );
           err = true;
         }
      }
     return err;
} 

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,
	};

	if (!adc_dev) {
		return -1;
	}
  
	ret = adc_read(adc_dev, &sequence);
//	printk("ADC read err: %d\n", ret);
                adc_result = &m_sample_buffer[0];
	return ret;
}

Parents
  • Hi,

     

    This line looks a bit suspicious:

    adc_result = &m_sample_buffer[0];

     

    What type is adc_result? If its not a pointer, you will assign it the address of m_sample_buffer[0]. Based on the printout, it might look like that is the case? Which value does m_sample_buffer hold?

     

    Kind regards,

    Håkon

  • Hi.

    You are correct , I am embarrassed .

    Now it is working  

    I change the resolution to 12 bits  . #define ADC_RESOLUTION 12

    The reading is fluctuates 

    00> Adc sample 74f
    00> Adc sample 74a
    00> Adc sample 74b
    00> Adc sample 74f
    00> Adc sample 749
    00> Adc sample 74b
    00> Adc sample 74f
    00> Adc sample 755
    00> Adc sample 73f
    00> Adc sample 748
    00> Adc sample 74b
    00> Adc sample 74d
    00> Adc sample 760
    00> Adc sample 754
    00> Adc sample 75c
    00> Adc sample 762
    00> Adc sample 760
    00> Adc sample 758
    00> Adc sample 752
    00> Adc sample 747

    Please Advise

  • Hi,

     

    No worries. Glad to hear its working!

    It looks like you have a variation of 30 LSB (0x762 - 0x73f).

    This could be due to tolerance in resistors and the NTC itself. This can be checked using an oscilloscope on the signal itself to see how much it varies.

     

    Have you tried connecting a constant voltage directly to the ADC pin to see if that also varies?

     

    Kind regards,

    Håkon

Reply Children
No Data
Related