<?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>Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/58970/simple-one-shot-adc-example</link><description>Hi, 
 I&amp;#39;m trying to find simple ADC code for one shot modus as described in the documentation: https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fsaadc.html 
 The question has been asked before (without any concrete</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 16 Mar 2020 07:23:22 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/58970/simple-one-shot-adc-example" /><item><title>RE: Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/thread/239872?ContentTypeID=1</link><pubDate>Mon, 16 Mar 2020 07:23:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d7cdef2a-7eff-4eda-aad5-8459dcef9e22</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Philip, you seem to miss my point.&lt;/p&gt;
&lt;p&gt;You can remove&amp;nbsp;saadc_sampling_event_init() and&amp;nbsp;saadc_sampling_event_enable() from my main.c file, but you will have to call&amp;nbsp;nrf_drv_saadc_sample() from somewhere.&lt;/p&gt;
&lt;p&gt;If I removed this from the file before sending it to you, I would be sending a project that does nothing.&lt;/p&gt;
&lt;p&gt;Try to remove those functions, and add&amp;nbsp;nrf_drv_saadc_sample() to whatever event you want to trigger the sampling from.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/thread/239822?ContentTypeID=1</link><pubDate>Sat, 14 Mar 2020 08:24:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b303e0ba-49bb-43d0-a7c1-878ad8c91d45</guid><dc:creator>ephimee</dc:creator><description>&lt;p&gt;If I follow your code.&lt;/p&gt;
&lt;p&gt;In the function main() there&amp;#39;s a call to&amp;nbsp;saadc_sampling_event_init().&lt;/p&gt;
&lt;p&gt;In the function&amp;nbsp;&lt;span&gt;saadc_sampling_event_init() there&amp;#39;s a call to&amp;nbsp;nrf_drv_timer_init().&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I tried to remove all timer related stuff, but then it no longer works. I&amp;#39;m looking specifically for an example without timers. Which means without any reference to *timer* function calls and without any *timer* includes.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/thread/239480?ContentTypeID=1</link><pubDate>Thu, 12 Mar 2020 09:48:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2584f17e-737e-46f6-9c7d-63f62789ff7d</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;This is what I tried to show with the attached example.&lt;/p&gt;
&lt;p&gt;Use&amp;nbsp;&lt;/p&gt;
&lt;p&gt;nrf_drv_saadc_sample();&lt;/p&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;p&gt;NRF_SAADC-&amp;gt;TASKS_SAMPLE = 1;&lt;/p&gt;
&lt;p&gt;to trigger one sample from anywhere in your project. The point was that I detached the PPI from the timer, and called it in the interrupt. You can of course call it from anywhere.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/thread/239459?ContentTypeID=1</link><pubDate>Thu, 12 Mar 2020 08:23:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e90905ec-7205-439b-8b6e-acd3e8ceccf6</guid><dc:creator>ephimee</dc:creator><description>&lt;p&gt;Hi Edwin,&lt;/p&gt;
&lt;p&gt;Thanks for your reply.&lt;/p&gt;
&lt;p&gt;This code does not use one shot mode but continues mode.&lt;/p&gt;
&lt;p&gt;Also this code uses timers. I don&amp;#39;t have timers available because they are being used by other peripherals.&lt;/p&gt;
&lt;p&gt;I know the advantage of not waiting for the CPU. But because of the limited resources I rather have a simple example that waits to the CPU than a complex example that uses a lot of fancy includes &amp;amp; functionality that make no sense to me.&lt;/p&gt;
&lt;p&gt;Is it even possible to do ADC without timer? All I need is:&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;int value = analogRead(NRF_SAADC_INPUT_AIN7);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;#39;t mind to wait 50ms.&lt;/p&gt;
&lt;p&gt;/Philip&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/thread/239393?ContentTypeID=1</link><pubDate>Wed, 11 Mar 2020 16:05:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:79f40d64-f977-4ca3-839c-d8738cd3b9a9</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;The code looks broadly correct assuming that pin P0.26 provides the excitation voltage for something connected on pin P0.31. (edited, I misread the original)&lt;/p&gt;
&lt;p&gt;You are missing an errata workaround, however, detailed here:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;  // Calibrate the SAADC (only needs to be done once in a while)
  NRF_SAADC-&amp;gt;TASKS_CALIBRATEOFFSET = 1;
  while (NRF_SAADC-&amp;gt;EVENTS_CALIBRATEDONE == 0)
     ;
  NRF_SAADC-&amp;gt;EVENTS_CALIBRATEDONE = 0;
  while (NRF_SAADC-&amp;gt;STATUS == (SAADC_STATUS_STATUS_Busy &amp;lt;&amp;lt; SAADC_STATUS_STATUS_Pos))
     ;
  // [Errata 86] SAADC: Triggering START task after offset calibration may write a sample to RAM
  //  This anomaly applies to IC Rev. Rev 2, build codes QFAA-Ex0, CIAA-Ex0, QFAB-Ex0.
  //  Calibration should follow the pattern STOP -&amp;gt; STOPPED -&amp;gt; CALIBRATEOFFSET -&amp;gt; CALIBRATEDONE -&amp;gt; STOP -&amp;gt; STOPPED -&amp;gt; START
  // Stop the SAADC and wait for the stopped event as per Errata 86
  NRF_SAADC-&amp;gt;TASKS_STOP = 1;
  while (NRF_SAADC-&amp;gt;EVENTS_STOPPED == 0)
     ;
  NRF_SAADC-&amp;gt;EVENTS_STOPPED = 0;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Additionally I would suggest looking at the binary result in case there is some issue with IDE floating point settings in (say) printf as below:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;  printf(&amp;quot;result: 0x%04X, precise_result: %0.2f\n&amp;quot;, result, precise_result);
  printf(&amp;quot;result: 0x%04X, precise_result: %4.2f\n&amp;quot;, result, precise_result);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;%0.2f does not do what you expect; try 5.2% maybe :-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Simple one shot ADC example</title><link>https://devzone.nordicsemi.com/thread/239389?ContentTypeID=1</link><pubDate>Wed, 11 Mar 2020 15:52:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:370dcbb0-de89-4db9-b9e1-8a25b0666d7d</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;I agree that this example is a bit complex for showing some simple functionality.&lt;/p&gt;
&lt;p&gt;But it is actually quite simple to change this from using PPI to trigger it manually. Please check out the attached main.c file, which is a modification from the saadc examples main.c file:&lt;/p&gt;
&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;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

#define SAMPLES_IN_BUFFER 5
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;


void timer_handler(nrf_timer_event_t event_type, void * p_context)
{
    NRF_LOG_INFO(&amp;quot;Timer interrupt&amp;quot;);
    
    NRF_SAADC-&amp;gt;TASKS_SAMPLE = 1;
    /*
    // Alternatively, you can use the driver to trigger a sample:
    nrf_drv_saadc_sample();
    */
}


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, 400);

    nrf_drv_timer_extended_compare(&amp;amp;m_timer,
                                   NRF_TIMER_CC_CHANNEL0,
                                   ticks,
                                   NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK,
                                   true);
    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);

    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);

    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;&lt;/p&gt;
&lt;p&gt;Please note that I changed one parameter in&amp;nbsp;nrf_drv_timer_extended_compare() as well, in order to trigger the timer interrupt handler in the application.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;The advantage of using PPI is that you don&amp;#39;t need to rely on/wait for the CPU in order to trigger the sampling, but it is not necessary to use it.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>