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

How to Calculate PWM period and duration for known Frequency?


  • Please log in to reply
7 replies to this topic

#1 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 09 June 2011 - 08:55 PM

Still new at all this stuff, but I have encountered a situation where I need to run PWM at 32Khz. Problem is I have no idea how to calculate the correct period and duration to use with SetPulse. If someone can point me in the right direction I would greatly appreciate it. Thanks, Ryan

#2 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 10 June 2011 - 01:31 AM

Someone else will have to help with implementation. PWM runs at a set frequency so you would run at 32kHz. It would require you to change the duty cycle. 32kHz means you would have to create a new pulse every 3.125e-5 seconds. So a 50% duty cycle would have a hi value for half that. Whatever you are connecting with will have some information about what the different duty cycles mean on the that side.

#3 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 10 June 2011 - 09:39 PM

From what I have read elsewhere on the forums PWM runs at a default 10 Khz and that changing duty cycle only affects the default value. But from what I read they said you can change the frequency by using the SetPulse method by giving it the appropriate period and duration.

Maybe some info on what I am trying to do will help. I have picked up a barometric sensor HP03 from Futurlec and it requires a 32 Khz clock input. So I really have 2 choices, figure out how to set a PWN line to 32 Khz or use a 32 Khz crystal. Since I am still new to electronic components and not sure how I would wire in a crystal I thought throwing code at it would be easiest at the moment. But to throw code at it I need to know how I can calculate period and duration. If the easy solution is to wire it to a crystal and someone can share the proper way to do so I would be very glad to hear it.

Thanks,
Ryan

Reference:
PWM Demystified

#4 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 10 June 2011 - 10:17 PM

So I did some reading. The SetPulse does take a period and duration. So period = 1/frequency, so you can set that. Although 32kHz is 31.25uS, and the SetPulse seems to only take integers. Then the duration would be a fraction of your set period. So if I set the period at 32uS and wanted a duty cycle of 50% I'd set the duration at 16uS. see: http://10rem.net/blo...-modulation-pwm Anyone else want to comment on this? Hope this helps.

#5 BitFlipper

BitFlipper

    Advanced Member

  • Members
  • PipPipPip
  • 61 posts

Posted 10 June 2011 - 11:29 PM

/// <summary>
/// Set the dutycycle of the 32kHz signal
/// </summary>
/// <param name="dutyCycle">A value between 0.0f and 100.0f</param>
private void SetPwm(float dutyCycle)
{
    var period = (uint)(1000000.0f / 32000.0f + 0.5f);
    var duration = (uint)(dutyCycle * 0.01f * period + 0.5f); 

    m_pwm.SetPulse(period, duration);
}

Unfortunately this will result in a period of 31.25, which when rounded will be 31, or an actual frequency of 32,258.06 Hz.


EDIT: OK I re-read your post, and seems you only need 50% duty cycle. In that case all you need to do is something like:

m_pwm.SetPulse(31, 15);

Once again unfortunately you can't get exactly 32kHz. I wonder how critical it is to be exactly 32kHz.

#6 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 13 June 2011 - 04:09 PM

Thanks guys, that is the info I am looking for. I hear an future update to PWM is going to have the ability to set a frequency directly which will make this kind of stuff even easier. Thanks again, Ryan

#7 Justinius

Justinius

    Member

  • Members
  • PipPip
  • 15 posts

Posted 13 June 2011 - 09:58 PM

I hope these comments helped. If you get the sensor up and running document it in the Wiki, if you can. It would be a good guide.

#8 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 13 June 2011 - 10:42 PM

Funny thing is I never noticed the wiki link at the top before. Will do, I am also working with the HH10D humidity which takes some extra steps to get working as well. When it's all working well I will put them both in the wiki.




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.