![]() |
Software PWM 1.0
Software PWM for any digital pin
|
#include <MD_PWM.h>
Public Member Functions | |
Class constructor and destructor. | |
MD_PWM (uint8_t pin) | |
~MD_PWM (void) | |
Methods for core object control. | |
bool | begin (uint16_t freq=MAX_FREQUENCY) |
void | write (uint8_t duty) |
void | disable (void) |
bool | enable (void) |
ISR use only - NOT FOR END USERS. | |
void | setPin (void) |
Static Public Attributes | |
static const uint16_t | MAX_FREQUENCY = 300 |
the maximum PWM frequency allowed | |
static const uint8_t | MAX_PWM_PIN = 4 |
total number of concurrent PWM pins that can be used | |
static bool | _bInitialised = false |
ISR - Global vector initialization flag. | |
static volatile uint8_t | _pinCount |
ISR - Number of pins currently configured. | |
static MD_PWM * | _cbInstance [] |
ISR - Callback instance handle per pin slot. | |
Core object for the MD_PWM library
MD_PWM::MD_PWM | ( | uint8_t | pin | ) |
Class Constructor
Instantiate a new instance of the class.
The main function for the core object is to set the internal shared variables and timers to default values.
pin | pin number for this PWM output. |
MD_PWM::~MD_PWM | ( | void | ) |
Class Destructor.
Release any allocated memory and clean up anything else.
If all the instances of the class are closed, then the ISR is disconnected and the timer is stopped.
bool MD_PWM::begin | ( | uint16_t | freq = MAX_FREQUENCY | ) |
Initialize the object.
Initialize the object data. This needs to be called during setup() to set items that cannot be done during object creation.
If this is the first instance of this class, then the ISR code is initialized and the frequency of the hardware timer is set. Subsequent instances do not affect the timer frequency.
freq | the PWM frequency in Hz [0..MAX_FREQUENCY] (first instance only). |
void MD_PWM::disable | ( | void | ) |
Disable PWM output for this pin.
Stops PWM output for the pin related to this class instance. The pin relinquishes its slot allocated for the ISR [0..MAX_PWM_PINS], which can be reused for another pin if needed.
bool MD_PWM::enable | ( | void | ) |
Enables PWM output for this pin.
Starts PWM output for the pin related to this class instance. The pin takes the next available slot allocated for the ISR [0..MAX_PWM_PINS]. If no slots are available, then the method returns false.
void MD_PWM::setPin | ( | void | ) |
Set the output pin (for ISR use only).
Set the output PWM pin based on the current counter value. This is called 255 times during each PWM cycle.
This method is for use only by the TIMERn ISR.
void MD_PWM::write | ( | uint8_t | duty | ) |
Write PWM output value.
Set the PWM output value for the pin. This works like the standard analogWrite() for hardware enabled PWM - 0% duty cycle is 0, 50% is 127 and 100% is 255.
duty | the PWM duty cycle [0..255]. |