<?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>ticks to ms calculation</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/7297/ticks-to-ms-calculation</link><description>app_timer.h contains a useful macro for getting from milliseconds to a number of timer ticks, given a prescaler value with which the timer was started: 
 #define APP_TIMER_TICKS(MS, PRESCALER)\
 ((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 28 May 2015 13:35:52 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/7297/ticks-to-ms-calculation" /><item><title>RE: ticks to ms calculation</title><link>https://devzone.nordicsemi.com/thread/25792?ContentTypeID=1</link><pubDate>Thu, 28 May 2015 13:35:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:48583034-26d3-4721-a85d-375278280a62</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;I was already doing a lot with floats, so it&amp;#39;s no big deal. I agree this is not ideal if you&amp;#39;re not already using floats, good point.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ticks to ms calculation</title><link>https://devzone.nordicsemi.com/thread/25791?ContentTypeID=1</link><pubDate>Thu, 28 May 2015 13:30:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d5fae742-f788-479b-959b-a15c05443f2a</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;you imported the floating point emulation library just to do that? if you were using it anyway I guess that&amp;#39;s ok. With a couple of ifs you can decide whether to do the multiplication or division first, or indeed use the uint64_t emulation which is quite fast and won&amp;#39;t overflow. Either works.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ticks to ms calculation</title><link>https://devzone.nordicsemi.com/thread/25790?ContentTypeID=1</link><pubDate>Thu, 28 May 2015 13:26:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:cbb8d6b1-163b-4a80-b94e-467e3639c6ce</guid><dc:creator>Eliot Stock</dc:creator><description>&lt;p&gt;The risk with just rearranging the formula in that macro is that you a) end up trying to use a uint64_t beyond the preprocessor and/or b) end up overflowing a uint32_t at a low number of ticks and getting a bogus result back. I settled on this function.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint32_t app_timer_ms(uint32_t ticks)
{
	// eg. (7 + 1) * 1000 / 32768
	//   = 8000 / 32768
	//   = 0.24414062
	float numerator = ((float)APP_TIMER_PRESCALER + 1.0f) * 1000.0f;
	float denominator = (float)APP_TIMER_CLOCK_FREQ;
	float ms_per_tick = numerator / denominator;

	uint32_t ms = ms_per_tick * ticks;

	return ms;
}
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: ticks to ms calculation</title><link>https://devzone.nordicsemi.com/thread/25789?ContentTypeID=1</link><pubDate>Thu, 28 May 2015 01:38:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fe01e674-1592-4d29-81e9-58e46a06cf68</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;well just re-arrange the formula, ignoring the ROUNDED_DIV() for simplicity&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;APP_TIMER_TICKS = ( MS * APP_TIMER_CLOCK_FREQ ) / ( ( PRESCALAR + 1 ) * 1000 )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;so&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;MS = APP_TIMER_TICKS * ( ( PRESCALAR + 1 ) * 1000 ) / APP_TIMER_CLOCK_FREQ
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;m sure you can turn that into a define, not that it really helps you that much as the number of ticks will be in a variable so the compiler can&amp;#39;t evaluate it and compile it out into a constant which is what happens most of the time with the current macro as usually everything on the right hand side is a compile-time constant.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>