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

		<item>
			<title>gbulmer on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5771</link>
			<pubDate>Wed, 27 Jul 2011 10:15:26 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5771@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I had a look at the timer setup, 'void timer_init(timer_dev_num timer_num, uint16 prescale)`&#60;br /&#62;
in libmaple timers.c &#38;amp; timer.h, the mode is:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;timer-&#38;gt;CR1 = ARPE;        // No clock division
                              // Do not buffer auto-reload preload
                              // Edge aligned
                              // Upcounter
                              // Do not stop counter at update event
                              // Update events enabled (etc, see bits [1:2])
                              // Counter disabled for now&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;and APRE is:&#60;br /&#62;
&#60;code&#62;#define ARPE               BIT(7)       // Auto-reload preload enable&#60;/code&#62;&#60;/p&#62;
&#60;p&#62;This means the counter in the timer is in 'up-count' mode, and not 'up/down count' mode, so the prescaler is 22.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5768</link>
			<pubDate>Wed, 27 Jul 2011 08:28:05 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5768@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;poslathian - is their a bug in &#60;code&#62;void setPrescaleFactor(uint32 factor)&#60;/code&#62; ?&#60;/p&#62;
&#60;p&#62;If not, that can be used to set the prescaler, and nothing else should be needed; pwmWrite should 'Just Works' (TM:-)&#60;/p&#62;
&#60;p&#62;It happens that a 50Hz frequency for the model servo is quite close to the clock divided by 65,536 (the normal reload value) pre-scaled by 11 or 22, depending on whether the timer is in 'up counting' mode (prescale 22), or 'up/down counting' mode (prescale 11.&#60;/p&#62;
&#60;p&#62;The only question is whether the timers are set up for 'symmetric' PWM using 'Center-Aligned' 'up/down counting' mode, or simple 'up counting' mode, which effects the prescale value used.&#60;/p&#62;
&#60;p&#62;The documentation for timer_mode at&#60;br /&#62;
&#60;a href=&#34;http://leaflabs.com/docs/libmaple/api/timer.html#project0timer_8h_1a28110292f2481f7b44d1517fb4ce3c15&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/libmaple/api/timer.html#project0timer_8h_1a28110292f2481f7b44d1517fb4ce3c15&#60;/a&#62;&#60;br /&#62;
says the default for the timer is 'TIMER_PWM'.&#60;/p&#62;
&#60;p&#62;Edit:&#60;br /&#62;
I checked the code for &#60;code&#62;timer_int&#60;/code&#62; and the counter is in 'up-count' mode, so the appropriate prescaler value 22.&#60;/p&#62;
&#60;p&#62;(I realise this calculation is MCU clock dependant, but, at the moment all LeafLabs boards, and mine, run at 72MHz :-)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5759</link>
			<pubDate>Tue, 26 Jul 2011 14:08:03 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5759@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;thanks alot!
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5758</link>
			<pubDate>Tue, 26 Jul 2011 14:07:24 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">5758@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;btw, thats for a timer interrupt. If you just need the PWM frequency changed, you can just call timer_set_period and jump straight to pwmWrite ... I just pulled this from a sketch I happened have handy.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5757</link>
			<pubDate>Tue, 26 Jul 2011 14:06:29 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">5757@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;perhaps the hand written docs for the timers are more useful?&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;unfortunately, the setPeriod function had a bug introduced in 0.0.11 by accident. You can use this function instead:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;/* 0.0.11 breaks timer setPeriod, so lets provide a working version here */
uint16 timer_set_period(HardwareTimer timer, uint32 microseconds) {
  if (!microseconds) {
    timer.setPrescaleFactor(1);
    timer.setOverflow(1);
    return timer.getOverflow();
  }

  uint32 cycles = microseconds*(72000000/1000000); // 72 cycles per microsecond

  uint16 ps = (uint16)((cycles &#38;gt;&#38;gt; 16) + 1);
  timer.setPrescaleFactor(ps);
  timer.setOverflow((cycles/ps) -1 );
  return timer.getOverflow();
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;and you can incant the setperiod like this:&#60;/p&#62;
&#60;pre&#62;&#60;code&#62;#define AUDIO_RATE    15      /* timer period in us */

...

   // Setup Timer
    Timer2.setChannel1Mode(TIMER_OUTPUTCOMPARE);

    /* 0.0.11 IDE bug, setPeriod is no good */
    //Timer2.setPeriod(AUDIO_RATE); // in microseconds
    timer_set_period(Timer2,AUDIO_RATE);
    Timer2.setCompare1(1);      // overflow might be small
    Timer2.attachCompare1Interrupt(handler_t);&#60;/code&#62;&#60;/pre&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5752</link>
			<pubDate>Tue, 26 Jul 2011 13:38:45 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5752@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;LOL thanks alot. I can understand the frustration. Well I actually found out that my code was working, BUT my pin 5 was bad...probably a cause by my negligence. At any rate, I can control my servos BUT I am trying to control a fan. This may be a wiring issue, due to lack of documentation for the fan we bought, but I want to try every approach before I assume that the choice in fan was bad.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5750</link>
			<pubDate>Tue, 26 Jul 2011 13:08:39 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5750@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;For a normal (analogue) model servo, you want a pulse frequency of close to 50Hz, i.e. 20 millisecond cycle duration (Some seem to work with 16millisecond).&#60;/p&#62;
&#60;p&#62;Then the pulse needs to be between 1 millisecond and 2 milliseconds long, with 1.5 milliseconds being centre '90 degrees'.&#60;/p&#62;
&#60;p&#62;There is documentation of a Maple timer interface here:&#60;br /&#62;
&#60;a href=&#34;http://leaflabs.com/docs/libmaple/api/timer.html&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/libmaple/api/timer.html&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;The documentation is so baroque, ugly and complex, that I can't see the wood for the trees. Maybe you will have better luck.&#60;/p&#62;
&#60;p&#62;Timer's are 16 bits, so ideally, it will count to 65,536 in 20 milliseconds, or at 50Hz.&#60;/p&#62;
&#60;p&#62;For a simple square wave, that needs a prescaler of 21.97, so lets call that 22. That is less than 1% error, which servos should easily cope with. For a symetric PWM signal, it is half that, i.e. 11 (I'm a bit busy to check which one it is)&#60;/p&#62;
&#60;p&#62;So the 72MHz clock is divided by 22, (or 11) to give 3,273 KHz basic count rate. So each count is about 0.3 micoseconds&#60;/p&#62;
&#60;p&#62;That will need the &#34;void timer_set_prescaler(timer_dev * dev, uint16 psc)&#34; function.&#60;/p&#62;
&#60;p&#62;Then the timer will give the right servo signal frequency.&#60;/p&#62;
&#60;p&#62;Then the angle is a value between 1msec and 2msec. Which would be pwmWrite values of&#60;br /&#62;
1msec/0.3microsec = 3273&#60;br /&#62;
1.5msec/0.3micros = 4909&#60;br /&#62;
2msec/0.3microsec = 6546&#60;/p&#62;
&#60;p&#62;HTH&#60;/p&#62;
&#60;p&#62;{Start Rant: The documentation for Timer is very hard to navigate. Grrr. End Rant.}
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5748</link>
			<pubDate>Tue, 26 Jul 2011 11:39:08 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5748@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I'm sorry I wrote that completely wrong! Ok so maple run's off of 72MHz, and I am trying to control a servo. I'm guessing the maple is running extremely to fast to control the servo. So I'm assuming it is as simple as connecting the servo directly yo the maple's VCC, GND, and whatever pin I choose to use for control. For instance if I choose pin5 for my PWM control. I just simply write:&#60;br /&#62;
void pwmWrite(uint8 FanPin, uint16 37768)&#60;/p&#62;
&#60;p&#62;would this be what I need to control the PWM at half the rate?&#60;br /&#62;
And yes, how would I set the frequency?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5747</link>
			<pubDate>Tue, 26 Jul 2011 11:32:41 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">5747@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I'm not sure what you mean.&#60;/p&#62;
&#60;p&#62;The Maple runs at 72MHz. So the shortest possible pulse is much slower than 300MHz.&#60;/p&#62;
&#60;p&#62;The duty cycle of a PWM signal is set with pwmWrite&#60;br /&#62;
&#60;a href=&#34;http://leaflabs.com/docs/lang/api/pwmwrite.html#lang-pwmwrite&#34; rel=&#34;nofollow&#34;&#62;http://leaflabs.com/docs/lang/api/pwmwrite.html#lang-pwmwrite&#60;/a&#62;&#60;/p&#62;
&#60;p&#62;The signature is:&#60;br /&#62;
void pwmWrite(uint8 pin, uint16 duty_cycle)&#60;br /&#62;
so the duty-cycle is a number in the range 0 to 65,535, with 0 being continuously off, and 65,535 continuously on. Everything else is duty_cycle periods of the underlying timer's count period.&#60;/p&#62;
&#60;p&#62;Do you mean pwm frequency?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>iRobotGoddard on "PWM dutyCycle"</title>
			<link>http://forums.leaflabs.com/topic.php?id=929#post-5745</link>
			<pubDate>Tue, 26 Jul 2011 11:02:50 +0000</pubDate>
			<dc:creator>iRobotGoddard</dc:creator>
			<guid isPermaLink="false">5745@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Can someone explain how to alter your duty cycle on your PWM pins using the Maple rev5 based on the instrument you are controlling? For example if my pwm controlled component operates on 300MHz but the Maple operates on 16MHz how can we calculate, on the maple, how to change our duty cycle to fit the needs of the component? The issue is correctly programming the maple.
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
