While doing it that way is an option, I was always teached it is better to be lazy than tired. So I simply decided to make a standard PWM object and let that set all the stuff like pin settings, and only change the compare match registers manually. Maybe not the nicest method, but definately easier (not so hard to do it with registers, but finding out which register you need is always irritating).
Anyway I *think* the library should work:
Library that allows for higher resolution and speed than standard mbed PWM library using same syntax (drop-in replacement).
Few remarks, most important you can just directly replace mbed functions with this, you should not need to change anything (besides including the library and making the PwmOut object a FastPWM object). The library changes the clock speed of the PWM hardware, the divide by four is removed. This gives four times more resolution, but that happens also for PwmOut objects in the same program, so they also run four times faster. Of course in the FastPWM library this is compensated, if you tell the period should be 1 second, it will be one second. If you have a PwmOut object and you tell it its period should be 1 second, it will be 0.25 seconds.
Finally I found at least one reason why the standard resolution is not better: floating point precision. A 32-bit float simply lacks precision. So FastPWM uses doubles for everything. You can still send a float to it, the compiler will cast that automatically, but then you will lack precision again. (If you use for example PWM.period(0.0001321312); it will be a double automatically).
So let me know if it works. I did check it with a LED and checking register values, but I did not connect it to a scope. Resolution is 1/clock frequency, by default 96MHz, but if you use a higher clock you can define it in your code and the library will do the calculations with your value.
Btw this only works for LPC1768, I assume you got that one. Since I don't have the other one I cannot even compile for it, so kinda hard to add that functionality. Also it has one aditional function that normal PWM library does not have, you can write microsecond pulsewidth/period also as double (/float).
Hi All,
I've been struggling with getting pwm signals generated with resolutions less than the microsecond level. Is this even possible?
For example, if I want a period of 25 microseconds at a 50% duty cycle, it's easy:
myPWM.period(.000025); myPWM.pulsewidth(.0000125);
Now, if I want a period of 25.x (e.g., 25.5 us), everything just gets truncated. So it basically steps between 40 Khz, to 41.67 Khz (24us), and up in increments of +- 1us. Anyway to get any higher resolution (e.g., at the nano second level)?
Thanks, mrtm3050