<?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>Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69859/basic-nrf-timer-usage</link><description>So what I understand from the lesson over here is that we have couple of timers available to us and each timer comprise of multiple compare/capture register which can be used to generate and service an interrupt when the value of CC register is equal</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 14 Jan 2021 08:36:25 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69859/basic-nrf-timer-usage" /><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/289086?ContentTypeID=1</link><pubDate>Thu, 14 Jan 2021 08:36:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e8ff53c9-0af9-4584-89e9-bf68c6c994da</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;There is only RTC1 used for ticks generation. You can refer to discussion &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/10952/timer-vs-app_timer"&gt;timer-vs-app_timer&lt;/a&gt;&amp;nbsp;to learn more about their difference.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/289043?ContentTypeID=1</link><pubDate>Thu, 14 Jan 2021 00:15:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f889a52d-a3a7-4a5b-8b41-cbcaf327976c</guid><dc:creator>nordicuser91</dc:creator><description>&lt;p&gt;So while using app timer and creating two instances do we initialize two different RTCs or two different timing peripheral?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/288327?ContentTypeID=1</link><pubDate>Mon, 11 Jan 2021 09:59:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6862a9d-25a7-46fd-8e50-f47634f3b39c</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;The app timers are generated from RTC ticks, so it is different from the timer peripherals. Even so, your example still uses two app timer instances because the two led toggle at two different frequencies.&lt;/p&gt;
&lt;p&gt;If you have an application that two led toggle at the same frequency but have different duties, you can use one timer with two different comparison value.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/288257?ContentTypeID=1</link><pubDate>Sat, 09 Jan 2021 07:32:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5678845d-5fe6-4c34-920a-b1cf9d92c2e2</guid><dc:creator>nordicuser91</dc:creator><description>&lt;p&gt;Ok so my understanding is that we can have two different values in two different compare registers in that timer and the interrupt handler would executed. Having different cases in the interrupt handler for each compare event. we can have two led toggled at different frequencies taking into account that the we reset the timer only after the event which takes larger time.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This issue was solved by using app timer as shown in the following code.&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nordic_common.h&amp;quot;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_timer.h&amp;quot;
#include &amp;quot;nrf_drv_clock.h&amp;quot;
#include &amp;quot;nrf_gpio.h&amp;quot;

// LED pin 
#define LED_Pin1 17
#define LED_Pin2 18

// use a function to calculate the number of tick needed to 
// create a delay of 100 millisec and store this value in LED_INTERVAL constant
#define LED_INTERVAL APP_TIMER_TICKS(20)
#define LED1_INTERVAL APP_TIMER_TICKS(50)



// For each application timer we need to create a handle which points to that instance
// so for evey application timer (Software timer) you create, you need to define a handle
// create a handle and name it as:  m_app_timer_id 
APP_TIMER_DEF(m_app_timer_id);
APP_TIMER_DEF(m_app_timer_id1);



// its really important to initialize the clock other wise the app timer will not work
// so make sure you initialize it once in your code and if you are using a soft device
// then you don&amp;#39;t need to initialize the clock as the soft device automatically initializes it.
static void lfclk_config(void)
{
	// initialize the low power low frequency clock
  ret_code_t err_code = nrf_drv_clock_init();
  APP_ERROR_CHECK(err_code);

	// request the lf clock to not to generate any events on ticks
	// One tick =  1 value increment in the counter register
  nrf_drv_clock_lfclk_request(NULL);

}






// create a simple handler function which will be called once the timer reaches its 
// desired number of ticks value
static void app_timer_handler(void * p_context)
{
	// Toggle the LED 
    nrf_gpio_pin_toggle(LED_Pin1);
}
  
  static void app_timer_handler1(void * p_context)
{
	// Toggle the LED 
    nrf_gpio_pin_toggle(LED_Pin2);
}



// a function to initialize the Application timers
static void timers_init(void)
{
	// a variable to hold error value
    ret_code_t err_code;

	// Initialize the timer 
    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

	// Create an application timer with the handle, mode and interrupt event handle function
    err_code = app_timer_create(&amp;amp;m_app_timer_id, APP_TIMER_MODE_REPEATED, app_timer_handler);
    APP_ERROR_CHECK(err_code);
    
    err_code = app_timer_create(&amp;amp;m_app_timer_id1, APP_TIMER_MODE_REPEATED, app_timer_handler1);
}





int main(void)
{

// initialize the gpio for led
    nrf_gpio_cfg_output(LED_Pin1);
    nrf_gpio_cfg_output(LED_Pin2);

// call the function to initialize the clock
    lfclk_config();
// initialize the timer by calling this function which is performing all the basic steps
    timers_init();

// start the timer so that it can generate events on the desired tick value
    uint32_t err_code = app_timer_start(m_app_timer_id, LED_INTERVAL, NULL);
    uint32_t err_code1 = app_timer_start(m_app_timer_id1, LED1_INTERVAL, NULL);


    while (true)
    {
        // Do nothing.
    }
}





&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The app timer uses only single timer to toggle to LEDs on different frequencies. My aim is to achieve this same functionality without using app timer but by using timer library.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/287681?ContentTypeID=1</link><pubDate>Wed, 06 Jan 2021 10:50:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a404153e-665d-4e2a-a8ee-20cbb7500446</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Purvesh,&lt;/p&gt;
&lt;p&gt;This is not an issue. It is just how the timer compare channels interrupt events work. It&amp;nbsp;fits for applications that have different events happen in the same repeating interval. You should use two timers if the two events have different repeating frequencies.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/287602?ContentTypeID=1</link><pubDate>Tue, 05 Jan 2021 23:58:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:de9e2f01-7788-42f7-840a-8b8f206c2a5d</guid><dc:creator>nordicuser91</dc:creator><description>[quote userid="93921" url="~/f/nordic-q-a/69859/basic-nrf-timer-usage/286965#286965"]The codes have no issue,&amp;nbsp;but I think this is not what you expected and you expect LED1 toggles every 100ms and LED2 toggles every 1000ms. In the above code, LED1 and LED2 will toggle every 1000ms, and LED1 toggles&amp;nbsp;900ms early than LED2. Keep in mind the two channels compare with the same value in Counter.&amp;nbsp;[/quote]
&lt;p&gt;This is the thing which I am trying to implement. Not PWM. Can you please share an example code which might help me solve this issue?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/286965?ContentTypeID=1</link><pubDate>Wed, 30 Dec 2020 09:40:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f27b6f75-1b61-49bc-9c83-f9d201ed38a4</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;The codes have no issue,&amp;nbsp;but I think this is not what you expected and you expect LED1 toggles every 100ms and LED2 toggles every 1000ms. In the above code, LED1 and LED2 will toggle every 1000ms, and LED1 toggles&amp;nbsp;900ms early than LED2. Keep in mind the two channels compare with the same value in Counter.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If your purpose is to generate PWM waves on multiple channels, I suggest you use PWM peripheral. It is possible to use Timer but I think PWM is a better solution. Timer comparison channels are more likely to handle different tasks in one of the repeating periods.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/286897?ContentTypeID=1</link><pubDate>Wed, 30 Dec 2020 01:54:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f52b2603-b234-45bd-a65b-6e097bd71a6f</guid><dc:creator>nordicuser91</dc:creator><description>&lt;p&gt;How about the following code?&lt;pre class="ui-code" data-mode="c_cpp"&gt;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;bsp.h&amp;quot;
#include &amp;quot;nrf.h&amp;quot;
#include &amp;quot;nrf_drv_timer.h&amp;quot;
#include &amp;lt;stdbool.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;


const nrfx_timer_t TIMER_LED = NRFX_TIMER_INSTANCE(0); // Timer 0 Enabled


#define LED1 17
#define LED2 18
    


void timer0_handler(nrf_timer_event_t event_type, void *p_context)
{
    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
            nrf_gpio_pin_toggle(LED1);
            break;

        case NRF_TIMER_EVENT_COMPARE1:
            nrf_gpio_pin_toggle(LED2);
            nrfx_timer_clear(&amp;amp;TIMER_LED);
            break;
            
        default:
            break;
    }
          
}


void timer_init(void)
{
    uint32_t err_code = NRF_SUCCESS;

    uint32_t time_ms = 100;
    uint32_t time_ms1 = 1000;

    uint32_t time_ticks;
    uint32_t time_ticks1;


    nrfx_timer_config_t timer_cfg =
        NRFX_TIMER_DEFAULT_CONFIG; // Configure the timer instance to default settings


    err_code = nrfx_timer_init(&amp;amp;TIMER_LED, &amp;amp;timer_cfg, timer0_handler); // Initialize the timer0 with default settings
    APP_ERROR_CHECK(err_code);                   // check if any error occured


    time_ticks = nrfx_timer_ms_to_ticks(&amp;amp;TIMER_LED, time_ms); // convert ms to ticks
    time_ticks1 = nrfx_timer_ms_to_ticks(&amp;amp;TIMER_LED, time_ms1);

    // Assign a channel, pass the number of ticks &amp;amp; enable interrupt
    nrfx_timer_compare(&amp;amp;TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, true);
    nrfx_timer_compare(&amp;amp;TIMER_LED, NRF_TIMER_CC_CHANNEL1, time_ticks1, true);
}

/**
 * @brief Function for main application entry.
 */
int main(void)
{

    nrf_gpio_cfg_output(LED1); // Initialize the pin
    nrf_gpio_cfg_output(LED2); // Initialize the pin

    nrf_gpio_pin_set(LED1); // Turn off the LED
    nrf_gpio_pin_set(LED2); // Turn off the LED

    timer_init();

    nrfx_timer_enable(&amp;amp;TIMER_LED);


    while (1)
    {
        __WFI(); // GO INTO LOW POWER MODE
    }
}
&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/286891?ContentTypeID=1</link><pubDate>Tue, 29 Dec 2020 22:53:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46fdef47-1353-4b4e-b885-6ae882e2f009</guid><dc:creator>nordicuser91</dc:creator><description>&lt;p&gt;Hi Charlie,&lt;/p&gt;
&lt;p&gt;Thanks for the link. Do you have any example code which could help me understand this?&lt;/p&gt;
&lt;p&gt;Regards&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Basic nRF timer usage</title><link>https://devzone.nordicsemi.com/thread/286819?ContentTypeID=1</link><pubDate>Tue, 29 Dec 2020 10:47:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ad961f4f-dcae-4a59-9cce-2518f8057d06</guid><dc:creator>Charlie</dc:creator><description>&lt;p&gt;Hi Purvesh,&lt;/p&gt;
&lt;p&gt;Sorry for the late reply. The simple answer is that timmer&amp;nbsp;&lt;span&gt;reset on the first CC register so you never reach the second.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;See a similar case in this thread:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/24177/question-on-multiple-channels-in-timers-in-nrf52?ReplySortBy=CreatedDate&amp;amp;ReplySortOrder=Ascending"&gt;(+) Nordic DevZone (nordicsemi.com)&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Charlie&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>