<?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 in counter mode</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/33764/timer-in-counter-mode</link><description>How to start the timer in counter mode in nrf52 and how to count the number of pulses received in a pin ? Thank you</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 26 Apr 2018 14:53:39 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/33764/timer-in-counter-mode" /><item><title>RE: Timer in counter mode</title><link>https://devzone.nordicsemi.com/thread/129999?ContentTypeID=1</link><pubDate>Thu, 26 Apr 2018 14:53:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fc7732ee-b83a-42ab-b8a3-69b728a566fc</guid><dc:creator>Edvin</dc:creator><description>&lt;p&gt;You should look into the PPI.&lt;/p&gt;
&lt;p&gt;Here is an example, which uses the PPI. It uses a timer to toggle a GPIO, but you can change the GPIO to &amp;quot;event&amp;quot; instead of &amp;quot;task&amp;quot;, and use the timer in counter mode. The timer can be configured something like this:&lt;/p&gt;
&lt;p&gt;void timer_init()&lt;br /&gt;{&lt;br /&gt; NRF_TIMER3-&amp;gt;BITMODE = TIMER_BITMODE_BITMODE_24Bit &amp;lt;&amp;lt; TIMER_BITMODE_BITMODE_Pos;&lt;br /&gt; NRF_TIMER3-&amp;gt;PRESCALER = 0;&lt;br /&gt; NRF_TIMER3-&amp;gt;SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk &amp;lt;&amp;lt; TIMER_RELOAD_CC_NUM;&lt;br /&gt; NRF_TIMER3-&amp;gt;MODE = TIMER_MODE_MODE_Counter &amp;lt;&amp;lt; TIMER_MODE_MODE_Pos;&lt;br /&gt; NRF_TIMER3-&amp;gt;CC[TIMER_RELOAD_CC_NUM] = TIMER_RELOAD; &lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then you need to start the timer with:&lt;/p&gt;
&lt;p&gt;NRF3_TIMER3-&amp;gt;TASKS_START = 1;&lt;/p&gt;
&lt;p&gt;Then configure the GPIO like in the example, and set a PPI channel something like the following:&lt;/p&gt;
&lt;p&gt;NRF_PPI-&amp;gt;CH[PPI_CH_1].EEP = (uint32_t)&amp;amp;NRF_GPIOTE-&amp;gt;EVENTS_IN[GPIOTE_CH];&lt;/p&gt;
&lt;p&gt;NRF_PPI-&amp;gt;CH[PPI_CH_1].TEP = (uint32_t)&amp;amp;NRF_TIMER3-&amp;gt;TASKS_COUNT;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Then you can get the count value by checking the timer value:&lt;/p&gt;
&lt;p&gt;NRF_TIMER3-&amp;gt;TASKS_CAPTURE[0];&lt;/p&gt;
&lt;p&gt;and check the value with:&lt;/p&gt;
&lt;p&gt;uint32_t value = NRF_TIMER3-&amp;gt;CC[0];&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;you can also reset the counter using:&lt;/p&gt;
&lt;p&gt;NRF_TIMER3-&amp;gt;TASKS_CLEAR;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have not tested this right now, so the calls are from my memory, but it should look something like this.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Edvin&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>