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

Reasonable frequencies for PWM? Did I screw up my Netduino?

PWM frequency brick

  • Please log in to reply
11 replies to this topic

#1 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 22 January 2013 - 07:17 AM

I have an application where I need to vary the frequency of a 50% duty cycle PWM over 400-1200 Hz. I've been working with the constructor as such, just to wrap my head around how to vary the frequency:

 

using System;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace rawr{    public class Program    {        public static void Main()        {            PWM r = new PWM(PWMChannels.PWM_PIN_D5, 1, .5, false);            r.Start();            Debug.Print("Derp.");            Thread.Sleep(Timeout.Infinite);        }    }}

 

Here the frequency is set to 1Hz. I figured a 50% duty cycle of a 1Hz signal should give the effect of blinking it once every half second. It successfully deployed once (and didn't work), but now I can't deploy any code to my Netduino Plus 2. Did I somehow screw up an important timer? What are the reasonable ranges for PWM frequency?

 

Edit: MFDeploy can't ping my board.

 

Edit edit: after re-flashing the firmware, it's working again, though with a different program. I dare not try to mess with the PWM frequency again.



#2 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 22 January 2013 - 10:28 AM

I'm not sure about this, it's some time ago I last worked with PWM.

But isn't the time set in milliseconds? Something like

PWM r = new PWM(PWMChannels.PWM_PIN_D5, 1000, 500, false);

?

 

I am not sure though.

 

Greets, Markus

 

Please ignore me :P


NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#3 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 January 2013 - 02:32 PM

Hi Verdris, The PWM base clock is 1MHz and the period/duration values are 16-bit. This means that the longest period is around 65,535us (~65ms). If you specify a period which is larger than that, the NETMF classes might get angry. We will take a second look at them and make sure they throw the proper exception instead. Chris P.S. There are ways to change the base clock; we'll see if there's a potential extension to the NETMF PWM class to enable this (which would allow even faster PWM signaling and/or even slower frequencies).

#4 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 22 January 2013 - 04:39 PM

Hi Verdis,    This one stumped me for a bit as well. Hopefully from what I've learned from Chris, Wikipedia, Physics class, and this community, can shed some light on this topic which comes up often.   Most of the PWMs on the Netduino are 16-bit, which means that the largest value you can use for the period, in microseconds, is 65,535. The number 65,535 is the the largest unsigned integer one can have for a 16-bit value ( 2^16 = 65,535). So in this case we need to make sure we don't provide frequencies to the MS PWM constructor that will result in a value great than 65,535 microseconds or more commonly seen as ?s.   To find out what the resulting period will be for any given frequency we can use the formula, period = 1/frequency, where the frequency is measured in hertz (Hz) and the period in seconds.    So let's have a look at your scenario where you would like to have a frequency of 1Hz. Plugging this into our formula we find that we have 1/1 = 1 s. If we convert 1 second to microseconds we have a value of 1,000,000?s. We now can see that 1,000,000?s is much larger than the largest possible value of 65,535?s due to the 16-bit PWM. And if we attempt to use that value the compiler gets incredibly cranky, and in older firmware versions would corrupt the Netduino, though I have found this isn't the case any longer with the latest NP2 firmware.

 

So what is the lowest frequency we can use for the 16-bit PWMs? Well the formula for frequency is 1/period. So if we convert our max period value of 65,535?s, which is 0.06535 seconds, we get ~15.26Hz. Rounding it up to ensure compatibility we get 16Hz as the lowest value. Unfortunately this isn't as low as you would like it, but alas, there is a possible workaround!

 

As I said at the start, "most" PWMs use 16-bit, however if you are using the Netduino Plus 2, the STM32F4 has a 32-bit PWM snuck in there. Looking at the datasheets we see that the 32-bit timer TIM2/TIM5 is on pin PB10. And lucky for us that pin is mapped to D10 on the Netduino Plus 2.

 

So what does this all mean? We now have access to a much larger period, 2^32 = 4,294,967,295?s! And if you try your 1Hz frequency using D10, or PWM_3, it should work since the period of a 1Hz frequency is 1,000,000?s which will fit nicely inside of a 32-bit value. 

 

Hopefully this has shed some light on PWM. Happy hacking!

 

Cheers,

Steve

 

P.S. I'm not near my Netduinos at the moment, so I haven't tested D10 just yet, but it should work in theory. :)



#5 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 22 January 2013 - 06:16 PM

P.S. I'm not near my Netduinos at the moment, so I haven't tested D10 just yet, but it should work in theory. :)

I am near my Netduino and with the 4.2.2 firmware this does in fact give a 1Hz signal on the scope when you use D10.

 

Regards,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#6 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 22 January 2013 - 06:17 PM

Awesome! Thanks for checking Mark. I was getting anxious that I supplied faulty logic and info :D

 

Cheers,

Steve



#7 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 22 January 2013 - 06:26 PM

Steve, what a brilliant and well written post.

#8 Verdris

Verdris

    Advanced Member

  • Members
  • PipPipPip
  • 128 posts
  • LocationReno, NV

Posted 22 January 2013 - 07:45 PM

This community is amazing! When I was working with ChipKit UNO32, it often took weeks to get a halfhearted reply to a forum question. Thanks Steve! Your reply is exactly what all such communities need: a solution to a problem and the explanation to the solution!

 

Anyway, I wrote this for my NP2:

 

PWM derp = new PWM(PWMChannels.PWM_PIN_D10, 1, 0.5, false);derp.Start();Thread.Sleep(Timeout.Infinite);

 

And I'm now staring at a blue LED merrily blinking once a second for half a second. When I get to the office today I'm going to wire up some piezo buzzers and see about getting some pretty tones going.



#9 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 22 January 2013 - 08:57 PM

As I said at the start, "most" PWMs use 16-bit, however if you are using the Netduino Plus 2, the STM32F4 has a 32-bit PWM snuck in there. Looking at the datasheets we see that the 32-bit timer TIM2/TIM5 is on pin PB10. And lucky for us that pin is mapped to D10 on the Netduino Plus 2.

 

Do you also know if the Netduino+ (1, not 2) also has a 32-bit timer? Or does the old one only have 16 bit?

I'm pretty lost in data sheets, so probably you know it :P

 

Thanks,

Markus


NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#10 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 22 January 2013 - 09:24 PM

Hi Markus,
Do you also know if the Netduino+ (1, not 2) also has a 32-bit timer? Or does the old one only have 16 bit? I'm pretty lost in data sheets, so probably you know it :P
Netduino 1 and Netduino Plus 1 have only 16-bit PWMs. The 32-bit PWM is a hardware feature of the new STM32F2/4 micros :) Chris

#11 Strut

Strut

    Member

  • Members
  • PipPip
  • 18 posts

Posted 23 January 2013 - 12:10 AM

Hi Verdris, The PWM base clock is 1MHz and the period/duration values are 16-bit. This means that the longest period is around 65,535us (~65ms). If you specify a period which is larger than that, the NETMF classes might get angry. We will take a second look at them and make sure they throw the proper exception instead. Chris P.S. There are ways to change the base clock; we'll see if there's a potential extension to the NETMF PWM class to enable this (which would allow even faster PWM signaling and/or even slower frequencies).

 

Quick question since I don't have a scope... The following would give me the max frequency of 1MHz @ 50% duty? How much faster could we go with the hardware if enabled by this extension? I need to get somewhere at or above 8MHz for my project.

 

PWM MHZ = new PWM(PWMChannels.PWM_PIN_D10, .000001, 0.5, false);


#12 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 23 January 2013 - 03:00 AM

Hi Strut, The timers can run at up to 42MHz. You could get at least 10MHz if you changed the PWM base clock in the firmware (or via a future extension). Chris





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.