<?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>Timer with two compared values</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/18237/timer-with-two-compared-values</link><description>Hello all, 
 I try to trigger two events with two different periods, the first at 200ms, the second at 300ms. When I set the same period for the compared value, it works correctly, but when I set differents periods (respectively 200 and 300 ms) it didn</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 14 Aug 2017 15:31:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/18237/timer-with-two-compared-values" /><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70434?ContentTypeID=1</link><pubDate>Mon, 14 Aug 2017 15:31:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1fcbec0e-f005-4b19-a7d3-b498fac6cca5</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;@arjunhary: I&amp;#39;m not sure I understand your question, but the code I posted above was not a general example. You will have to modify the example to make it work for other periods. This method will not work for 100ms and 2 seconds, as there is not enough compare channels in the timer and GPIOTE/PPI channels in the chip. Can you solve your problem using app_timer, as suggested in my &lt;a href="https://devzone.nordicsemi.com/question/106152/timer-with-two-compared-values/?comment=106431#comment-106431"&gt;comment below&lt;/a&gt;, or &lt;a href="https://devzone.nordicsemi.com/question/162467/question-on-multiple-channels-in-timers-in-nrf52/?answer=162738#post-id-162738"&gt;the solution proposed by Torbjørn&lt;/a&gt; in this other question you posted?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70433?ContentTypeID=1</link><pubDate>Wed, 09 Aug 2017 00:29:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e74c66a7-8da4-48c4-b077-f437752bb865</guid><dc:creator>arjunhary</dc:creator><description>&lt;p&gt;In the example file for two channels given above, if i change the time to 100ms and 2seconds for the two channels, looks like led0 blinks 100ms for every 2seconds. It does not keep blinking continuously every 100ms. Looks like the multiple channels is only for pwm/sequencing. It is not a solution for the problem below&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Event:          0   1       0   1       0   1     CC
        |---|---|---|---|---|---|---|---|---|---|
Time:   0  100 200 300 400 500 600 700 800 900   [ms]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is my understanding correct?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70436?ContentTypeID=1</link><pubDate>Fri, 09 Dec 2016 09:55:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:98e1333a-410f-4c44-a657-054004d3f43b</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Then I would rather recommend that you take a look at the &lt;a href="https://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v12.2.0/lib_timer.html?cp=4_0_0_3_33"&gt;application timer library&lt;/a&gt;. There is a &lt;a href="https://devzone.nordicsemi.com/tutorials/19/"&gt;great tutorial&lt;/a&gt; to get you started here at DevZone. The application timer library use RTC instead of timers, making it more low-power. Only 1 RTC is used. I have created an example that use three different periods: &lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/app_5F00_timer_5F00_three_5F00_periods.c"&gt;app_timer_three_periods.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70435?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 17:21:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65710e6b-9efd-446d-bc59-27dc764d9a78</guid><dc:creator>acha</dc:creator><description>&lt;p&gt;thank you so much for your help. It works, but for my basic application, I dont want to use ppi, it was only for debug, so now, i try to implement a simple program using timer and 3 compare events with different period (respectively 200, 300 and 400 ms), without using ppi (of corse). So, I implement this program which didn&amp;#39;t work, and the events are triggered at the maximal period of these setted for each event (400 ms in my case). Bellow my code :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
 *
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC
 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 *
 * Licensees are granted free, non-transferable use of the information. NO
 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
 * the file.
 *
 */

/** @file
*
* @defgroup gpiote_example_main main.c
* @{
* @ingroup nrf_gpiote_example
* @brief GPIOTE Example Application main file.
*
* This file contains the source code for a sample application using GPIOTE.
*/

#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nrf_gpiote.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;nrf_drv_ppi.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;
#include &amp;quot;nrf_drv_gpiote.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;

#define PERIOD_0 200
#define PERIOD_1 300
#define PERIOD_2 400

static nrf_drv_timer_t timer = NRF_DRV_TIMER_INSTANCE(0);

void timer_dummy_handler(nrf_timer_event_t event_type, void * p_context)
{
  if (event_type==NRF_TIMER_EVENT_COMPARE0)
  {
          nrf_drv_gpiote_out_toggle(BSP_LED_0);
  }

  if (event_type==NRF_TIMER_EVENT_COMPARE1)
  {
          nrf_drv_gpiote_out_toggle(BSP_LED_1);
  }

  if (event_type==NRF_TIMER_EVENT_COMPARE2)
  {
          nrf_drv_gpiote_out_toggle(BSP_LED_2);
  }
}

static void led_blinking_setup()
{

    ret_code_t err_code;

    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_SIMPLE(false);

    err_code = nrf_drv_gpiote_out_init(BSP_LED_0, &amp;amp;out_config);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_gpiote_out_init(BSP_LED_1, &amp;amp;out_config);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_gpiote_out_init(BSP_LED_2, &amp;amp;out_config);
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_compare(&amp;amp;timer, NRF_TIMER_CC_CHANNEL0, PERIOD_0 * 1000UL, true);
    nrf_drv_timer_compare(&amp;amp;timer, NRF_TIMER_CC_CHANNEL1, PERIOD_1 * 1000UL, true);
    nrf_drv_timer_extended_compare(&amp;amp;timer, NRF_TIMER_CC_CHANNEL2, PERIOD_2 * 1000UL, NRF_TIMER_SHORT_COMPARE2_CLEAR_MASK, true);

}

/**
 * @brief Function for application main entry.
 */
int main(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_ppi_init();
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_gpiote_init();
    APP_ERROR_CHECK(err_code);

    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&amp;amp;timer, &amp;amp;timer_cfg, timer_dummy_handler);
    APP_ERROR_CHECK(err_code);
#ifdef NRF51
    //Workaround for PAN-73.
    *(uint32_t *)0x40008C0C = 1;
#endif

    // Setup PPI channel with event from TIMER compare and task GPIOTE pin toggle.
    led_blinking_setup();

    // Enable timer
    nrf_drv_timer_enable(&amp;amp;timer);

    while (true)
    {
        // Do Nothing - GPIO can be toggled without software intervention.
    }
}


/** @} */
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70432?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 12:58:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:03e917fd-e34c-4899-bf85-41085ae03991</guid><dc:creator>acha</dc:creator><description>&lt;p&gt;Yess, that&amp;#39;s exactly what I want. For your proposed method, as you say, the events will not be happen regulary..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70431?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 12:56:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:96264df0-2680-40e8-bc12-1702cbf41787</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;In this case it can be solved by using 4 compare registers, but this will require more PPI channels. If your periods does not have a clear relationship between them, it might not be this simple. You can also handle this in software, by calculating the next compare value on each event, but I guess you want to do this with as little overhead as possible? &lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/one_5F00_timer_5F00_two_5F00_cc_5F00_example.c"&gt;one_timer_two_cc_example.c&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70430?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 12:37:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:865066bf-7779-4cae-a905-484c0a8fbaf7</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;It is possible to have different compare values, the problem is that you use a function that clears the timer on when the first compare value is matched. You can get these two events to happen on 200 and 300 ms by using:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;nrf_drv_timer_compare(&amp;amp;timer1, NRF_TIMER_CC_CHANNEL0, PERIOD_0 * 1000UL, true);
nrf_drv_timer_extended_compare(&amp;amp;timer1, NRF_TIMER_CC_CHANNEL1, PERIOD_1 * 1000UL, NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then the first compare event will not clear the timer, allowing for the second timer event to happen. The problem with this method is that the timer will be cleared after the second timer event, so you will not get the second event for the first compare value befor 200 ms after the timer is cleared, i.e. at 500 ms:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Event:          0   1       0   1       0   1     CC
        |---|---|---|---|---|---|---|---|---|---|
Time:   0  100 200 300 400 500 600 700 800 900   [ms]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I understand you correctly, you want the events to happen regularly, like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Event:          0   1   0      0/1      0   1     CC
        |---|---|---|---|---|---|---|---|---|---|
Time:   0  100 200 300 400 500 600 700 800 900   [ms]
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70429?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 11:22:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:59b513f0-033c-4987-947f-8f52536ea4da</guid><dc:creator>acha</dc:creator><description>&lt;p&gt;Hello,&lt;/p&gt;
&lt;p&gt;thanks you Jorgen for your answer. I wonder what is the utility to have multiple compare channel in one timer when we can&amp;#39;t use different compare events with differents period? :/
The goal is to use one timer, because i will have a few events a trigger, if i use a lot of timer instances, that&amp;#39;s will not be optimal..
Thank you in advance for your answer!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Timer with two compared values</title><link>https://devzone.nordicsemi.com/thread/70428?ContentTypeID=1</link><pubDate>Thu, 08 Dec 2016 10:25:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7a2edec2-9251-4a9a-814b-34b1c4771d11</guid><dc:creator>J&amp;#248;rgen Holmefjord</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You are using &lt;code&gt;nrf_drv_timer_extended_compare()&lt;/code&gt; with a short, &lt;code&gt;NRF_TIMER_SHORT_COMPAREx_CLEAR_MASK&lt;/code&gt;, to clear the timer. When the compare event of the first period happens, the timer will be cleared, resulting in the second period compare event never happening.&lt;/p&gt;
&lt;p&gt;You should use two different timers, or use the timers in a different way to get the functionality you want. Also, I don&amp;#39;t think it is a good idea to configure the PPI channels inside the timer handler like you have done. I have rewritten your code to work in the way I think you intended:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/two_5F00_timers_5F00_example.c"&gt;two_timers_example.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Jørgen&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>