<?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>Is there an code example for using a timer in count mode with compare?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/123490/is-there-an-code-example-for-using-a-timer-in-count-mode-with-compare</link><description>Hi i am trying to configure a timer to work in count mode and when it hits a certain number ( for example 100) to activeta an interrupt, clear count and start again. 
 Best regards.</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 08 Aug 2025 11:16:33 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/123490/is-there-an-code-example-for-using-a-timer-in-count-mode-with-compare" /><item><title>RE: Is there an code example for using a timer in count mode with compare?</title><link>https://devzone.nordicsemi.com/thread/545044?ContentTypeID=1</link><pubDate>Fri, 08 Aug 2025 11:16:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:85e9846d-86fb-4304-9a7a-832042bf10aa</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Thank you, marking it as verified.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Is there an code example for using a timer in count mode with compare?</title><link>https://devzone.nordicsemi.com/thread/545041?ContentTypeID=1</link><pubDate>Fri, 08 Aug 2025 11:06:14 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:69c110d5-2e5b-4f71-9eb1-0518e746cba5</guid><dc:creator>Petar02</dc:creator><description>&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;ISR_DIRECT_DECLARE(trig_cnt_irq_handler)
{
	// Half way point
	if(SPI_TRIG_COUNTER-&amp;gt;EVENTS_COMPARE[0]) {
		SPI_TRIG_COUNTER-&amp;gt;EVENTS_COMPARE[0] = 0;
		
		m_item_num = LIST_ITEMS / 2;
		m_item_index = 0;
		k_sem_give(&amp;amp;m_sem_samples_available);
	}	

	// End of buffer (not including margin)
	if(SPI_TRIG_COUNTER-&amp;gt;EVENTS_COMPARE[1]) {
		SPI_TRIG_COUNTER-&amp;gt;EVENTS_COMPARE[1] = 0;

		// If there is a risk that this interrupt gets delayed we need to handle the case where more items have been sampled than expected
		// trigger a capture on the counter to check how far the count has reached
		// TODO: Possibly we would also need to check the state of the timer to check if is just about to trigger another sample, which could lead to a race condition
		SPI_TRIG_COUNTER-&amp;gt;TASKS_CAPTURE[3] = 1;
		m_item_num = SPI_TRIG_COUNTER-&amp;gt;CC[3] - (LIST_ITEMS / 2);
		m_item_index = LIST_ITEMS / 2;
		SPI_MASTER-&amp;gt;TXD.PTR = (uint32_t)m_tx_buffer[0];
		SPI_MASTER-&amp;gt;RXD.PTR = (uint32_t)m_rx_buffer[0];
		SPI_TRIG_COUNTER-&amp;gt;TASKS_CLEAR = 1;
		k_sem_give(&amp;amp;m_sem_samples_available);
	}

	ISR_DIRECT_PM();
	return 1;
}


static void trig_counter_init(void)
{
	SPI_TRIG_COUNTER-&amp;gt;MODE = TIMER_MODE_MODE_Counter &amp;lt;&amp;lt; TIMER_MODE_MODE_Pos;
	SPI_TRIG_COUNTER-&amp;gt;CC[0] = LIST_ITEMS / 2;
	SPI_TRIG_COUNTER-&amp;gt;CC[1] = LIST_ITEMS;
	SPI_TRIG_COUNTER-&amp;gt;INTENSET = TIMER_INTENSET_COMPARE0_Msk | TIMER_INTENSET_COMPARE1_Msk;
	SPI_TRIG_COUNTER-&amp;gt;TASKS_CLEAR = 1;
	SPI_TRIG_COUNTER-&amp;gt;TASKS_START = 1;

    IRQ_DIRECT_CONNECT(TIMER2_IRQn, IRQ_PRIO_LOWEST, trig_cnt_irq_handler, 0);
	irq_enable(TIMER2_IRQn);

	LOG_INF(&amp;quot;timer konfigurisan&amp;quot;);
}&lt;/pre&gt;Here it is.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Is there an code example for using a timer in count mode with compare?</title><link>https://devzone.nordicsemi.com/thread/544910?ContentTypeID=1</link><pubDate>Thu, 07 Aug 2025 10:46:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:65bb3933-8760-4179-ab47-aeae0df11c9d</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Petar, would be nice if you talk a bit more about the solution to help the fellow forum users.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Is there an code example for using a timer in count mode with compare?</title><link>https://devzone.nordicsemi.com/thread/544750?ContentTypeID=1</link><pubDate>Wed, 06 Aug 2025 10:04:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:604b68c9-82e3-4bf8-b0ba-9cc620ca75ff</guid><dc:creator>Petar02</dc:creator><description>&lt;p&gt;I found a solution&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>