Sunday, 22 December 2013

easyPWM.h, simple, interrupt-light and high-resolution 6-channel software PWM

Working 6-channel PWM

This library is a simple and very resource-friendly software PWM. The library is fully compatible with my Receiver.h library. Both these use the Timer/Counter1 with a prescaler of 8. However, Receiver.h does not use any timer interrupts, which makes it possible for easyPWM.h to use the Timer/Counter1 Compare A interrupt. Only compatible with Arduino Uno or any application using the ATmega328P or register-compatible chip clocked at 16MHz.

The library uses only two interrupts per channel per period, and has half-microsecond resolution. (In theory, practice may differ.)

Included is a whole bunch of examples for Arduino. For the Arduino Uno, channel 0 is Digital pin 8, ranging to channel 5 that is Digital pin 13. Examples included are: ArmSixESC, InitializeAndSet, potentiometerKnob, Sweep.

To use it, first, you initializeSoftwarePWM(). This will cause all channels to output a pulse of 1000┬Ás.
After that, simply updatePulsewidth(channel, value), where value is an integer between 2000 to 4000, measured in half-microseconds pulsewidth. This signal works great with ESC's and hobby servos.

To Do:
The accuracy can be improved by accounting for the calculations in the ISR, not needed though.
Change it so that the amount of pins are customizable. This will be a pain in the ass most likely and is not a priority.

Unzip the archive into arduino_dir/libraries

Note if you want to modify the library:
If you want a 50Hz PWM signal utilizing a longer pulsewidth, adjust the input check in updatePulsewidth(), together with the CHANNEL_DELAY define in the header file.
Keep in mind that CHANNEL_DELAY must be PWM_MAXPULSE + ~10 ticks for safety. This limits your max pulsewidth to around (20 / amount_of_channels) milliseconds.

No comments:

Post a Comment