<?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: Use millis() with of external interrupts</title>
		<link>http://forums.leaflabs.com/topic.php?id=74179</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:17:58 +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=74179" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Use millis() with of external interrupts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74179#post-105040</link>
			<pubDate>Sun, 08 Dec 2013 12:35:16 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">105040@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;vfr800nut - &#60;em&#62;&#34;I've got a similar problem, reading a 12 pulse encoder at 18K RPM and a 3 pulse encoder at 9KRpm with the 3rd pulse being a timing trigger point.&#34;&#60;/em&#62;&#60;/p&#62;
&#60;p&#62;I think you are describing a significantly harder problem than samtal's code is aiming to solve. The code is using millis() to measure pulse duration, which introduces about a 1ms error.&#60;/p&#62;
&#60;p&#62;&#60;em&#62;&#34;DMA channel 1 ADC can function to print a timestamp on voltage above x(digital high), without stopping the timer(timer 1). It prints the timer 2 value.&#34;&#60;/em&#62;&#60;br /&#62;
That is a nifty approach in the general case.&#60;/p&#62;
&#60;p&#62;Assuming the signals from the pulse encoders are clean, and at 3.3V level, the timers can directly time signals. (If they are quadrature encoders, then timers can be configured to count up and down using the two pulse signals.)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>vfr800nut on "Use millis() with of external interrupts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74179#post-105036</link>
			<pubDate>Sun, 08 Dec 2013 07:17:51 +0000</pubDate>
			<dc:creator>vfr800nut</dc:creator>
			<guid isPermaLink="false">105036@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I've got a similar problem, reading a 12 pulse encoder at 18K RPM and a 3 pulse encoder at 9KRpm with the 3rd pulse being a timing trigger point.&#60;/p&#62;
&#60;p&#62;From what I've read,&#60;br /&#62;
DMA channel 1 ADC can function to print a timestamp on voltage above x(digital high), without stopping the timer(timer 1). It prints the timer 2 value. Obviously using dma the value won't be a real time interrupt but would provide the timestamp.  &#60;/p&#62;
&#60;p&#62;I haven't written any code for it, yet.&#60;br /&#62;
hope that helps
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Use millis() with of external interrupts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74179#post-105010</link>
			<pubDate>Tue, 03 Dec 2013 21:19:56 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">105010@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;samtal - The timers can be used to time a signal on a pin.&#60;/p&#62;
&#60;p&#62;libmaple doesn't support that, so it would require digging through RM0008 :-(&#60;/p&#62;
&#60;p&#62;That would require very brief interrupts, one when the values are copied from the counter, and the other to enable or disable the counter.&#60;/p&#62;
&#60;p&#62;Edit:&#60;br /&#62;
samtal, I realise the code is simplified, but that code doesn't compile for me.&#60;/p&#62;
&#60;p&#62;I've created a dummy declaration for &#60;code&#62;Select_Mode&#60;/code&#62;.&#60;br /&#62;
However, when I try to compile the code, I get:&#60;br /&#62;
&#34; error:  'time' was not declared in this scope&#34;&#60;br /&#62;
&#34; warning: unused variable 'time' &#34;&#60;br /&#62;
&#34; error: 'attachinterrupt' was not declared in this scope&#34; - it should be &#60;code&#62;attachInterrupt&#60;/code&#62;&#60;br /&#62;
Does this compile okay for you? &#60;/p&#62;
&#60;p&#62;Edit2: detachInterrupt() simply stops an interrupt service routine being called in the future.&#60;br /&#62;
detachInterrupt() does not effect the currently running interrupt service routine, I does not effect the priority of a currently running interrupt service routine. It can't effect the ability of the the millis() interrupt to run until the current interrupt service routine has finished. So it doesn't enable the millis() interrupt to run while run_cycle() is running.&#60;/p&#62;
&#60;p&#62;I guess the time taken to do:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;uint8 nowstate = digitalRead(runChannel);
while (digitalRead(runChannel)== nowstate) { }&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;Is not much different from taking an interrupt. &#60;/p&#62;
&#60;p&#62;So you might rewrite the code to throw an interrupt on every transition of enableChannel and runChannel (assuming the pins are chosen so that there are two different interrupt service routines).&#60;/p&#62;
&#60;p&#62;&#60;code&#62;count&#60;/code&#62; and &#60;code&#62;starttime&#60;/code&#62; would become static variables accessible by both interrupt service routines.&#60;br /&#62;
The enableChannel's interrupt would either &#60;code&#62;attachInterrupt&#60;/code&#62; or &#60;code&#62;detachInterrupt&#60;/code&#62; for the &#60;code&#62;runChannel&#60;/code&#62;, and when doing an attachInterrupt, it would do &#60;code&#62;count=0;&#60;/code&#62; and &#60;code&#62;starttime = millis();&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;The runChannel interrupt would count, and do something about the time (I can't be sure what as the code seems to have a bug).&#60;br /&#62;
I assume you want each runChannel duration. As I wrote above, that could be done by a timer, in which case it might not need an interrupt.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mlundinse on "Use millis() with of external interrupts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74179#post-105009</link>
			<pubDate>Tue, 03 Dec 2013 13:08:46 +0000</pubDate>
			<dc:creator>mlundinse</dc:creator>
			<guid isPermaLink="false">105009@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;First of all interrupt handlers should generally be short and never have to wait for millis(). It will interfere with other things like serial and USB communications.&#60;/p&#62;
&#60;p&#62;If you still need an interrupt handler that takes a long time, just make sure that your interrupt handler runs at lower priority (higher value in priority registers) than the priority for the SysTick handler.&#60;/p&#62;
&#60;p&#62;This is done in the NVIC interrupt priority registers. There is a function to do this, probably found in files nvic.c/h ( I dont have the Maple lib files available right now )
&#60;/p&#62;</description>
		</item>
		<item>
			<title>samtal on "Use millis() with of external interrupts"</title>
			<link>http://forums.leaflabs.com/topic.php?id=74179#post-105008</link>
			<pubDate>Tue, 03 Dec 2013 08:56:07 +0000</pubDate>
			<dc:creator>samtal</dc:creator>
			<guid isPermaLink="false">105008@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi all,&#60;br /&#62;
It is known that millis() (that is interrupt driven) can not update time when run within an interrupt handler function.&#60;br /&#62;
I tried to detach the interrupt within the handler, after its start, and read the millis(), but the millis does not update although the interrupt was detached.&#60;/p&#62;
&#60;p&#62;I have also tried to have an in-between handler to run my function, but it is still the same: The millis() timer does not advance.&#60;br /&#62;
(Does this mean that whenever there is an active interrupt, the timer freezes?!)&#60;/p&#62;
&#60;p&#62;I still wish I could start the function by an external interrupt!&#60;br /&#62;
Is this normal and is there a reasonable workaround to that?&#60;/p&#62;
&#60;p&#62;The code looks like: (simplified) &#60;/p&#62;
&#60;pre&#62;&#60;code&#62;void setup()
{
  attachinterrupt(enableChannel, run_cycle, RISING);
}

void run_cycle()
{
  detachInterrupt(enableChannel);
  uint16 count = 0;
  uint32 starttime = millis();
  do
  {
    uint8 nowstate = digitalRead(runChannel);
    while (digitalRead(runChannel)== nowstate) { }  //Loop until logic change
    uint16 time = (millis() - starttime) &#38;amp; 0xFFFF;
    count ++;
    }
   while (digitalRead(enableChannel) == 1);  

attachInterrupt(enableChannel, run_cycle, RISING);

  Select_Mode(time, count);
}&#60;/code&#62;&#60;/pre&#62;</description>
		</item>

	</channel>
</rss>
