<?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: Can&#039;t figure out timers, please help.</title>
		<link>http://forums.leaflabs.com/topic.php?id=834</link>
		<description>A place to share, learn, and grow...</description>
		<language>en-US</language>
		<pubDate>Fri, 22 Jan 2016 00:14:35 +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=834" rel="self" type="application/rss+xml" />

		<item>
			<title>gbulmer on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6478</link>
			<pubDate>Wed, 21 Sep 2011 08:32:43 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">6478@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;gulu2065 - Have a look at &#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=1054#post-6477&#34; rel=&#34;nofollow&#34;&#62;http://forums.leaflabs.com/topic.php?id=1054#post-6477&#60;/a&#62;&#60;br /&#62;
I've suggested a couple of ways to read data at 1.6MHz.&#60;/p&#62;
&#60;p&#62;Essentially, don't bother with interrupts. IMHO they waste far too many cycles for something this fast and this regular.&#60;br /&#62;
Instead poll the signal and use a timer to give an accurate and consistent timebase, or use the 'Input Capture mode' of a timer to measure the duration of the signal in each state.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gulu2065 on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6469</link>
			<pubDate>Wed, 21 Sep 2011 01:45:31 +0000</pubDate>
			<dc:creator>gulu2065</dc:creator>
			<guid isPermaLink="false">6469@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;gbulmer,&#60;br /&#62;
I have generate the signal with timer work on PWM mode.&#60;br /&#62;
Now the problem is I need it to read data from another chip, at 1.6Mhz. While the interrupt work at too low speed and can't reach 1.6Mhz.&#60;br /&#62;
So I am considering to reduce the time it need to do the handler function but I haven't get how to?&#60;br /&#62;
Any suggestions?&#60;br /&#62;
Thanks
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6458</link>
			<pubDate>Tue, 20 Sep 2011 21:05:16 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">6458@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;gulu2065&#60;/p&#62;
&#60;p&#62;The timer is capable of generating a square wave directly, without any software involvement. If you can do what you need without software, try to do it. When you rely on software and interrupt handlers, it will jitter when other interrupts trigger.&#60;/p&#62;
&#60;p&#62;You could use PWM mode, but you don't need 'updown counter mode'. That PWM mode uses two cycles to generate one cycle of output to get the PWM pulse symmetric, so the frequency needs to be doubled to get it right.&#60;/p&#62;
&#60;p&#62;(full disclosure: I am not a member of LeafLabs staff)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gulu2065 on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6440</link>
			<pubDate>Tue, 20 Sep 2011 16:01:46 +0000</pubDate>
			<dc:creator>gulu2065</dc:creator>
			<guid isPermaLink="false">6440@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;robodude666,&#60;br /&#62;
I still didn't get it. How could I reduce the timer handler need with this?&#60;br /&#62;
Could you explain in a little more detail?&#60;br /&#62;
Thanks a lot!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6424</link>
			<pubDate>Tue, 20 Sep 2011 12:27:05 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">6424@http://forums.leaflabs.com/</guid>
			<description>&#60;blockquote&#62;&#60;p&#62;
You could also try and use the timers to generate your toggle with PWM, then you could get it going faster.
&#60;/p&#62;&#60;/blockquote&#62;
&#60;p&#62;Anyone working on this may want to check out the Servo library source for some example code:&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;https://github.com/leaflabs/libmaple/tree/v0.0.12/libraries/Servo/&#34; rel=&#34;nofollow&#34;&#62;https://github.com/leaflabs/libmaple/tree/v0.0.12/libraries/Servo/&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6415</link>
			<pubDate>Tue, 20 Sep 2011 12:03:49 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">6415@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;vonnieda,&#60;br /&#62;
  You could also try and use the timers to generate your toggle with PWM, then you could get it going faster. I see gulu2065 is working on this here &#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=1046&#34; rel=&#34;nofollow&#34;&#62;http://forums.leaflabs.com/topic.php?id=1046&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>robodude666 on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6402</link>
			<pubDate>Tue, 20 Sep 2011 09:59:33 +0000</pubDate>
			<dc:creator>robodude666</dc:creator>
			<guid isPermaLink="false">6402@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Locate your libmaple installation and go to the &#60;code&#62;libmaple/timers.c&#60;/code&#62; file. Locate the &#60;a href=&#34;https://github.com/leaflabs/libmaple/blob/master/libmaple/timer.c#L253&#34;&#62;__irq_XXXXX&#60;/a&#62; functions. These are the handlers called when an interrupt occurs. They then dispatch the call to the respective user handler.&#60;/p&#62;
&#60;p&#62;To workaround this overhead, you will have to play in there.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gulu2065 on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-6396</link>
			<pubDate>Mon, 19 Sep 2011 17:38:16 +0000</pubDate>
			<dc:creator>gulu2065</dc:creator>
			<guid isPermaLink="false">6396@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;&#34; checkout the source behind the timer's interrupt handlers.&#34;&#60;/p&#62;
&#60;p&#62;&#34; I put my bit flip code directly into libmaple where the IRQ runs and it easily gets 2 MHz.&#34;&#60;/p&#62;
&#60;p&#62;I am quite interested in this. Could anyone told me how to do this?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>mbolivar on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-4996</link>
			<pubDate>Mon, 06 Jun 2011 13:04:11 +0000</pubDate>
			<dc:creator>mbolivar</dc:creator>
			<guid isPermaLink="false">4996@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;vonnieda,&#60;/p&#62;
&#60;p&#62;As robodude666 said, there is some overhead involved with the timer interrupt handling.  I'm doing some work this week with a friend of ours who's doing a Maple-compatible shield which relies on timer interrupts; I'll try to do some performance tuning/bugfixing as I go.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>vonnieda on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-4984</link>
			<pubDate>Sun, 05 Jun 2011 18:44:55 +0000</pubDate>
			<dc:creator>vonnieda</dc:creator>
			<guid isPermaLink="false">4984@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thanks for the responses. &#60;/p&#62;
&#60;p&#62;nissarup, I found the same problem with setPeriod so I am using the setOverflow and setCompare directly, as you are.&#60;/p&#62;
&#60;p&#62;robodude666, you are right. I put my bit flip code directly into libmaple where the IRQ runs and it easily gets 2 MHz. Seems like I will need to try to find a way to optimize the interrupt handlers.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>nissarup on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-4982</link>
			<pubDate>Sun, 05 Jun 2011 14:42:46 +0000</pubDate>
			<dc:creator>nissarup</dc:creator>
			<guid isPermaLink="false">4982@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I probably should have mentioned that I tried the setPeriod method and it refused to run when called with anything less than 1000.&#60;br /&#62;
At least that's what I think was happening. The board did nothing when all I changed was the period.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>robodude666 on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-4980</link>
			<pubDate>Sun, 05 Jun 2011 12:55:37 +0000</pubDate>
			<dc:creator>robodude666</dc:creator>
			<guid isPermaLink="false">4980@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I don't have much experience with dialing in the timers to an exact period myself, but you can checkout the documentation on &#60;a href=&#34;http://static.leaflabs.com/pub/leaflabs/maple-docs/0.0.10beta/lang/api/hardwaretimer.html#lang-hardwaretimer&#34;&#62;HardwareTimer&#60;/a&#62;. It has a &#60;code&#62;setPeriod&#60;/code&#62; method that accepts a period in microseconds.&#60;/p&#62;
&#60;p&#62;As for the interrupts taking so long to call your function, checkout the source behind the timer's interrupt handlers. It jumps around between a few functions, does some logic checks, and then calls your function. It's got a bit of overhead.&#60;/p&#62;
&#60;p&#62;Maybe a future release of libmaple would remove this overhead and allow us to include the __...irq functions ourselves in our projects. Possibly as a feature of the new timer classes &#60;a href=&#34;http://forums.leaflabs.com/topic.php?id=794&#34;&#62;mbolivar&#60;/a&#62; is considering.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>nissarup on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-4979</link>
			<pubDate>Sun, 05 Jun 2011 12:38:59 +0000</pubDate>
			<dc:creator>nissarup</dc:creator>
			<guid isPermaLink="false">4979@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Here is how I setup my timer interrupt for an Apple Remote receiver script.&#60;br /&#62;
I use it at 30us interrupt.&#60;/p&#62;
&#60;p&#62;If I understand the timer right, whatever you set U_SEC_PER_TICK will be the us between each interrupt.&#60;/p&#62;
&#60;p&#62;&#60;code&#62;&#60;br /&#62;
  timer.pause();&#60;br /&#62;
  timer.setPrescaleFactor(72);&#60;br /&#62;
  timer.setOverflow(U_SEC_PER_TICK);&#60;br /&#62;
  timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);&#60;br /&#62;
  timer.setCompare(TIMER_CH1, 1);&#60;br /&#62;
  timer.attachCompare1Interrupt(handler_ir_remote);&#60;br /&#62;
  timer.refresh();&#60;br /&#62;
  timer.resume();&#60;br /&#62;
&#60;/code&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>vonnieda on "Can&#039;t figure out timers, please help."</title>
			<link>http://forums.leaflabs.com/topic.php?id=834#post-4977</link>
			<pubDate>Sat, 04 Jun 2011 20:28:20 +0000</pubDate>
			<dc:creator>vonnieda</dc:creator>
			<guid isPermaLink="false">4977@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Hi folks,&#60;/p&#62;
&#60;p&#62;I've been fighting with the timers on the Maple for a week now and something just isn't adding up. I have a r5 with pin 0 hooked up to my 25 MHz scope and running the following sketch:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;timer.h&#38;gt;

void setup() {
  SerialUSB.end();
  systick_disable();

  pinMode(0, OUTPUT);

  digitalWrite(0, LOW);

  timer_pause(TIMER1);
  timer_set_mode(TIMER1, TIMER_CH1, TIMER_OUTPUT_COMPARE);
  timer_set_prescaler(TIMER1, 1);
  timer_set_reload(TIMER1, 0xffff);
  timer_set_compare(TIMER1, TIMER_CH1, 71);
  timer_attach_interrupt(TIMER1, TIMER_CH1, handler1);
  timer_generate_update(TIMER1);
  timer_resume(TIMER1);
}

void handler1() {
  GPIOA_BASE-&#38;gt;ODR ^= BIT(3);
  timer_set_count(TIMER1, 0);
}

void loop() {
  for (;;) {
  }
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;My intention is to prove to myself that I can count on a 1uS timer but when I run this sketch (from Flash or RAM) I am only seeing output at 150 kHz. So, the interrupt is firing at 300 kHz. This is more than 3x as slow as I would expect. &#60;/p&#62;
&#60;p&#62;The way (I think) I have this set up, I would expect the timer to count up at 72 MHz (prescaler 1), and then fire the interrupt when it hits the compare of 71. Since the clock runs at 72 MHz, and I am effectively dividing it by 72 I expect to see the interrupt firing at 1 MHz. &#60;/p&#62;
&#60;p&#62;The only thing I can think of is that the interrupt handler takes more than 1uS to execute, but that seems like a long time to me. I believe the Maple is supposed to run instructions at approximately clock speed so that means it should have 72 instructions worth of time to execute the interrupt handler. I'd be surprised to find out that it takes 72 instructions to jump to the handler, flip a bit, set a 16 bit register and return.&#60;/p&#62;
&#60;p&#62;Even if it is taking more than 1uS to execute, 3.3uS seems excessively long.&#60;/p&#62;
&#60;p&#62;So, any ideas? Suggestions?&#60;/p&#62;
&#60;p&#62;I'm open to other ideas on how to do this. The final purpose of the code is to implement a 1 MHz timer that I can use for stepper motor step timing. I will be setting the compare to the time of the next step and then modifying the value on the fly. I need this interrupt to be handled quickly because I will be controlling 4 steppers on the same timer and it's possible that the &#34;next&#34; step value could be very close to the previous one.&#60;/p&#62;
&#60;p&#62;-----&#60;/p&#62;
&#60;p&#62;Before I finished posting this I decided to try another test.&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#include &#38;lt;timer.h&#38;gt;

void setup() {
  //SerialUSB.end();
  systick_disable();

  pinMode(0, OUTPUT);

  digitalWrite(0, LOW);

  timer_pause(TIMER1);
  timer_set_mode(TIMER1, TIMER_CH1, TIMER_OUTPUT_COMPARE);
  timer_set_prescaler(TIMER1, 1);
  timer_set_reload(TIMER1, 0xffff);
  timer_set_compare(TIMER1, TIMER_CH1, 71);
  timer_attach_interrupt(TIMER1, TIMER_CH1, handler1);
  timer_generate_update(TIMER1);
  timer_resume(TIMER1);
}

uint16 a, b;

void handler1() {
  a = timer_get_count(TIMER1);
  GPIOA_BASE-&#38;gt;ODR ^= BIT(3);
  b = timer_get_count(TIMER1);
  timer_set_count(TIMER1, 0);
}

void loop() {
  for (;;) {
    delay(1000);
    timer_pause(TIMER1);
    SerialUSB.print(a, DEC);
    SerialUSB.print(&#38;quot; &#38;quot;);
    SerialUSB.println(b, DEC);
    timer_resume(TIMER1);
  }
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;And here's the output:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;112 123
113 123
113 123
112 123
112 123
113 123
112 123
113 123
112 123
113 123
113 123
112 123
113 123
113 123
112 123
113 123
112 123
113 123
112 123
112 123
113 123
113 123
112 123
112 123
74 74
112 123
112 123
113 123
113 123
112 123
112 123
112 123
112 123&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;This is pretty surprising to me. It's taking nearly 40 clocks to get into the interrupt handler and read a 16 bit value?!&#60;/p&#62;
&#60;p&#62;Anyone have any suggestions, or want to tell me what I am doing wrong?&#60;/p&#62;
&#60;p&#62;Thanks,&#60;br /&#62;
Jason
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
