adc output calculation

hi team,

i have trying to wrote a code for analog to digital converter module in segger 5.40 using nrf52840 controller and and i found one example code related to analog to digital converter in  sdk 16.0. version.

in that code you are using on chip temperature sensor as a analog input. but you getting output in decimal numbers in degrees.

so can you provide formulae for converting output binary values in decimal value. why because i given vcc pin voltage as a input analog signal for analog to digital converter pins in my code but i am getting adc output as a 612 decimal number after converting adc output binary into decimal. but i do not know what is 612 is weather it is no of samples or  voltage. because i given 5v dc supply as a input and using default configurations for ADC driver api's.

but i am not getting again that input 5volts value after converting into digital output binary.

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "nrf.h"
#include "nrf_drv_saadc.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "boards.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "app_util_platform.h"
#include "nrf_pwr_mgmt.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"


/* Create an empty handler and pass this handler in the saadc initialization function
  > Normally this handler deals with the adc events but we are using blocking mode
  > In blocking mode the functions are called and the processor waits for the adc to finish taking samples from the respective channels
  > Event handler will not be called in this method
*/

void saadc_callback_handler(nrf_drv_saadc_evt_t const * p_event)
{
 // Empty handler function
}




// Create a function which configures the adc input pins and channels as well as the mode of operation of adc

void saadc_init(void)
{
	// A variable to hold the error code
  ret_code_t err_code;

  // Create a config struct and assign it default values along with the Pin number for ADC Input
  // Configure the input as Single Ended(One Pin Reading)
  // Make sure you allocate the right pin.
  nrf_saadc_channel_config_t channel_config = NRFX_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN2);

  // Initialize the saadc 
  // first parameter is for configuring the adc resolution and other features, we will see in future tutorial
  //on how to work with it. right now just pass a simple null value
  err_code = nrf_drv_saadc_init(NULL, saadc_callback_handler);
  APP_ERROR_CHECK(err_code);

// Initialize the Channel which will be connected to that specific pin.
  err_code = nrfx_saadc_channel_init(0, &channel_config);
  APP_ERROR_CHECK(err_code);

  

}



// A function which will initialize the Log module for us
void log_init(void)
{
	// check if any error occurred during its initialization
  APP_ERROR_CHECK(NRF_LOG_INIT(NULL));

	// Initialize the log backends module
  NRF_LOG_DEFAULT_BACKENDS_INIT();

}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
	
	// call the log initialization function
  log_init();

// call the saadc initialization function created above
  saadc_init();

// a struct to hold 16-bit value, create a variable of this type because our input resolution may vary from 8 bit to 14 bits depending on our configurations
// this variable holds the adc sample value
  nrf_saadc_value_t adc_val;


// Print a simple msg that everything started without any error
  NRF_LOG_INFO("Application Started!!!");


   
// Inifinite loop
    while (1)
    {
		// a blocking function which will be called and the processor waits until the value is read
		// the sample value read is in 2's complement and is automatically converted once retrieved
		// first parameter is for the adc input channel 
		// second parameter is to pass the address of the variable in which we store our adc sample value
      nrfx_saadc_sample_convert(0, &adc_val);

		// print this value using nrf log : here %d represents the integer value 
      NRF_LOG_INFO("Sample value Read: %d", adc_val);
		
		// use nrf log and float marker to show the floating point values on the log
		// calculate the voltage by this: input_sample * 3.6 / 2^n (where n = 8 or 10 or 12 or 14 depending on our configuration for resolution in bits)
      NRF_LOG_INFO("Volts: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(adc_val * 3.6 / 512));
       
	   // give 500ms delay 
       nrf_delay_ms(500);
     
    }
}


/** @} */

Parents
  • Hello,

    The analog input needs to be between 0V and VDD!

    When you want to perform an an analog measurement you have to first configure the gain and a voltage reference used by the ADC.

    For instance can configure the ADC to have a gain of 1/6 and using the 0.6V internal reference. This means that the analog input is divided by 6, for instance if the analog input is 3V, then the measured value of the ADC will be 3V/6=0.5V (which is less than the internal reference of 0.6V). If you now make an 8-bit measurement, then the the 3V analog input will give the result of (3V * 1/6 / 0.6V * 2^8) = 213.

    Best regards,
    Kenneth

Reply
  • Hello,

    The analog input needs to be between 0V and VDD!

    When you want to perform an an analog measurement you have to first configure the gain and a voltage reference used by the ADC.

    For instance can configure the ADC to have a gain of 1/6 and using the 0.6V internal reference. This means that the analog input is divided by 6, for instance if the analog input is 3V, then the measured value of the ADC will be 3V/6=0.5V (which is less than the internal reference of 0.6V). If you now make an 8-bit measurement, then the the 3V analog input will give the result of (3V * 1/6 / 0.6V * 2^8) = 213.

    Best regards,
    Kenneth

Children
No Data
Related