Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Trying to make sense of PWM


  • Please log in to reply
7 replies to this topic

#1 Pete Brown

Pete Brown

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationAnne Arundel County, MD

Posted 27 September 2010 - 07:29 AM

Hi All

Warning: I am an electronics newbie, but I've been programming for about a zillion years.

I'm trying to make sense of how PWM works with Netduino. Through playing around (couldn't find any definitive info **), I think I somewhat understand, but would like to confirm.

  • SetDutyCycle takes 0 to 100 as the cycle. This is a percentage of "on" time.
  • SetPulse takes period and duration, both expressed as microseconds

FWIW, duty cycle doesn't appear to be giving me what I'm looking for. If I set it before SetPulse, I get no effect: duty cycle always seems to be about 60%. If I set it after, I get "weirdness".

I assume duration is how long to generate the pulse? At the end of the duration, it looks like the pin remains high. Is that correct? Is there an infinite timeout option, or do I need to loop as seen in this code? I ask because at high pulse frequencies, I'm going to get hiccups whenenver the functions are called again and the loop comes back around. I can see it on my scope. (Yes, noob with a scope is a very dangerous thing <g>)

public static void Main()
{
    PWM pwm = new PWM(Pins.GPIO_PIN_D5);

    const uint period = 2 * 1000 * 1000;    // 2 ms
    const uint duration = 5 * 1000 * 1000;  // 5ms
    const int dutyCycle = 50;

    pwm.SetDutyCycle(dutyCycle);

    while (true)
    {
        pwm.SetPulse(period, duration);

        Thread.Sleep((int)duration/1000);
    }       
}

The period is the standard peak-to-peak period of the pulse, correct?

If I'm correct, given that the period is expressed in microseconds, does this mean I could not use PWM as a 1.04mhz clock pulse for another chip?

Thanks.

Pete
http://10rem.net

** recommendation: use XML comments on the netduino libs so object browser provides some limited documentation on the parameters.
Pete Brown - http://10rem.net (NETMF, C++, Windows, C64, and general geekery) Twitter: @pete_brown
I work for Microsoft. Opinions expressed here are my own and do not necessarily reflect those of my employer,our partners or customers.

#2 Szymon

Szymon

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts
  • LocationPoland, Krakow

Posted 27 September 2010 - 07:53 AM

Hi Pete,

Did you look at this thread
http://forums.netdui...__fromsearch__1


Actually I think this explains it better http://forums.netdui..._2411#entry2411

#3 Pete Brown

Pete Brown

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationAnne Arundel County, MD

Posted 27 September 2010 - 07:56 AM

Hi Pete,
Did you look at this thread
http://forums.netdui...ch__1#entry2560


Thanks. I did (and anything else that mentioned PWM or SetPulse or SetDutyCycle <g>). It didn't seem to answer my question except that more precise frequency possibilities may be coming next month (not sure how close it'll get to 1mhz, though)

[edit]
Looked at your second link too. I'll need to check the scope again tomorrow and see if I misread regarding the duration. That doesn't explain the DutyCycle though. In all the examples, duty cycle is used just to turn the thing off :)

Pete
Pete Brown - http://10rem.net (NETMF, C++, Windows, C64, and general geekery) Twitter: @pete_brown
I work for Microsoft. Opinions expressed here are my own and do not necessarily reflect those of my employer,our partners or customers.

#4 Szymon

Szymon

    Advanced Member

  • Members
  • PipPipPip
  • 108 posts
  • LocationPoland, Krakow

Posted 27 September 2010 - 08:12 AM

According to this post the PWM is currently set to fixed clock of 10KHz http://forums.netdui...findpost__p__42 Current implementation of PWM.SetDutyCycle uses values in range 0 from 100 to express the duration of high state in the cycle as percentage. So duty cycle 0 is always off and duty cycle of 100 is always on. But if I understand correctly this is with the default 10KHz clock. On the other hand PWM.SetPulse lets you control the period and duration of the waveform directly. So you should use one of the functions - not both at the same time. Chris also mentioned they want to expose more PWM functions in next firmware.

#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 September 2010 - 01:20 PM

Yes, you'll be able to set the base PwmClocks in the v4.1.1 firmware. The next alpha/beta is coming soon (+ SD speed improvements)... Chris

#6 Pete Brown

Pete Brown

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationAnne Arundel County, MD

Posted 27 September 2010 - 05:03 PM

Thanks. Those were key things I missed: the 10khz clock rate, and how the functions are either/or. I couldn't for the life of me figure out how SetDutyCycle was figuring out a clock rate :) Chris, saw your replies in the other threads as well. I'm glad to hear PWM is getting upgraded in 4.1.1. Consider this a vote for 1mhz+ PWM :) Pete
Pete Brown - http://10rem.net (NETMF, C++, Windows, C64, and general geekery) Twitter: @pete_brown
I work for Microsoft. Opinions expressed here are my own and do not necessarily reflect those of my employer,our partners or customers.

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 September 2010 - 05:19 PM

Hi Pete, One of the nice things about the Atmel processor is that we have 16-bit PWM on all four channels... And in theory, you can set the clock up to 48MHz if desired :) Also... there are _two_ PWM clocks on the AT91SAM7X processor; the new firmware will let you set each one independently--and then choose which clock to use for each PWM output. Chris

#8 Pete Brown

Pete Brown

    Advanced Member

  • Members
  • PipPipPip
  • 138 posts
  • LocationAnne Arundel County, MD

Posted 27 September 2010 - 05:50 PM

Thanks again.

I completed the PWM intro post I was working on last night. Feel free to correct me if I said something incorrect/dumb :)

Pete
Pete Brown - http://10rem.net (NETMF, C++, Windows, C64, and general geekery) Twitter: @pete_brown
I work for Microsoft. Opinions expressed here are my own and do not necessarily reflect those of my employer,our partners or customers.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.