<?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>how to implement accurate milisecond function</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/4840/how-to-implement-accurate-milisecond-function</link><description>I am new to nrf51822.
I am trying to convert my Arduino code into NRF51822 
 I need something like this 
 currentMillis = micros(); //microseconds since the Arduino board began running
if(currentMillis-past_time&amp;gt;=1000)
{
do something
} 
 What</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 18 Dec 2014 16:43:00 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/4840/how-to-implement-accurate-milisecond-function" /><item><title>RE: how to implement accurate milisecond function</title><link>https://devzone.nordicsemi.com/thread/17104?ContentTypeID=1</link><pubDate>Thu, 18 Dec 2014 16:43:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d491c2c-df68-46e2-bd7d-b9c38e878d98</guid><dc:creator>Matt Barr</dc:creator><description>&lt;p&gt;Indeed, 512s range and roughly 30.5 us per tick is what we have with the bare 24-bit RTC at 32.768 kHz.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to implement accurate milisecond function</title><link>https://devzone.nordicsemi.com/thread/17103?ContentTypeID=1</link><pubDate>Wed, 17 Dec 2014 23:50:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8c89e16d-514f-45ea-bc2d-314f25b64aa7</guid><dc:creator>Clem Taylor</dc:creator><description>&lt;p&gt;My problem was the 24 bit counter with a 32khz clock rolls over every 512s. I needed to keep track of time for the power on life of the device. You divide the 32khz down but it depends on how much resolution you need.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to implement accurate milisecond function</title><link>https://devzone.nordicsemi.com/thread/17102?ContentTypeID=1</link><pubDate>Wed, 17 Dec 2014 23:20:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dfb156f2-f153-4436-82f5-d494a0b91f88</guid><dc:creator>Matt Barr</dc:creator><description>&lt;p&gt;Here&amp;#39;s another solution using Nordic&amp;#39;s app_timer API to RTC1.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint32_t watchdog_timer;
uint32_t app_timer_cur;
uint32_t app_timer_diff;

/* Set watchdog timer */
app_timer_cnt_get(&amp;amp;watchdog_timer);

/* Other processing occurs... */

/* Check for a watchdog timeout */
app_timer_cnt_get(&amp;amp;app_timer_cur);
app_timer_cnt_diff_compute(app_timer_cur, watchdog_timer, &amp;amp;app_timer_diff);
if (app_timer_diff &amp;gt; APP_TIMER_TICKS(WATCHDOG_TMOUT,APP_TIMER_PRESCALER))
{
    /* Watchdog timeout */
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;WATCHDOG_TIMOUT is in milliseconds, here it is a constant but it could be a variable.&lt;/p&gt;
&lt;p&gt;The cnt_diff_compute function handles timer range and rollover. This of course has the same constraint that RTC1 is running when you are tracking elapsed time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: how to implement accurate milisecond function</title><link>https://devzone.nordicsemi.com/thread/17101?ContentTypeID=1</link><pubDate>Tue, 16 Dec 2014 03:58:13 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:66eedf9f-2482-43bb-9026-d01d68a378b5</guid><dc:creator>Clem Taylor</dc:creator><description>&lt;p&gt;I modified app_timer.c to include a overflow count and then I added an access function to (hopefully) atomically access the current overflow count and the RTC1 counter value. From there you can compute seconds and microseconds or whatever you need. I do all my long term timekeeping stuff in seconds &amp;amp; ticks and my short term timekeeping in RTC1 ticks.&lt;/p&gt;
&lt;p&gt;One quirk is you always need to keep at least one timer active or the app_timer code will disable RTC1 to save power. I happen to have a timer that is always running, so this wasn&amp;#39;t an issue for me.&lt;/p&gt;
&lt;p&gt;My access code is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;void app_timer_ticks(uint32_t *p_overflow, uint32_t *p_ticks)
{
    volatile uint32_t overflow0, overflow1;
    uint32_t ticks;
    
    /* spin trying to sample clean counters */
    do
    {
        overflow0 = m_overflow_count;
        ticks = NRF_RTC1-&amp;gt;COUNTER;
        overflow1 = m_overflow_count;
    } while (overflow0 != overflow1); // unlikely 
    
    *p_overflow = overflow1;
    *p_ticks    = ticks;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;d imagine this still has a small race window where COUNTER is sampled after it rolls over but before m_overflow_count is updated by the interrupt. Reading the COUNTER twice to make sure the second read is larger then the next might close the window, but I haven&amp;#39;t tried this.&lt;/p&gt;
&lt;p&gt;I increment m_overflow_count on EVENTS_OVRFLW in RTC1_IRQHandler() and turn on the overflow event in rtc1_start().&lt;/p&gt;
&lt;p&gt;This might be a useful feature to add to a future version of the SDK.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>