per1234 / PalatisSoftPWM

Software PWM library for Arduino
BSD 3-Clause "New" or "Revised" License
18 stars 5 forks source link
arduino arduino-library avr pwm

PalatisSoftPWM

Software PWM library for Arduino

AVR microcontrollers provide hardware PWM on some of the pins but if you need PWM on other pins then it must be implemented in software. This library provides easy and efficient software PWM on any pin. Each channel can be set to a different PWM duty cycle.

Differences from the original library

This is a fork of the excellent https://github.com/Palatis/arduino-softpwm. It is intended to be easier to use while retaining the same level of efficiency and flexibility achieved by the original author.

Installation

Usage

See File > Examples > PalatisSoftPWM for demonstration of library usage.

#define SOFTPWM_OUTPUT_DELAY - Add this line above #include <PalatisSoftPWM.h> for a 1 PWM clock cycle delay between outputs to prevent large in-rush currents.

SOFTPWM_DEFINE_PINn_CHANNEL(CHANNEL) - Configure Arduino pin n for software PWM use. This supports boards that use ATmega328P, ATmega168, ATmega32U4, ATmega2560, ATtiny85, ATmega1284P and related microcontrollers. For other microcontrollers use SOFTPWM_DEFINE_CHANNEL() instead. With ATmega1284P, etc. PalatisSoftPWM will default to the Mighty 1284P standard variant if the specific pinout can't be automatically determined.

SOFTPWM_DEFINE_PINn_CHANNEL_INVERT(CHANNEL) - Depending on your application you may prefer to invert the output of the channel. This will cause PWM level 0 to produce a 100% duty cycle.

SOFTPWM_DEFINE_CHANNEL(CHANNEL, PMODE, PORT, BIT) - Configure a pin for software PWM use. Consult the datasheet for your microcontroller for the appropriate PORT and BIT values for the physical pin. This information is shown in the pinout diagram, for example: ATmega328P datasheet Figure 1-1 found on page 3. If you want to determine the Arduino pin assigned to the physical pin http://www.pighixxx.com/test/pinoutspg/boards/ provides this information for the most popular Arduino boards or you can look at the pins_arduino.h file in the variant folder used by your board. Usage is demonstrated in File > Examples > PalatisSoftPWM > PalatisSoftPWM_example2.

SOFTPWM_DEFINE_CHANNEL_INVERT(CHANNEL, PMODE, PORT, BIT) - Depending on your application you may prefer to invert the output of the channel. This will cause PWM level 0 to produce a 100% duty cycle. See SOFTPWM_DEFINE_CHANNEL() for description of parameters.

SOFTPWM_DEFINE_OBJECT(CHANNEL_CNT) - Define the softPWM object with the default 256 PWM levels.

SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS) - Define the softPWM object with the specified number of PWM levels.

SOFTPWM_DEFINE_EXTERN_OBJECT(CHANNEL_CNT) - Add this if you want to use the SoftPWM object outside where it's defined. See SOFTPWM_DEFINE_OBJECT() for description of the parameter.

SOFTPWM_DEFINE_EXTERN_OBJECT_WITH_PWM_LEVELS(CHANNEL_CNT, PWM_LEVELS) - Add this if you want to use the SoftPWM object outside where it's defined. See SOFTPWM_DEFINE_OBJECT_WITH_PWM_LEVELS() for description of parameters.

PalatisSoftPWM.begin(hertz) - Initialize PalatisSoftPWM.

PalatisSoftPWM.printInterruptLoad() - Prints diagnostic information to the serial monitor. This can be used to find the optimal PWM frequency by setting different PWM frequency values in PalatisSoftPWM.begin() and then checking the resulting interrupt load. Calling this function will momentarily turn off the PWM on all channels.

PalatisSoftPWM.set(channel_idx, value) - Set the PWM level of the given channel.

PalatisSoftPWM.size()

PalatisSoftPWM.PWMlevels()

PalatisSoftPWM.allOff() - Set the PWM value of all channels to 0.

Troubleshooting

Contributing

Pull requests or issue reports are welcome! Please see the contribution rules for instructions.