Very simple class to give slow speed PWM using timers and digital out.
Example use...
#include "SlowPWM.h" SlowPWM MyTimer1(LED1); SlowPWM MyTimer2(LED2); SlowPWM MyTimer3(LED3); main() { MyTimer1.setPeriod(4); MyTimer1.setHighTime(2); MyTimer1.start(); MyTimer2.setPeriod(2); MyTimer2.setHighTime(1.5); MyTimer2.start(); MyTimer3.setPeriod(3.8); MyTimer3.setHighTime(1.5); MyTimer3.start(); while(true) { wait(1); } }
Revision 2:c90e2d2f52aa, committed 2019-06-21
- Comitter:
- AndyA
- Date:
- Fri Jun 21 09:33:26 2019 +0000
- Parent:
- 1:386d04fe1e37
- Child:
- 3:3f7eb3ad23d4
- Commit message:
- Re-order constructor arguments.; Add special case handling for duty cycles of 0 or 1.; Stop now also cancels any scheduled turn off.
Changed in this revision
SlowPWM.cpp | Show annotated file Show diff for this revision Revisions of this file |
SlowPWM.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/SlowPWM.cpp Tue Jun 18 16:22:56 2019 +0000 +++ b/SlowPWM.cpp Fri Jun 21 09:33:26 2019 +0000 @@ -6,14 +6,14 @@ _timeOn=0.5f; } -SlowPWM::SlowPWM( float period, float highTime, const PinName pin ):DigitalOut(pin) +SlowPWM::SlowPWM( const PinName pin, const float period, const float highTime ):DigitalOut(pin) { setPeriod(period); setHighTime(highTime); start(); } -void SlowPWM::setPeriod(float period) +void SlowPWM::setPeriod(const float period) { _repeatTime = period; if (_repeatTime <= 0) // check it's not 0 or negative @@ -21,22 +21,30 @@ setHighTime(_timeOn); // perform sanity check on high time } -void SlowPWM::setHighTime(float highTime) +void SlowPWM::setHighTime(const float highTime) { _timeOn = highTime; if (_timeOn >= _repeatTime) // check it's not more than the cycle time. _timeOn = _repeatTime/2; // set to 50% if invalid } -void SlowPWM::setDutyCycle(float cycle) { - if ((cycle >=0) && (cycle <=1)) { +void SlowPWM::setDutyCycle(const float cycle) { + if (cycle == 1) { + stop(); + write(1); + } else if (cycle == 0) { + stop(); + write(0); + } else if ((cycle >0) && (cycle <1)) { _timeOn = _repeatTime*cycle; + start(); } else _timeOn = _repeatTime/2; // set to 50% if invalid } void SlowPWM::stop() { + offTimer.detach(); cycleTimer.detach(); }
--- a/SlowPWM.h Tue Jun 18 16:22:56 2019 +0000 +++ b/SlowPWM.h Fri Jun 21 09:33:26 2019 +0000 @@ -2,6 +2,11 @@ #define __SlowPWM_H__ #include "mbed.h" + + +/// A basic class to create very slow PWM signals using Digital IO and timers. +/// If setting a period and high time then you must manually call start. +/// If setting a duty cycle then it will start automatically, make sure you've set the peiod first if you care about it. class SlowPWM : public DigitalOut { public: @@ -10,19 +15,18 @@ SlowPWM( const PinName pin); // constructor that also starts things running - SlowPWM( float period, float highTime, const PinName pin ); + SlowPWM( const PinName pin, const float period, const float highTime); // set the period - void setPeriod(float period); + void setPeriod(const float period); // set the on time per cycle - void setHighTime(float highTime); + void setHighTime(const float highTime); // set the on time per cycle as a fraction - void setDutyCycle(float cycle); - + void setDutyCycle(const float cycle); -// stop things. If output is high it will still turn low at the correct time +// stop things. void stop(); // start things