<?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: Timer interrupts and SerialUSB.print()</title>
		<link>http://forums.leaflabs.com/topic.php?id=139</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:23:12 +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=139" rel="self" type="application/rss+xml" />

		<item>
			<title>poslathian on "Timer interrupts and SerialUSB.print()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=139#post-866</link>
			<pubDate>Fri, 27 Aug 2010 23:12:14 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">866@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;real quickly (again, ill hack on this problem at a later time) - SerialUSB.end() is unique in that it disables the usb interrupts. Even if you dont call SerialUSB.read/print/write/println/etc, USB interrupts are still firing, because thats how USB works (its a polling based protocol...as in every 5ms the host says - do you have data? how about now? now? .... forever). A lot of time this question (do you have data) is not even intended for Maple, but some other device on the usb bus. It is all the devices on the bus responsibility to filter out requests that arnt for them, via addressing. This filtering process also uses the interrupt. &#60;/p&#62;
&#60;p&#62;The USB interrupt *could* be conflicting with your timer interrupts in some convoluted way. Thats why i suggested running SerialUSB.end()&#60;/p&#62;
&#60;p&#62;As you probably guessed, running SerialUSB.end() disables the auto-reset feature during programming (since thats done over USB). You will have to manually hit reset when programming.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>StephenFromNYC on "Timer interrupts and SerialUSB.print()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=139#post-864</link>
			<pubDate>Fri, 27 Aug 2010 23:06:44 +0000</pubDate>
			<dc:creator>StephenFromNYC</dc:creator>
			<guid isPermaLink="false">864@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hello,&#60;/p&#62;
&#60;p&#62;Thanks for the input, poslathian.  I am not near my development system, so I cannot try your suggestions before tomorrow.&#60;/p&#62;
&#60;p&#62;I am not sure of the value of calling SerialUSB.end() in setup(), because as your say this will turn off SerialUSB, which is what I want to use later in the handler.  Calling SerialUSB.end() is not necessary if I simply remove SerialUSB.print from the handler which would return the code to exactly as it appears in the simpler example on:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/maple/timers/&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/maple/timers/&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;However, I had two ideas which might help someone else debug the code before I have time.&#60;/p&#62;
&#60;p&#62;The first idea is to use the Maple with an external power supply.  Will this make SerialUSB more stable if the wire is just used for communication (and not for power).  So far, for all my early Maple coding I have been powering the board with the USB cord.  I guess I should just make an adapter for connecting a 9V battery.&#60;/p&#62;
&#60;p&#62;I am guessing, but is it possible the interrupt code and the handler might be more stable if the SerialUSB is momentarily inactivated in setup()?&#60;/p&#62;
&#60;p&#62;Something which might work is to use SerialUSB.end() very early in the setup() function before setting up the timer routines, and then after the timer settings are complete to call SerialUSB.begin() and perhaps also delay(5000) before setup() ends.  I am just suggesting five seconds as a guess; something long to give the SerialUSB a chance to reinitialize completely before setup() ends.&#60;/p&#62;
&#60;p&#62;The five second delay after SerialUSB.end() might be helpful, because I read somewhere than SerialUSB.end() is pretty harsh and can confuse other code or the host system.&#60;/p&#62;
&#60;p&#62;On the Maple I have not tried SerialN.println() instead of SerialUSB.print().  What is faster at sending characters to the host system Serial SerialUSB.print() or SerialN.print()?  I prefer the USB method, because the wiring is trivial, but if I can get Serial working at a high baud (at least 115200 baud) then I will be happy.&#60;/p&#62;
&#60;p&#62;On the Arduino platform I have not tried invoking Serial.print() from within an interrupt handler, so I do not know if the Arduino would behave any better.  Of course, the Arduino requires a Serial.begin().&#60;/p&#62;
&#60;p&#62;A separate question.  In a leaflabs forum post, how do I get my &#34;code&#34; to appear in a separate &#34;rectangle&#34;.  I tried to use &#60;code&#62; and &#60;/code&#62; in my last post, but it did not work.  In the last sentence I was trying to use &#34;&#38;amp; less than colon&#34; and also &#34;&#38;amp; greater than colon&#34; to show &#38;lt; code &#38;gt; and also &#38;lt; /code &#38;gt;.  I hope this question gets formatted correctly.&#60;/p&#62;
&#60;p&#62;Thanks, and to all a good night!&#60;/p&#62;
&#60;p&#62;Stephen from NYC
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "Timer interrupts and SerialUSB.print()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=139#post-862</link>
			<pubDate>Fri, 27 Aug 2010 22:23:07 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">862@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Stephen, I will take a close look at your sketch when I have moment. However my first pass answer is to try disabling the USB and see if it then works. You can do this via SerialUSB.end(). Also, roughly, using timers and USB together is &#34;expected to work&#34; however, that isnt to say it isnt reasonable that it wouldnt. &#60;/p&#62;
&#60;p&#62;Longer post on the way....try SerialUSB.end() in your setup and remove the SerialUSB print line in your handler. Let me know if that works (I realize its not a fix...since then you dont have serialUSB...)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>StephenFromNYC on "Timer interrupts and SerialUSB.print()"</title>
			<link>http://forums.leaflabs.com/topic.php?id=139#post-851</link>
			<pubDate>Fri, 27 Aug 2010 12:36:03 +0000</pubDate>
			<dc:creator>StephenFromNYC</dc:creator>
			<guid isPermaLink="false">851@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hello,&#60;/p&#62;
&#60;p&#62;Recently, I have been playing around with timer interrupts.&#60;/p&#62;
&#60;p&#62;I made a simple modification to the &#34;LED blink&#34; sketch listed at:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/maple/timers/&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/maple/timers/&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;I added a single SerialUSB.println() statement.&#60;/p&#62;
&#60;p&#62;The code compiles and uploads successfully to RAM (based on getting a complete message after the upload finishes), but the Maple seems to hang the first time handler_led() is called.  I have not tried uploading to FLASH.&#60;/p&#62;
&#60;p&#62;What functions can be called within handler_led()?  I know that analogRead() calls work (ie, do not crash the board), but I have not been able to see the value returned by my call to analogRead().&#60;/p&#62;
&#60;p&#62;I am using a Maple Rev 3 board, Maple IDE 0.6.0, Windows XP Pro SP3, Java 1.6.0 update 20 (&#34;version 6&#34;; yes, I know that update 21 is available), on a 1 GHz Pentium III (512 meg RAM).&#60;/p&#62;
&#60;p&#62;Thanks!&#60;/p&#62;
&#60;p&#62;Stephen from NYC&#60;/p&#62;
&#60;p&#62;P.S.  Here is the first time I am trying to enter code:&#60;/p&#62;
&#60;p&#62;Here is the original UNmodified code:&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;
#define LED_PIN 13&#60;br /&#62;
#define LED_RATE 500000    // in microseconds; should give 0.5Hz toggles&#60;/p&#62;
&#60;p&#62;void handler_led(void);&#60;br /&#62;
int toggle = 0;&#60;/p&#62;
&#60;p&#62;void setup()&#60;br /&#62;
{&#60;br /&#62;
    // Set up the LED to blink&#60;br /&#62;
    pinMode(LED_PIN, OUTPUT);&#60;/p&#62;
&#60;p&#62;    // Setup Timer&#60;br /&#62;
    Timer2.setChannel1Mode(TIMER_OUTPUTCOMPARE);&#60;br /&#62;
    Timer2.setPeriod(LED_RATE); // in microseconds&#60;br /&#62;
    Timer2.setCompare1(1);      // overflow might be small&#60;br /&#62;
    Timer2.attachCompare1Interrupt(handler_led);&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop() {&#60;br /&#62;
    // Nothing! It's all in the interrupts&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void handler_led(void) {&#60;br /&#62;
    toggle ^= 1;&#60;br /&#62;
    digitalWrite(LED_PIN, toggle);&#60;br /&#62;
}&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;Here is the modified code:&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;
#define LED_PIN 13&#60;br /&#62;
#define LED_RATE 500000    // in microseconds; should give 0.5Hz toggles&#60;/p&#62;
&#60;p&#62;void handler_led(void);&#60;br /&#62;
int toggle = 0;&#60;/p&#62;
&#60;p&#62;void setup()&#60;br /&#62;
{&#60;br /&#62;
    // Set up the LED to blink&#60;br /&#62;
    pinMode(LED_PIN, OUTPUT);&#60;/p&#62;
&#60;p&#62;    // Setup Timer&#60;br /&#62;
    Timer2.setChannel1Mode(TIMER_OUTPUTCOMPARE);&#60;br /&#62;
    Timer2.setPeriod(LED_RATE); // in microseconds&#60;br /&#62;
    Timer2.setCompare1(1);      // overflow might be small&#60;br /&#62;
    Timer2.attachCompare1Interrupt(handler_led);&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void loop() {&#60;br /&#62;
    // Nothing! It's all in the interrupts&#60;br /&#62;
}&#60;/p&#62;
&#60;p&#62;void handler_led(void) {&#60;br /&#62;
    toggle ^= 1;&#60;br /&#62;
    digitalWrite(LED_PIN, toggle);&#60;/p&#62;
&#60;p&#62;    // this is the only line added to the sketch&#60;br /&#62;
    SerialUSB.println(&#34;Hello at 0.5 Hz&#34;);&#60;br /&#62;
}&#60;/p&#62;
&#60;/blockquote&#62;</description>
		</item>

	</channel>
</rss>
