Posted 28 October 2010 - 03:52 PM
Hello All. First post - great community.
I started playing with the PWM function yesterday and quickly realised that the posts I read never really came up with a definitive answer to how to use the period and duration parameters (and the limits on their values).
I played with the LED dimmer contributed by 'dab' on 16th August, and it worked fine and all made sense. This uses the SetDutyCycle(duty) method, where the clock free-runs at 10kHz, and the duty cycle is expressed as a percentage [duty takes integer values 0-100]. duty = 100 has it on all the time, duty = 0 has it off all the time.
So far so good.
Then I started using the SetPulse(period, duration) method. I'd seen other posts which indicated that the values of period and duration changed the characteristics of the pulse, but they all used huge numbers (allegedly in nanoseconds). These didn't always work, and gave incorrect periods and durations. I spent an afternoon with an oscilloscope trying different values and came to the following conclusions:
period and duration can only take values in the range 0 - 65535 (they are 16 bit unsigned integers).
These period and duration values represent microseconds
duration must be smaller than period, or ouput appears to be always ON
If you use values > 65535, it MAY work (the upper bits in the uint may be ignored), but the values will not be what you expect.
Thus, SetPulse(10000, 1000) produces a pulse every 10 ms (100 Hz) of duration 1 ms (a duty cycle of 10%)
The lowest frequency you can get is when period = 65535, giving period of 65.5 ms (=15.2 Hz)
The highest frequency you can get is SetPulse(2,1): period = 2 us (500 kHz) duty cycle 50%.
I hope this helps.
I keep reading promises of documentation . . . . c'mon chaps