<?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>Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/2010/setting-timer2-interval</link><description>How do I set TIMER2 to interrupt every 1 ms? 
 Sounds like a very basic question, but I haven&amp;#39;t succeed. For TIMER1 I can get it working, but for TIMER2 the highest frequecy I have achieved is about 100 Hz. 
 The code is as follows: 
 void init_timer2</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 03 Apr 2017 17:43:23 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/2010/setting-timer2-interval" /><item><title>RE: Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/thread/8613?ContentTypeID=1</link><pubDate>Mon, 03 Apr 2017 17:43:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2059c36f-c32c-4d17-b822-1e9ce97ac0ab</guid><dc:creator>Craig</dc:creator><description>&lt;p&gt;Hello, I am struggling with timers in general, I so happen to use TIMER2 as well with my BLE UART app all I need is a way to generate 1sec and in another similar app 2 ms, 2 versions of HEX, how can I generate this? I seem to be calculating it wrong according to my scope output, this example was not anywhere near 1 ms and neither is the math in the comments.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/thread/8612?ContentTypeID=1</link><pubDate>Tue, 13 May 2014 10:48:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c6aed02b-5ba6-4009-9377-b49a6c15cca6</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;The reason this must be there is because the timer is not cleared between each interrupt. When you&amp;#39;ve finished your first interrupt, the timer will keep on incrementing until it overflows.
You then have two options:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Keep updating the CC-register&lt;/li&gt;
&lt;li&gt;Clear the timer between each interrupt.
This can be done manually, or via SHORTS register.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Best regards
Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/thread/8611?ContentTypeID=1</link><pubDate>Mon, 12 May 2014 12:15:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:773245ad-76e1-4be1-9a8e-bd716f54eac1</guid><dc:creator>Mo A</dc:creator><description>&lt;p&gt;Hello Håkon,
Thanks for this.
I was able to start working with the timer by referring this code. But I have a question that  whether the line, NRF_TIMER2-&amp;gt;CC[0] += TIMER2_CC_DELAY; can be removed here ? But when I tried that , the toggling was not proper. why is this so?&lt;/p&gt;
&lt;p&gt;thanks,
Mo&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/thread/8610?ContentTypeID=1</link><pubDate>Fri, 28 Mar 2014 09:42:58 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e29c0a86-8f4a-43dc-97d5-8c7f746f4e6e</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi Jarmo,&lt;/p&gt;
&lt;p&gt;It&amp;#39;s most likely because you enabled CC[2]:
NRF_TIMER2-&amp;gt;INTENSET = TIMER_INTENSET_COMPARE2_Enabled
Enabling CC[0] is done like this:
NRF_TIMER2-&amp;gt;INTENSET = TIMER_INTENSET_COMPARE0_Enabled&lt;/p&gt;
&lt;p&gt;BR
Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/thread/8609?ContentTypeID=1</link><pubDate>Fri, 28 Mar 2014 09:36:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0b8579b2-e9b9-46da-a605-199239213462</guid><dc:creator>Jarmo</dc:creator><description>&lt;p&gt;For some reason, I had to use CC[2] instead of CC[0] to get it working. Could it has something to do with S110 soft device which I am using at the same time? Anyway, thanks!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Setting TIMER2 interval</title><link>https://devzone.nordicsemi.com/thread/8608?ContentTypeID=1</link><pubDate>Thu, 27 Mar 2014 16:12:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3daa5e8b-9a04-4e2c-a287-ff7f87554445</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The prescaler sets the base timer tick like this (given in us):
TimerTick = 16M / 2^prescaler.&lt;/p&gt;
&lt;p&gt;For instance, if we set 4:
TimerTick = 16M/2^4 = 1 us.&lt;/p&gt;
&lt;p&gt;In order to generate an interrupt each 1ms:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
#define TIMER2_CC_DELAY 1000
...
NRF_TIMER2-&amp;gt;PRESCALER = 4;
NRF_TIMER2-&amp;gt;CC[0] = TIMER2_CC_DELAY; // Fbase = 16M/2^4 * 1000 = 1 ms.
/*Reset of init*/

&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In your handler:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;
void TIMER2_IRQHandler(void)
{    
    if(NRF_TIMER2-&amp;gt;EVENTS_COMPARE[0])
    {
        nrf_gpio_pin_toggle(18);
        NRF_TIMER2-&amp;gt;EVENTS_COMPARE[0] = 0;
        // Need to update the CC[0]
        NRF_TIMER2-&amp;gt;CC[0] += TIMER2_CC_DELAY;
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For a more precise measurement you can add a static variable inside the interrupt handler to count instead of reading the CC[]-register.&lt;/p&gt;
&lt;p&gt;Best regards
Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>