<?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>Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/4296/problem-running-a-10khz-pwm-on-a-timer</link><description>Hi, 
 I am trying to obtain a 10khz PWM (50%) on an output using Timer2 (see code below). It is working fine up to around 500Hz (2msec), above that (1kHz) I am getting only sporadic PWM bursts, PWM will run up to advertising then will stop, then timer</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 04 Nov 2014 16:45:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/4296/problem-running-a-10khz-pwm-on-a-timer" /><item><title>RE: Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/thread/15312?ContentTypeID=1</link><pubDate>Tue, 04 Nov 2014 16:45:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fd6a46dc-8533-4ee2-95c9-af896803e4c4</guid><dc:creator>Phil</dc:creator><description>&lt;p&gt;Hello Stefan,&lt;/p&gt;
&lt;p&gt;Thanks for the link.
One more thing though, I did manage to get the PWM on compare 0 to work fine, however I tried in parallel to run an interrupt on Compare 1 every 10msec and that does not work. The interrupt is not entered and my PWM is 20msec.
What am I getting wrong?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Init TIMER2 for 10kHz PWM
NRF_TIMER2-&amp;gt;MODE = TIMER_MODE_MODE_Timer;           // Set the timer in Counter Mode
NRF_TIMER2-&amp;gt;TASKS_CLEAR = 1;                        // Clear the tasks first to be usable for later
NRF_TIMER2-&amp;gt;PRESCALER = 4;                          // 2^PRESCALER (values authorized 1 to 9) 4 : 16MHz / 16 = 10MHz
NRF_TIMER2-&amp;gt;BITMODE = TIMER_BITMODE_BITMODE_16Bit;  //Set counter to 16 bit resolution

// Init OC0 PWM
NRF_TIMER2-&amp;gt;CC[0] = 50;
PWM_TIMER-&amp;gt;SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos);

// Enable OC1 interrupt on TIMER2
NRF_TIMER2-&amp;gt;CC[1] = 10000;
PWM_TIMER-&amp;gt;SHORTS = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE1_CLEAR_Pos);
NVIC_EnableIRQ(TIMER2_IRQn);

// Start TIMER2
NRF_TIMER2-&amp;gt;TASKS_START = true;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Interrupt :&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void TIMER2_IRQHandler(void)
{

    if (NRF_TIMER2-&amp;gt;EVENTS_COMPARE[1] != 0)
    {
        NRF_TIMER2-&amp;gt;EVENTS_COMPARE[1] = 0; // Reset event

       // Code to execute

    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Regards
Phil&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/thread/15310?ContentTypeID=1</link><pubDate>Tue, 04 Nov 2014 14:00:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f0240436-1684-4a0b-af1a-09139d37dea9</guid><dc:creator>Stefan Birnir Sverrisson</dc:creator><description>&lt;p&gt;Hi Phil.
If you feel that you have received a good answer to you question, please accept that answer. Also remember to vote for good answers. Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/thread/15311?ContentTypeID=1</link><pubDate>Tue, 04 Nov 2014 13:59:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a6bdcaa3-463e-4106-a4fb-dad68c756a52</guid><dc:creator>Stefan Birnir Sverrisson</dc:creator><description>&lt;p&gt;For further PWM code reference, there is a PWM library available &lt;a href="https://github.com/NordicSemiconductor/nrf51-pwm-library"&gt;here&lt;/a&gt; for nRF51.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/thread/15308?ContentTypeID=1</link><pubDate>Tue, 04 Nov 2014 13:56:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76233082-8afe-45e5-886f-bf2a127dfc99</guid><dc:creator>Stefan Birnir Sverrisson</dc:creator><description>&lt;p&gt;Thank you for your valuable input Tony.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/thread/15309?ContentTypeID=1</link><pubDate>Mon, 03 Nov 2014 16:48:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bac95ccb-ba90-4055-bb98-0be4d2d63e96</guid><dc:creator>Phil</dc:creator><description>&lt;p&gt;Hi Tony,&lt;/p&gt;
&lt;p&gt;Great thanks. Just the thing I needed to know. Works great now!&lt;/p&gt;
&lt;p&gt;Thanks for the help&lt;/p&gt;
&lt;p&gt;Phil&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Problem running a 10kHz PWM on a Timer</title><link>https://devzone.nordicsemi.com/thread/15307?ContentTypeID=1</link><pubDate>Mon, 03 Nov 2014 16:31:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:31903d93-7074-411c-a9b5-f7b30d115eb7</guid><dc:creator>Tony.X</dc:creator><description>&lt;p&gt;Hi Phil,&lt;/p&gt;
&lt;p&gt;You can totally get rid of the interrupt handler if you don&amp;#39;t want to change the desired frequency on the fly.&lt;/p&gt;
&lt;p&gt;This can be done by setting the SHORT register.&lt;/p&gt;
&lt;p&gt;Add one more line in Timer init function,&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;PWM_TIMER-&amp;gt;SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled &amp;lt;&amp;lt; TIMER_SHORTS_COMPARE0_CLEAR_Pos);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This enables the timer to clear the internal counter to 0 when it reaches CC[0].&lt;/p&gt;
&lt;p&gt;Hope this will help.&lt;/p&gt;
&lt;p&gt;Tony&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>