<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/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/"><channel><title>How use timer to delay in a function?</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/23511/how-use-timer-to-delay-in-a-function</link><description> 
A delay function is defined normally as: 

 
void delay(int n)
{
 int i;
 for (i= 0; i&amp;lt;n; i++)
 _nop_();
}
 

 
 
when delay() performed,ALU get in loop but do nothing.Can use
a timer counting to delay and release ALU to do something ?For
example ,after</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/113793?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2008 01:29:03 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:f3a479ae-4619-4adf-9d70-da9c52111f6e</guid><dc:creator>Arthur Dent</dc:creator><description>&lt;p&gt;&lt;p&gt;
... and &lt;b&gt;&lt;i&gt;&amp;quot;Real Programmers Eat Quiche&amp;quot;&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/113782?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2008 00:42:55 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ff4fcf66-099f-4446-88ae-1da8b92f4065</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
In addition to the reasons to avoid delay loops already given by
Hans-Bernhard Broeker, writing them in a high-level language (HLL)
such as &amp;#39;C&amp;#39; is also a &lt;i&gt;&lt;b&gt;Really Bad Idea&lt;/b&gt;&lt;/i&gt; - see:&lt;br /&gt;
&lt;a target="_blank" href="http://www.8052.com/forum/read.phtml?id=149030"&gt;&amp;quot; href=
&amp;quot;http://www.8052.com/forum/read.phtml?id=152076&amp;quot;&amp;gt;&lt;a href="http://www.8052.com/forum/read.phtml?id=152076"&gt;www.8052.com/.../read.phtml&lt;/a&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/100430?ContentTypeID=1</link><pubDate>Mon, 31 Mar 2008 00:36:28 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6b9907b2-15b9-4e26-b351-38fcf50e2941</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;Oh dear, please don&amp;#39;t say that something as mundane as a delay
must now be classified as proper or otherwise!&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
You can argue about whether &amp;quot;proper&amp;quot; is an appropriate term for
it, but it is certainly a &lt;i&gt;&lt;b&gt;Really Bad Idea&lt;/b&gt;&lt;/i&gt; - for
precisely the reasons given (and subject to the exceptions explained)
by Hans-Bernhard Broeker.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/76131?ContentTypeID=1</link><pubDate>Sun, 30 Mar 2008 03:15:25 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e0834125-58a4-4512-a327-76e76f852772</guid><dc:creator>Arthur Dent</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;Proper delays are implemented as timers.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Oh dear, please don&amp;#39;t say that something as mundane as a delay
must now be classified as proper or otherwise!&lt;/p&gt;

&lt;p&gt;
May have to put an &lt;b&gt;&amp;quot;only academics permitted&amp;quot;&lt;/b&gt; warning on
the forum if it continues.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/49619?ContentTypeID=1</link><pubDate>Sun, 30 Mar 2008 03:03:46 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:a06a0000-32fc-49a4-b993-47cbd62ca520</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;A delay function is defined normally as:&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Actually, no. Delay functions are &amp;quot;normally&amp;quot; not defined or used
at all, and for precisely the reason you just described: they hog the
CPU and keep it from doing useful things in the meantime. The only
exception are delays that are so short that a) it doesn&amp;#39;t hurt to hog
the CPU for their duration, and b) they can&amp;#39;t be implemented other
than by busy-waiting anyway.&lt;/p&gt;

&lt;p&gt;
Proper delays are implemented as timers. Either by polling a
free-running timer to see when the requisite time has passed, or by
setting a timer interrupt (think &amp;quot;alarm clock&amp;quot;) that signals
completion of the waiting time.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/49626?ContentTypeID=1</link><pubDate>Sun, 30 Mar 2008 02:32:30 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:00fdd54a-bc16-42a8-b94a-8fb328a8a926</guid><dc:creator>Tamir Michael</dc:creator><description>&lt;p&gt;&lt;p&gt;
Of course you can. all you need to do is the following:&lt;br /&gt;
* start/reset your hardware timer.&lt;br /&gt;
* poll its value in your main loop.&lt;br /&gt;
* don&amp;#39;t take any action until the required delay has elapsed.&lt;br /&gt;
* do whatever you need to do once the delay has elapsed.&lt;br /&gt;
* repeat this procedure whenever required.&lt;/p&gt;

&lt;p&gt;
for increased accuracy you might want to have your timer generate
an interrupt, but that might not be required at all.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How use timer to delay in a function?</title><link>https://community.arm.com/thread/51641?ContentTypeID=1</link><pubDate>Sat, 29 Mar 2008 23:22:20 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6de7f1d5-1704-436b-a2fd-11de132c315a</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
Yes, you can use the timer to count time in the background:&lt;/p&gt;

&lt;pre&gt;
start_long_operation();
start_timer();
while (!timer_end) {
    do_something();
}
&lt;/pre&gt;

&lt;p&gt;
Programs that does not make use of an RTOS normally has a
super-loop that constantly scans inputs, timer flags, ...&lt;/p&gt;

&lt;p&gt;
They may perform too much concurrent operations to be able to use
a timer for each of them, so instead they may have a single timer
that ticks time. Let&amp;#39;s say in 10ms steps:&lt;/p&gt;

&lt;pre&gt;
extern unsigned current_ticks;
unsigned flash_start_time;
unsigned char flash_op;
for (;;) {
    if (flash_op != FLASH_IDLE) {
        switch (flash_op) {
            case FLASH_ERASE:
                start_flash_erase();
                flash_start_time = current_ticks;
                flash_op = FLASH_ERASE_WAIT;
                break;
            case FLASH_ERASE_WAIT:
                if ((current_ticks - flash_start_time) &amp;gt; FLASH_ERASE_TIME) {
                    do_flash_job();
                    flash_op = FLASH_NEXT_STATE;
                }
                break;
            case FLASH_NEXT_STATE:
                ...
        }
    }
    if (other_job_needed) {
        do_other_job();
    }
    if (yet_another_job_needed) {
        do_yet_another_job();
    }
    if ((other_timer - current_ticks) &amp;gt; OTHER_INTERVAL) {
        do_other_timed_job();
        other_timer = current_ticks;
    }
}
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>