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.