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.
Reasonable frequencies for PWM? Did I screw up my Netduino?
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.
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. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
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).
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.
NeonMika / Markus VV.
,
Lunddahl
,
NooM
and 1 other like this
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.
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
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. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
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
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
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);
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