<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/49446/how-to-vary-gain-in-saadc-example-nrf52832-sdk15-3</link><description>Im using nrf52832 kit with sdk15.3 SAADC example for ADC output, which is working fine but im not getting how to vary gain. 
 i want to set gain these values 1,2,4,5,8,10,16,32 . Im using only 1 channel channel0 (AIN0). 
 Is it possible to set more than</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 12 Jul 2019 08:52:14 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/49446/how-to-vary-gain-in-saadc-example-nrf52832-sdk15-3" /><item><title>RE: How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/thread/198082?ContentTypeID=1</link><pubDate>Fri, 12 Jul 2019 08:52:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f2e312c8-1a10-425e-bb29-df3cf2e066fe</guid><dc:creator>awneil</dc:creator><description>&lt;p&gt;This seems to be a duplicate:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/49644/how-to-set-or-configure-gain-register-in-saadc-example-so-that-i-can-set-various-gain"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/49644/how-to-set-or-configure-gain-register-in-saadc-example-so-that-i-can-set-various-gain&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/thread/198078?ContentTypeID=1</link><pubDate>Fri, 12 Jul 2019 08:46:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:aa4a7261-36dd-4786-ad02-c63458620438</guid><dc:creator>Pooja Vernekar</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *	
 */
/** @file
 * @defgroup nrf_adc_example main.c
 * @{
 * @ingroup nrf_adc_example
 * @brief ADC Example Application main file.
 *
 * This file contains the source code for a sample application using ADC.
 *
 * @image html example_board_setup_a.jpg &amp;quot;Use board setup A for this example.&amp;quot;
 */

#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nrf_drv_saadc.h&amp;quot;
#include &amp;quot;nrf_drv_ppi.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;nrf_pwr_mgmt.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;


#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;
//-------------------------spi code---------------------------
#include &amp;quot;nrf_drv_spi.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;


#define SPI_INSTANCE  1 /**&amp;lt; SPI instance index. */
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**&amp;lt; SPI instance. */
static volatile bool spi_xfer_done;  /**&amp;lt; Flag used to indicate that SPI instance completed the transfer. */

//#define TEST_STRING &amp;quot;Nordic&amp;quot;
static uint8_t       m_tx_buf[00100000];           /**&amp;lt; TX buffer. */
static uint8_t       m_rx_buf[40];    /**&amp;lt; RX buffer. */
//static const uint8_t m_length = sizeof(m_tx_buf);        /**&amp;lt; Transfer length. */


/**
 * @brief SPI user event handler.
 * @param event
 */
void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
   spi_xfer_done = true;
	
		NRF_LOG_INFO(&amp;quot; Sent %d bytes: &amp;quot;, p_event-&amp;gt;data.done.tx_length);
		for(int i=0; i&amp;lt; p_event-&amp;gt;data.done.tx_length;i++)
		{
				NRF_LOG_INFO(&amp;quot; ;(0x%x); &amp;quot;,p_event-&amp;gt;data.done.p_tx_buffer[i], p_event-&amp;gt;data.done.p_tx_buffer[i]);
		}
		//NRF_LOG_INFO(&amp;quot;\r\n&amp;quot;);
		NRF_LOG_INFO(&amp;quot; Received %d bytes: &amp;quot;, p_event-&amp;gt;data.done.rx_length);
		 for(int i=0; i&amp;lt; p_event-&amp;gt;data.done.rx_length;i++)
		{
				NRF_LOG_INFO(&amp;quot; ;(0x%x); &amp;quot;,p_event-&amp;gt;data.done.p_rx_buffer[i], p_event-&amp;gt;data.done.p_rx_buffer[i]);
				nrf_delay_ms(5);
		}
		//NRF_LOG_INFO(&amp;quot;\r\n\n&amp;quot;);
}


void spi_con(void)
{
	
 nrf_gpio_cfg_output(18); //Configure pin 18 as output

 NRF_CLOCK-&amp;gt;TASKS_HFCLKSTART = 1; //Start high frequency clock
 while (NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED == 0)
 {
      //Wait for HFCLK to start
 }
 NRF_CLOCK-&amp;gt;EVENTS_HFCLKSTARTED = 0; //Clear event
 
 
 //Configure GPIOTE to toggle pin 18 
 NRF_GPIOTE-&amp;gt;CONFIG[0] = GPIOTE_CONFIG_MODE_Task &amp;lt;&amp;lt; GPIOTE_CONFIG_MODE_Pos |
                         GPIOTE_CONFIG_POLARITY_Toggle &amp;lt;&amp;lt; GPIOTE_CONFIG_POLARITY_Pos |
                         18 &amp;lt;&amp;lt; GPIOTE_CONFIG_PSEL_Pos | 
                         GPIOTE_CONFIG_OUTINIT_Low &amp;lt;&amp;lt; GPIOTE_CONFIG_OUTINIT_Pos;
 
 //Configure timer
 NRF_TIMER1-&amp;gt;PRESCALER = 3;
 NRF_TIMER1-&amp;gt;CC[0] = 1;  // Adjust the output frequency by adjusting the CC.
 NRF_TIMER1-&amp;gt;SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos;
 NRF_TIMER1-&amp;gt;TASKS_START = 1;
 
 //Configure PPI
 NRF_PPI-&amp;gt;CH[0].EEP = (uint32_t) &amp;amp;NRF_TIMER1-&amp;gt;EVENTS_COMPARE[0];
 NRF_PPI-&amp;gt;CH[0].TEP = (uint32_t) &amp;amp;NRF_GPIOTE-&amp;gt;TASKS_OUT[0];
 
 NRF_PPI-&amp;gt;CHENSET = PPI_CHENSET_CH0_Enabled &amp;lt;&amp;lt; PPI_CHENSET_CH0_Pos;
	
    LEDS_CONFIGURE(BSP_LED_0_MASK);
    LEDS_OFF(BSP_LED_0_MASK);
	
	
	
		nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = SPI_SS_PIN;
    spi_config.miso_pin = SPI_MISO_PIN;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin  = SPI_SCK_PIN;
    APP_ERROR_CHECK(nrf_drv_spi_init(&amp;amp;spi, &amp;amp;spi_config, spi_event_handler, NULL));

    NRF_LOG_INFO(&amp;quot;SPI example started.&amp;quot;);


        // Reset rx buffer and transfer done flag
        memset(m_rx_buf, 0, 40);
        spi_xfer_done = false;

			  m_tx_buf[0]=0x0d;
			
			
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&amp;amp;spi, m_tx_buf, 2, m_rx_buf, 40));
	
	
	
        while (!spi_xfer_done)
        {
            __WFE();
        }
	
			
}

#define SAMPLES_IN_BUFFER 20
volatile uint8_t state = 1;

static const nrf_drv_timer_t m_timer = NRF_DRV_TIMER_INSTANCE(0);
static nrf_saadc_value_t     m_buffer_pool[2][SAMPLES_IN_BUFFER];
static nrf_ppi_channel_t     m_ppi_channel;
static uint32_t              m_adc_evt_counter;
int GAIN=0;

void timer_handler(nrf_timer_event_t event_type, void * p_context)
{

}


void saadc_sampling_event_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_32;
    err_code = nrf_drv_timer_init(&amp;amp;m_timer, &amp;amp;timer_cfg, timer_handler);
    APP_ERROR_CHECK(err_code);

    /* setup m_timer for compare event every 400ms */
    uint32_t ticks = nrf_drv_timer_ms_to_ticks(&amp;amp;m_timer, 100);
    nrf_drv_timer_extended_compare(&amp;amp;m_timer,
                                   NRF_TIMER_CC_CHANNEL0,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                   false);
    nrf_drv_timer_enable(&amp;amp;m_timer);

    uint32_t timer_compare_event_addr = nrf_drv_timer_compare_event_address_get(&amp;amp;m_timer,
                                                                                NRF_TIMER_CC_CHANNEL0);
    uint32_t saadc_sample_task_addr   = nrf_drv_saadc_sample_task_get();

    /* setup ppi channel so that timer compare event is triggering sample task in SAADC */
    err_code = nrf_drv_ppi_channel_alloc(&amp;amp;m_ppi_channel);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_ppi_channel_assign(m_ppi_channel,
                                          timer_compare_event_addr,
                                          saadc_sample_task_addr);
    APP_ERROR_CHECK(err_code);
}


void saadc_sampling_event_enable(void)
{
    ret_code_t err_code = nrf_drv_ppi_channel_enable(m_ppi_channel);

    APP_ERROR_CHECK(err_code);
}


void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    if (p_event-&amp;gt;type == NRF_DRV_SAADC_EVT_DONE)
    {
        ret_code_t err_code;

        err_code = nrf_drv_saadc_buffer_convert(p_event-&amp;gt;data.done.p_buffer, SAMPLES_IN_BUFFER);
        APP_ERROR_CHECK(err_code);

        int i;
        NRF_LOG_INFO(&amp;quot;ADC event number: %d&amp;quot;, (int)m_adc_evt_counter);

        for (i = 0; i &amp;lt; SAMPLES_IN_BUFFER; i++)
        {
            NRF_LOG_INFO(&amp;quot;%d&amp;quot;, p_event-&amp;gt;data.done.p_buffer[i]);
        }
        m_adc_evt_counter++;
    }
}


void saadc_init(void)
{
    ret_code_t err_code;
    nrf_saadc_channel_config_t channel_config =
     NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
    switch(GAIN)
		{
			case 1:
				nrf_drv_saadc_channel_uninit(NRF_SAADC_GAIN1);
			case 2:
			 nrf_drv_saadc_channel_uninit (NRF_SAADC_GAIN2);
			case 3:
				nrf_drv_saadc_channel_uninit(NRF_SAADC_GAIN4);
			default:
				NRF_LOG_INFO(&amp;quot;no operation&amp;quot;);	
		}
    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_channel_init(0, &amp;amp;channel_config);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAMPLES_IN_BUFFER);
    APP_ERROR_CHECK(err_code);
	
		

}


/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();

    ret_code_t ret_code = nrf_pwr_mgmt_init();
    APP_ERROR_CHECK(ret_code);

		spi_con();
    saadc_init();
    saadc_sampling_event_init();
    saadc_sampling_event_enable();
    NRF_LOG_INFO(&amp;quot;SAADC HAL simple example started.&amp;quot;);

    while (1)
    {
        nrf_pwr_mgmt_run();
        NRF_LOG_FLUSH();
    }
}


/** @} */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Hello Kenneth,&lt;/p&gt;
&lt;p&gt;I have attached code please verify the code and give feedback what changes to be made if required.&lt;/p&gt;
&lt;p&gt;As channel 0 consists of AIN0-AIN7 so i want each of AIN pins to get configured with each gain. I have added switch case but im not sure with code what i did. I want t use each gain defined in header file.&lt;/p&gt;
&lt;p&gt;Please suggest&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/thread/197974?ContentTypeID=1</link><pubDate>Fri, 12 Jul 2019 05:22:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:18549a70-7f46-4586-b48f-469a9f4312cf</guid><dc:creator>Pooja Vernekar</dc:creator><description>&lt;p&gt;but if i define gain in nrf.saadc.h then it gives error.&lt;/p&gt;
&lt;p&gt;it works fine for default gain, but i want to set gain with different values. How can i do that ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/thread/197933?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2019 16:59:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96cf176f-69cf-4710-bb1e-991b97e654b4</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;If you base your code on&amp;nbsp;\examples\peripheral\saadc\ I believe this should be straight forward. Simply call&amp;nbsp;nrf_drv_saadc_channel_init(), nrf_drv_saadc_buffer_convert(), and&amp;nbsp;nrf_drv_saadc_channel_uninit() for each gain setting you want to sample.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/thread/197799?ContentTypeID=1</link><pubDate>Thu, 11 Jul 2019 11:05:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e229289-6e58-4270-bc10-2d353e5df3ae</guid><dc:creator>Pooja Vernekar</dc:creator><description>&lt;p&gt;Hello Kenneth,&lt;/p&gt;
&lt;p&gt;Im trying to configure or set gain in&amp;nbsp; SAADC example, for various gains it should operate but im not getting how to set gain register so that i get SAADC output depending on various gain and even i tried to change gain values in nrfx.saadc.h file but getting error. gains defined in nrfx.saadc.h files are default one but i want it to work as per my amplifier. So how can i define gains and make it work properly. Im using only 1channel AIN0&lt;/p&gt;
&lt;p&gt;Presently its working properly with default gain values.&lt;/p&gt;
&lt;p&gt;Please Suggest.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to vary gain in SAADC example nrf52832 SDK15.3 ?</title><link>https://devzone.nordicsemi.com/thread/197124?ContentTypeID=1</link><pubDate>Mon, 08 Jul 2019 12:14:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9566916f-77d9-4a22-b8f2-10435275c040</guid><dc:creator>Kenneth</dc:creator><description>&lt;p&gt;You will need to execute a new ADC measurement for each configuration, only 1 gain setting can be active at the time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>