Basic pulse width modulation support. More...
#include <HardwarePWM.h>
Public Member Functions | |
| HardwarePWM (const uint8_t *pins, uint8_t no_of_pins) | |
| Instantiate hardware PWM object. More... | |
| virtual | ~HardwarePWM () |
| bool | analogWrite (uint8_t pin, uint32_t duty) |
| Set PWM duty cycle. More... | |
| bool | setDutyChan (uint8_t chan, uint32_t duty, bool update=true) |
| Set PWM duty cycle for a channel. More... | |
| bool | setDuty (uint8_t pin, uint32_t duty, bool update=true) |
| Set PWM duty cycle. More... | |
| uint32_t | getDutyChan (uint8_t chan) const |
| Get PWM duty cycle. More... | |
| uint32_t | getDuty (uint8_t pin) |
| Get PWM duty cycle. More... | |
| void | setPeriod (uint32_t period) |
| Set PWM period. More... | |
| uint32_t | getPeriod () const |
| Get PWM period. More... | |
| uint8_t | getChannel (uint8_t pin) const |
| Get channel number for a pin. More... | |
| uint32_t | getMaxDuty () const |
| Get the maximum duty cycle value. More... | |
| void | update () |
| This function is used to actually update the PWM. More... | |
| uint32_t | getFrequency (uint8_t pin) const |
| Get PWM Frequency. More... | |
Detailed Description
Basic pulse width modulation support.
PERIOD: A full PWM cycle has a duration, measured in hardware timer counts DUTY: The 'ON' portion of a PWM cycle measured in hardware timer counts
These values differ between architectures.
DUTY is always <= PERIOD. If DUTY == 0 then output is OFF. If DUTY == PERIOD then output is ON. Intermediate values produce a proportional output, PERCENT = 100 * DUTY / PERIOD.
Producing an analogue voltage from such a PWM output requires a suitable filter with characteristics tuned to the specific PWM frequency in use. A single-pole R/C filter with R*C = PERIOD (in seconds) is often sufficient.
Note: Devices such as servo motors use the digital signal directly.
Note: The Esp8266 has no PWM hardware and uses an interrupt-based approach (software PWM).
Note: Esp32 and Rp2040 hardware offers more capability which requires use of SDK calls or direct hardware access. To avoid resource conflicts avoid using this class in those cases.
Constructor & Destructor Documentation
◆ HardwarePWM()
| HardwarePWM::HardwarePWM | ( | const uint8_t * | pins, |
| uint8_t | no_of_pins | ||
| ) |
Instantiate hardware PWM object.
- Parameters
-
pins Pointer to array of pins to control no_of_pins Quantity of elements in array of pins
◆ ~HardwarePWM()
|
virtual |
Member Function Documentation
◆ analogWrite()
|
inline |
Set PWM duty cycle.
- Parameters
-
pin GPIO to set duty Value of duty cycle to set pin to
- Return values
-
bool True on success
- Note
- Default frequency is 1khz but can be varied by various function
◆ getChannel()
| uint8_t HardwarePWM::getChannel | ( | uint8_t | pin | ) | const |
Get channel number for a pin.
- Parameters
-
pin GPIO to interrogate
- Return values
-
uint8_t Channel of GPIO
◆ getDuty()
|
inline |
Get PWM duty cycle.
- Parameters
-
pin GPIO to get duty cycle for
- Return values
-
uint32_t Value of PWM duty cycle in timer ticks
◆ getDutyChan()
| uint32_t HardwarePWM::getDutyChan | ( | uint8_t | chan | ) | const |
Get PWM duty cycle.
- Parameters
-
chan Channel to get duty cycle for
- Return values
-
uint32_t Value of PWM duty cycle in timer ticks
◆ getFrequency()
| uint32_t HardwarePWM::getFrequency | ( | uint8_t | pin | ) | const |
Get PWM Frequency.
- Parameters
-
pin GPIO to get frequency for
- Return values
-
uint32_t Value of Frequency
◆ getMaxDuty()
|
inline |
Get the maximum duty cycle value.
- Return values
-
uint32_t Maximum permissible duty cycle in timer ticks
- Note
- Attempt to set duty of a pin above this value will fail
◆ getPeriod()
| uint32_t HardwarePWM::getPeriod | ( | ) | const |
Get PWM period.
- Return values
-
uint32_t Value of PWM period in microseconds
◆ setDuty()
|
inline |
Set PWM duty cycle.
- Parameters
-
pin GPIO to set duty Value of duty cycle (timer ticks) to set pin to update Update PWM output
- Return values
-
bool True on success
- Note
- This function is used to set the pwm duty cycle for a given pin. If parameter 'update' is false then you have to call update() later to update duties.
◆ setDutyChan()
| bool HardwarePWM::setDutyChan | ( | uint8_t | chan, |
| uint32_t | duty, | ||
| bool | update = true |
||
| ) |
Set PWM duty cycle for a channel.
- Parameters
-
chan Channel to set duty Value of duty cycle to set channel to update Update PWM output
- Return values
-
bool True on success
◆ setPeriod()
| void HardwarePWM::setPeriod | ( | uint32_t | period | ) |
Set PWM period.
- Parameters
-
period PWM period in microseconds
- Note
- All PWM pins share the same period
◆ update()
| void HardwarePWM::update | ( | ) |
This function is used to actually update the PWM.
The documentation for this class was generated from the following file:
1.9.1