<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.0.2" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>LeafLabs Garden &#187; Topic: Using timer interrupt inside a for or while loop</title>
		<link>http://forums.leaflabs.com/topic.php?id=1129</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:22:55 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.0.2</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://forums.leaflabs.com/search.php</link>
		</textInput>
		<atom:link href="http://forums.leaflabs.com/rss.php?topic=1129" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Using timer interrupt inside a for or while loop"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1129#post-6954</link>
			<pubDate>Sun, 23 Oct 2011 16:07:14 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">6954@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;samtal - According to the RM0008 manual, in section 15.3.2 &#34;Counter modes&#34;,  there are several ways to do this without any interrupt.&#60;br /&#62;
The simplest solution looks like setting the counter for the whole timer block (using pre-scalar and auto-reload value) so that you can see the event in the timers registers&#60;/p&#62;
&#60;p&#62;You could just poll the appropriate bit directly (I think it is the UIF bit 15.4.5 &#34;TIMx status register (TIMx_SR)&#34;, but I haven't read that carefully enough to be sure. Pulling directly using something like like a register address.)&#60;/p&#62;
&#60;p&#62;The alternative is to write a tiny interrupt handler which sets a 'flag'.&#60;br /&#62;
The documentation is at &#60;a href=&#34;http://leaflabs.com/docs/lang/api/hardwaretimer.html#using-timer-interrupts&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/lang/api/hardwaretimer.html#using-timer-interrupts&#60;/a&#62;&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;HardwareTimer timer(2);
volatile int flag = 0;

void setup() {
    // set up timer
    // attach interrupt
    timer.attachCompare1Interrupt(timer2interrupt);
}

void loop() {
    //...
    flag = 1;
    while (flag) ;  // wait for the flag to clear, and hence synchronise
    flag = 1
    for (int i = 0; i&#38;lt;100;i++) {
        while (flag !=0) //Wait until timer fires (resets to 0 or other).

        timearray[i]=micros();
        flag = 1;
    }
}

void timer2interrupt() {
     flag = 0;
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>samtal on "Using timer interrupt inside a for or while loop"</title>
			<link>http://forums.leaflabs.com/topic.php?id=1129#post-6948</link>
			<pubDate>Sat, 22 Oct 2011 13:50:04 +0000</pubDate>
			<dc:creator>samtal</dc:creator>
			<guid isPermaLink="false">6948@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I have a 'for' loop in which I wish to do something (for test, make a time reading) each predefined time interval of several tens micros.&#60;br /&#62;
I initialize a hardware compare timer, define interrupt handler, sets the period and starts the timer in the setup.&#60;br /&#62;
I expect the loop to halt in each cycle until the timer reaches a certain value (0 or any) and then make the next loop.&#60;br /&#62;
At the end of the loop number, the program is expected to exit the loop and proceed in the main loop, until the function will be called again.&#60;br /&#62;
Here is a basic example of what I need:&#60;/p&#62;
&#60;p&#62;void program()&#60;br /&#62;
{&#60;br /&#62;
   for (int i = 0; i&#38;lt;100;i++)&#60;br /&#62;
  {&#60;br /&#62;
    while (timer !=0)      //Wait until timer fires (resets to 0 or other).&#60;/p&#62;
&#60;p&#62;      timearray[i]=micros();&#60;br /&#62;
   }&#60;br /&#62;
}&#60;br /&#62;
I assume I need to use an interrupt(?), but I can not use an external function, as I want the interrupt inside my function.&#60;br /&#62;
Does anyone know how I can make this work?&#60;br /&#62;
Thanks&#60;br /&#62;
samtal
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
