Read Differential adc and print the value serially using uart

Hi all iam trying to read the differential analog voltage between AIN0 and AIN1 which is pin0.02 and pin0.03 and print the voltage which will be in micro volts from 400 micorvolt, and since the examples in sdk uses log_flush i could not get the data or not acess any data from that, i need to acess the data and print the data using uart, can anybody help me out in this, thankyou.

Parents Reply Children
  • #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 "app_uart.h"
    #include "app_error.h"
    #include "nrf_delay.h"
    #include "nrf.h"
    #include "bsp.h"
    #if defined (UART_PRESENT)
    #include "nrf_uart.h"
    #endif
    #if defined (UARTE_PRESENT)
    #include "nrf_uarte.h"
    #endif
    
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    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();
    
    }
     
    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!!!");
    
    
        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);
            
        }
    }
    
    
    /** @} */
    

    The above is my code i could not print the voltage value on serial terminal, the above code is for single ended adc where do i miss i dont understand any idea please share with me.

  • Hi,

    Your handler is empty, so nothing happens, the handler completes, and the program moves on.


    Look at the saadc example, in the handler there is the following:

        if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
        {
            ret_code_t err_code;
    
            err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, SAMPLES_IN_BUFFER);
            APP_ERROR_CHECK(err_code);
    
            int i;
            NRF_LOG_INFO("ADC event number: %d", (int)m_adc_evt_counter);
    
            for (i = 0; i < SAMPLES_IN_BUFFER; i++)
            {
                NRF_LOG_INFO("%d", p_event->data.done.p_buffer[i]);
            }
            m_adc_evt_counter++;
        }


    Use this to get the data.

    Regards,
    Jonathan

Related