Software PWM 1.0
Software PWM for any digital pin
MD_PWM Class Reference

#include <MD_PWM.h>

Collaboration diagram for MD_PWM:

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.
 

Detailed Description

Core object for the MD_PWM library

Constructor & Destructor Documentation

◆ MD_PWM()

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.

Parameters
pinpin number for this PWM output.

◆ ~MD_PWM()

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.

Member Function Documentation

◆ begin()

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.

See also
enable()
Parameters
freqthe PWM frequency in Hz [0..MAX_FREQUENCY] (first instance only).
Returns
true if the pin was successfully enabled for PWM.

◆ disable()

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.

See also
enable()

◆ enable()

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.

See also
disable()
Returns
true if the pin was successfully enabled.

◆ setPin()

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.

◆ write()

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.

Parameters
dutythe PWM duty cycle [0..255].

The documentation for this class was generated from the following files: