<?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>RTC compare event</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/87513/rtc-compare-event</link><description>I am testing the RTC example in nRF5 SDK v17.1.0. 
 This example shows LED1 switching at a rate of 8 Hz and LED2 lighting up after 3 seconds. 
 This is confirmed. 
 The programme shows that the action after 3 seconds is performed by a compare event interrupt</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 04 May 2022 08:09:20 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/87513/rtc-compare-event" /><item><title>RE: RTC compare event</title><link>https://devzone.nordicsemi.com/thread/366113?ContentTypeID=1</link><pubDate>Wed, 04 May 2022 08:09:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6e7072f-3874-4efa-ad3f-c5db3ba62058</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
[quote user="TML_YT"]The result was the same as when I did it, only one time into the &amp;#39;if (int_type == NRF_DRV_RTC_INT_COMPARE0)&amp;#39; interrupt.&lt;br /&gt;Thanks to the &amp;#39;err code&amp;#39; I could see that it was stopping at &amp;#39;NRF_BREAKPOINT_COND&amp;#39; in &amp;#39;app_error_weak.c&amp;#39;, but the cause was not clear to me.[/quote]
&lt;p&gt;Strange, I tested this method and it worked for me. Could share the error message and check the return value from&amp;nbsp;&lt;span&gt;nrf_drv_rtc_cc_set()?&lt;/span&gt;&lt;/p&gt;
[quote user="TML_YT"]Regarding the second example.&lt;br /&gt;This method worked.&lt;br /&gt;I would like to try to apply this method to change the repetition cycle and send packets in that cycle. By the way, when transmitting, I plan to use ESB for the protocol with low consumption.[/quote]
&lt;p&gt;Good, then you have a working alternative &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;regards&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;span&gt;Jared&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC compare event</title><link>https://devzone.nordicsemi.com/thread/366072?ContentTypeID=1</link><pubDate>Tue, 03 May 2022 20:24:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9e55b41d-2e1e-4c0a-9d0a-d22d2b8e67f8</guid><dc:creator>TML_YT</dc:creator><description>&lt;p&gt;Reply, thank you.&lt;/p&gt;
&lt;p&gt;Regarding the first example.&lt;br /&gt;Regarding clearing the counter in the interrupt and resetting the compare interrupt, I was trying the same code (two lines of the following).&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;nrf_drv_rtc_counter_clear(&amp;amp;rtc);&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;int err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc,0,COMPARE_COUNTERTIME * 8,true);&lt;br /&gt;I just did not have the &amp;#39;APP_ERROR_CHECK(err_code)&amp;#39; statement.&lt;/p&gt;
&lt;p&gt;The result was the same as when I did it, only one time into the &amp;#39;if (int_type == NRF_DRV_RTC_INT_COMPARE0)&amp;#39; interrupt.&lt;br /&gt;Thanks to the &amp;#39;err code&amp;#39; I could see that it was stopping at &amp;#39;NRF_BREAKPOINT_COND&amp;#39; in &amp;#39;app_error_weak.c&amp;#39;, but the cause was not clear to me.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regarding the second example.&lt;br /&gt;This method worked.&lt;br /&gt;I would like to try to apply this method to change the repetition cycle and send packets in that cycle. By the way, when transmitting, I plan to use ESB for the protocol with low consumption.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: RTC compare event</title><link>https://devzone.nordicsemi.com/thread/365986?ContentTypeID=1</link><pubDate>Tue, 03 May 2022 12:36:07 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4c68db9a-edf8-4002-b374-12ee79f8d414</guid><dc:creator>Jared</dc:creator><description>&lt;p&gt;Hi there,&lt;/p&gt;
&lt;p&gt;You need to clear the counter and reset the compare interrupt like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;/**
 * Copyright (c) 2014 - 2021, 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 rtc_example_main main.c
 * @{
 * @ingroup rtc_example
 * @brief Real Time Counter Example Application main file.
 *
 * This file contains the source code for a sample application using the Real Time Counter (RTC).
 *
 */

#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;nrf_drv_rtc.h&amp;quot;
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;stdbool.h&amp;gt;

#define COMPARE_COUNTERTIME  (3UL)                                        /**&amp;lt; Get Compare event COMPARE_TIME seconds after the counter starts from 0. */

#ifdef BSP_LED_0
    #define TICK_EVENT_OUTPUT     BSP_LED_0                                 /**&amp;lt; Pin number for indicating tick event. */
#endif
#ifndef TICK_EVENT_OUTPUT
    #error &amp;quot;Please indicate output pin&amp;quot;
#endif
#ifdef BSP_LED_1
    #define COMPARE_EVENT_OUTPUT   BSP_LED_1                                /**&amp;lt; Pin number for indicating compare event. */
#endif
#ifndef COMPARE_EVENT_OUTPUT
    #error &amp;quot;Please indicate output pin&amp;quot;
#endif

const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(0); /**&amp;lt; Declaring an instance of nrf_drv_rtc for RTC0. */

/** @brief: Function for handling the RTC0 interrupts.
 * Triggered on TICK and COMPARE0 match.
 */
static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    if (int_type == NRF_DRV_RTC_INT_COMPARE0)
    {
        nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
        nrf_drv_rtc_counter_clear(&amp;amp;rtc);
        int err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc,0,COMPARE_COUNTERTIME * 8,true);
        APP_ERROR_CHECK(err_code);
     
    }
    else if (int_type == NRF_DRV_RTC_INT_TICK)
    {
        nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
    }
}

/** @brief Function configuring gpio for pin toggling.
 */
static void leds_config(void)
{
    bsp_board_init(BSP_INIT_LEDS);
}

/** @brief Function starting the internal LFCLK XTAL oscillator.
 */
static void lfclk_config(void)
{
    ret_code_t err_code = nrf_drv_clock_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request(NULL);
}

/** @brief Function initialization and configuration of RTC driver instance.
 */
static void rtc_config(void)
{
    uint32_t err_code;

    //Initialize RTC instance
    nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
    config.prescaler = 4095;
    err_code = nrf_drv_rtc_init(&amp;amp;rtc, &amp;amp;config, rtc_handler);
    APP_ERROR_CHECK(err_code);

    //Enable tick event &amp;amp; interrupt
    nrf_drv_rtc_tick_enable(&amp;amp;rtc,true);

    //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
    err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc,0,COMPARE_COUNTERTIME * 8,true);
    APP_ERROR_CHECK(err_code);

    //Power on RTC instance
    nrf_drv_rtc_enable(&amp;amp;rtc);
}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    leds_config();

    lfclk_config();

    rtc_config();

    while (true)
    {
        __SEV();
        __WFE();
        __WFE();
    }
}


/**  @} */
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Alternatively:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
{
    static int counter = 2;
    if (int_type == NRF_DRV_RTC_INT_COMPARE0)
    {
        nrf_gpio_pin_toggle(COMPARE_EVENT_OUTPUT);
        //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
        int err_code = nrf_drv_rtc_cc_set(&amp;amp;rtc,0,((COMPARE_COUNTERTIME * 8)*counter) % 0x1000000,true);
        APP_ERROR_CHECK(err_code);
        counter++;
    }
    else if (int_type == NRF_DRV_RTC_INT_TICK)
    {
        nrf_gpio_pin_toggle(TICK_EVENT_OUTPUT);
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;regards&lt;/p&gt;
&lt;p&gt;Jared&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>