Fast PWM (up to 1.6 MHz)
Phil Pilgrim (PhiPi)
Posts: 23,514
Fast PWM seems to be all the rage lately, so I thought I'd give it a try. The attached object provides PWM rates from 2.44 kHz to 1.6 MHz, with 12.5ns resolution (80 MHz clock) and full 0-100% duty cycles. It requires one pin, one extra cog, and no external circuitry. The on and off times can also be set separately. Changes to the timing parameters are done instantly, without stopping or otherwise interrupting the flow of the output.
Enjoy!
-Phil
Corrected archive attached ...
Improved archive attached ...
Enjoy!
-Phil
Corrected archive attached ...
Improved archive attached ...
Comments
-Phil
Andy
If you do in Spin only, it goes up to 60 kHz @80 MHz clock:
Andy
One correction in the posted code ...
also made the direction |= instead of := so as to not affect other direction bits.
Playing with it, I changed the first two lines in the Start method: and now have fully complementary output from pin and pin+1. That would be applicable say to driving an h-bridge or bridge tied load.
'Nice extension for complementary outputs, BTW!
-Phil
-Phil
One nice application of this technique is embedded in the wave player introduced here. It uses this standard PWM and noise shaping in order to avoid the subharmonic hash noise that arises from counter DUTY mode. In that application of course the technique is buried deep within the sampling loop.
-Phil