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

		<item>
			<title>dezulm on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858&amp;page=2#post-8579</link>
			<pubDate>Sun, 11 Mar 2012 14:02:59 +0000</pubDate>
			<dc:creator>dezulm</dc:creator>
			<guid isPermaLink="false">8579@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;It seems that signal from function generator is not working.  Although looks great on scope it being read correctly. I generated a signal on the board and jumpered.  This works.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>dezulm on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858&amp;page=2#post-8578</link>
			<pubDate>Sat, 10 Mar 2012 11:04:11 +0000</pubDate>
			<dc:creator>dezulm</dc:creator>
			<guid isPermaLink="false">8578@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I have included the code in the pastebin.  The external interrupt handler gets called.  The timer interrupt does not do anything. The pulse width and period values are completely off.  I had another version where I just use micros() and that one also had bad values for period and pulse width.  I am injecting a 1khz 50% duty cycle PWM on pin 0 (3.2 V amplitude and dc offset of 1.6).  I want to see if the period and pulse width counts being displayed reflect 1000 us and 500 us for pulse width.&#60;/p&#62;
&#60;p&#62;&#60;a href=&#34;http://pastebin.com/FK6gXYKk&#34; rel=&#34;nofollow&#34;&#62;http://pastebin.com/FK6gXYKk&#60;/a&#62;
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858&amp;page=2#post-8577</link>
			<pubDate>Sat, 10 Mar 2012 09:16:38 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">8577@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;dezulm - I took the liberty of formatting your code, to make it easier for me to read. I hope that is okay.&#60;/p&#62;
&#60;p&#62;I don't think I understand what is happening or not happening. Would you please explain? &#60;/p&#62;
&#60;p&#62;Maybe you could paste all the code at pastebin or somewhere?&#60;/p&#62;
&#60;p&#62;Is &#60;code&#62;timer1_handler&#60;/code&#62; getting called?&#60;br /&#62;
Is &#60;code&#62;exti_handler&#60;/code&#62; getting called?&#60;/p&#62;
&#60;p&#62;(You could test these by flipping a pin in the handler, and observing the pin or pins with your oscilloscope)&#60;/p&#62;
&#60;p&#62;(Full disclosure: I am not a member of LeafLabs staff)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>dezulm on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858&amp;page=2#post-8576</link>
			<pubDate>Sat, 10 Mar 2012 08:57:32 +0000</pubDate>
			<dc:creator>dezulm</dc:creator>
			<guid isPermaLink="false">8576@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I have an external interrupt that kick offs when a pin change occurs.  In the the handler, I do timer1.getCount to determine the period and pulse width.   The signal that I have as input is 1 kHZ 50% duty cycle.  I set up the timer in the setup() as follows:&#60;br /&#62;
&#60;pre&#62;&#60;code&#62;// ...
    timer1.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    timer1.pause();
    timer1.setCount(0);
    timer1.setPrescaleFactor(72);  //run at 72 MHz/72 = 1MHz count every 1us
    timer1.setOverflow(65535);
    timer1.setCompare1(1);
    timer1.attachCompare1Interrupt(timer1_handler);
    timer1.refresh();
    timer1.resume();

Sample handler code:

void exti_handler()
{
    timer1.pause();
    CurrentCount = timer1.getCount();
    val = digitalRead(0);

    if (val == HIGH)
    {
        LowToHighCount++;
        Period = CurrentCount;
        timer1.setCount(0);
        timer1.refresh();
    }
    else { // state must be LOW
        HighToLowCount++;
        PulseWidth = CurrentCount;
    }

    timer1.resume();
}&#60;/code&#62;&#60;/pre&#62;
&#60;p&#62;timer interrupt is just adding up a timercounter which does nothing&#60;/p&#62;
&#60;p&#62;Any guidance or thoughts?
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-8544</link>
			<pubDate>Fri, 09 Mar 2012 10:53:05 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">8544@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;dezulm - it sounds like you have enough equipment, understanding and a sound plan to make this work.&#60;/p&#62;
&#60;p&#62;You should be able to see the approximate PWM frequency with your scope, and know what you are dealing with. Do you know approximately  what PWM frequency is used, and what level of precision are you aiming for?&#60;/p&#62;
&#60;p&#62;The sort of DC motors I am aware of tend to run with a PWM frequency below 100kHz (switching losses become an issue for the sort of low-cost drivers I use much above that). If that is your situation you could do a prototype in relatively simple software by polling (repeatedly reading) a pin, and using a timer to give the PWM duty cycle.&#60;br /&#62;
Set the timer prescaler to 0 so that the timer is counting at 72MHz.&#60;/p&#62;
&#60;p&#62;If you read the forum threads, you'll see that it is relatively straightforward to 'poll' a pin at, about, 12MHz using 'bit band addressing'. So you could do a 'quick and dirty' prototype using only software and a suitably configured timer as a 'stop watch'. &#60;/p&#62;
&#60;p&#62;The other interrupts which will distort your results are the systick timer, which triggers every millisecond, and USB gets polled at a similar rate. So you won't get every PWM cycle until they are switched off (which you can do), but even with them running, it might be enough to prove the concept.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>dezulm on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-8540</link>
			<pubDate>Fri, 09 Mar 2012 08:08:51 +0000</pubDate>
			<dc:creator>dezulm</dc:creator>
			<guid isPermaLink="false">8540@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Thank you for summary and analysis. Glad to know that things can be kept simple assuming the frequencies are not quite high. I have a scope and a function generator.  For testing I was planning on using the function generator to create PWM signals.   The PWM signals are driving  motors.  The board is just for measuring PWM signals.  The PWM signals will be measured in terms of duty cycle and period before they can be connected to drive the motors.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>gbulmer on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-8533</link>
			<pubDate>Fri, 09 Mar 2012 05:22:08 +0000</pubDate>
			<dc:creator>gbulmer</dc:creator>
			<guid isPermaLink="false">8533@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;dezulm -&#60;/p&#62;
&#60;blockquote&#62;&#60;p&#62;Do I need DMA to buffer the captured info? Does the DMA need to point to just timer 1 or do I need to setup a DMA buffer for timer 2?&#60;/p&#62;
&#60;/blockquote&#62;
&#60;p&#62;Approximately, how fast is the signal, how precise or accurate must it be measured, and is there a lot of other interrupts or code to run? Must your system capture the timing of every cycle, or can it drop some?&#60;/p&#62;
&#60;p&#62;Have you got access to test equipment, e,g, oscilloscope or storage scope, and do you know how to use it? If you have test equipment, it will be much easier to check that stuff is working correctly.&#60;/p&#62;
&#60;p&#62;This is my process:&#60;br /&#62;
0. Make a test system using a button, or 2nd microcontroller. It'll be much easier to test an artificial signal than a real one. Feed the artificial signal into the pin you plan on using for the 'real' solution to avoid introducing bugs later.&#60;br /&#62;
1. Try to make a protoype work with pure software (e.g. digitalRead and time functions), against the artificial signal. It will likely be easiest to solve this way anyway unless the cycle time is under 100 microseconds, or better than 1 microsecond precision is needed, or there are other interrupts (the usual problem is USB, but that can be disabled).&#60;br /&#62;
2. If the cycle time is 100us to 10us, consider using a 'pin change' interrupt for a prototype. Look-up the time on a timer. There was a recent thread where someone did that (within last three weeks?)&#60;br /&#62;
3. If the cycle time is sub 10us, or sub 100us and there is other interrupt or time-critical activity, it is probably worth using a timer input capture mode, so read the general purpose timer chapter (15) in RM008 (most of it applies to Timer 1 as well as Timer 2).&#60;br /&#62;
4. Use the HardwareTimer documentation (&#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;) to get a timer into a useful state for measurements. The HardwareTimer functions will let you set the count rate of the timer right for timing the signal. Carefully read the documentation because resetting the timer (after a match) needs to be done right. Look at the source of HardwareTimer, and you'll see how to access the timers. The code should match the RM0008 documentation quite closely.&#60;br /&#62;
5. Many threads on the forum are from people struggling to use DMA. It adds enough complexity that is seems to be worth avoiding  if practical. If the cycle time of the signal is more than 10us, you probably don't need DMA unless there is plenty of other interrupt, or time-critical activity.&#60;/p&#62;
&#60;p&#62;Summary&#60;br /&#62;
Build an easy to control test rig. Debugging real real-time code can be frustrating and difficult.&#60;/p&#62;
&#60;p&#62;If it is something like a RC servo signal (frequency about 50Hz), which is okay with 0.1% precision, and not much else happening (e.g. no USB), then software should be enough.&#60;/p&#62;
&#60;p&#62;If the frequency is quite high (10kHz), or there are other interrupts or time-critical stuff happening, but you can service the interrupt, then there is no need for DMA.&#60;br /&#62;
If the frequency is faster than 100kHz, and there are other interrupts, and no signal can be lost, then DMA will eventually be needed, but you can get quite a lot working and tested at slower speeds in a test rig.&#60;/p&#62;
&#60;p&#62;(Full disclosure: I am not a member of LeafLabs staff)
&#60;/p&#62;</description>
		</item>
		<item>
			<title>dezulm on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-8529</link>
			<pubDate>Thu, 08 Mar 2012 21:35:00 +0000</pubDate>
			<dc:creator>dezulm</dc:creator>
			<guid isPermaLink="false">8529@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;I need to measure the pulse width, duty cycle, and period of a PWM signal.  I want to use the PWM input mode which like Input Capture Mode but with timer 1 and timer 2 as slave.  Does anyone have sample code on this?  &#60;/p&#62;
&#60;p&#62;Do I need DMA to buffer the captured info?  Does the DMA need to point to just timer 1 or do I need to setup a DMA buffer for timer 2?&#60;/p&#62;
&#60;p&#62;I am a newbie and I appreciate any guidance and pointers.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5205</link>
			<pubDate>Tue, 14 Jun 2011 11:52:10 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">5205@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;fair enough. You could always run as many channels as you could with input capture mode and do the remaining ones with extis
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5204</link>
			<pubDate>Tue, 14 Jun 2011 11:39:09 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">5204@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;Currently there is a small amount of processing that is done in the interrupt function, but removing as much overhead is desirable at this stage due to the nature of the system. The Kalman filter which is currently implemented (I still need to work on it) takes 700uS approximately to process, with more calculation that still need to be done later on, and I'm trying to get all functions to run in less than 1ms to increase the aircraft stability. I don't mind a bit of extra work if it means I save 50-150 cycles later. It would be desirable if I could offload most of this stuff to the hardware.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>poslathian on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5198</link>
			<pubDate>Tue, 14 Jun 2011 10:32:00 +0000</pubDate>
			<dc:creator>poslathian</dc:creator>
			<guid isPermaLink="false">5198@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;How much CPU is it really taking up? 6 external interrupts, each doing little more than latching the current micros() value? Its true there is a certain amount of entry/exit overhead, but I cant imagine you are paying more than 1000 instructions to latch all 6 channels, and thats only every 20ms, in which time you get 1.4M instructions. &#60;/p&#62;
&#60;p&#62;Thus, the PWM measurement shouldnt be taking a lot more than 1/1000 of your total processor resources. Even if it were taking 1/100 of the time (10,000 instructions to enter and exit 6 interrupts and call millis() 6 times...) you wouldnt be paying much.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5193</link>
			<pubDate>Tue, 14 Jun 2011 10:17:18 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">5193@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;The ideal situation is I'd use a CPLD or FPGA to measure the PWM pulses and interface with the Maple with SPI. However, I don't have access to one, and while I have multiple micros available for use, I'd prefer to have a single one on the aircraft if it can handle all data collection and processing. If it can't, then the controller board will have additional microcontrollers, and yes, I realise for my board, I have a Razer IMU with has an ATMEGA328P on it.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>robodude666 on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5190</link>
			<pubDate>Tue, 14 Jun 2011 10:05:33 +0000</pubDate>
			<dc:creator>robodude666</dc:creator>
			<guid isPermaLink="false">5190@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;How about using two MCUs?&#60;/p&#62;
&#60;p&#62;Say, for example, an Arduino or STM Discovery to handle software-based PWM capture. It can then communicate with the &#34;master&#34; Maple via SPI and (18Mbps SPI!) send its findings over. &#60;/p&#62;
&#60;p&#62;This way the &#34;master&#34; Maple only worries about processing the data rather than capturing it. The SPI communication can be handled via a DMA controller to reduce CPU usage.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>crenn on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5185</link>
			<pubDate>Tue, 14 Jun 2011 02:19:18 +0000</pubDate>
			<dc:creator>crenn</dc:creator>
			<guid isPermaLink="false">5185@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;That's how it's working currently, however it's using up CPU time to do it that way. Something I want to try to avoid as much as possible.
&#60;/p&#62;</description>
		</item>
		<item>
			<title>Darth Maker on "PWM Capture without using the PWM Capture timer feature"</title>
			<link>http://forums.leaflabs.com/topic.php?id=858#post-5184</link>
			<pubDate>Tue, 14 Jun 2011 01:02:22 +0000</pubDate>
			<dc:creator>Darth Maker</dc:creator>
			<guid isPermaLink="false">5184@http://forums.leaflabs.com/</guid>
			<description>&#60;p&#62;You could just use the external interrupts and compare times from micros() (which I think now works?)&#60;/p&#62;
&#60;p&#62;This would use an already running timer, so you don't lose anything but the interrupt on three other pins.
&#60;/p&#62;</description>
		</item>

	</channel>
</rss>
