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);
    }
}

Files at this revision

API Documentation at this revision

Comitter:
AndyA
Date:
Fri Jun 21 09:45:08 2019 +0000
Parent:
2:c90e2d2f52aa
Commit message:
Added auto start stop to setHighTime;

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
diff -r c90e2d2f52aa -r 3f7eb3ad23d4 SlowPWM.cpp
--- a/SlowPWM.cpp	Fri Jun 21 09:33:26 2019 +0000
+++ b/SlowPWM.cpp	Fri Jun 21 09:45:08 2019 +0000
@@ -1,18 +1,18 @@
 #include "SlowPWM.h"
- 
+
 SlowPWM::SlowPWM(const PinName pin ):DigitalOut(pin)
 {
     _repeatTime=1;
     _timeOn=0.5f;
 }
- 
+
 SlowPWM::SlowPWM( const PinName pin, const float period, const float highTime ):DigitalOut(pin)
 {
     setPeriod(period);
     setHighTime(highTime);
     start();
 }
- 
+
 void SlowPWM::setPeriod(const float period)
 {
     _repeatTime = period;
@@ -20,46 +20,54 @@
         _repeatTime = 1;
     setHighTime(_timeOn);  // perform sanity check on high time
 }
- 
+
 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 
+    if (highTime == 0) {
+        stop();
+        write(0);
+    } else if (highTime >= _repeatTime) {
+        stop();
+        write(1);
+    } else {
+        _timeOn = highTime;
+        start();
+    }
 }
- 
-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; 
+
+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 
+    } else
+        _timeOn = _repeatTime/2; // set to 50% if invalid
 }
- 
+
 void SlowPWM::stop()
 {
     offTimer.detach();
     cycleTimer.detach();
 }
- 
+
 // start things.
 void SlowPWM::start()
 {
     cycleTimer.attach(callback(this,&SlowPWM::onCycleStart),_repeatTime);
     onCycleStart();
 }
- 
+
 void SlowPWM::onTurnOff(void)
 {
     write(0);
 }
- 
+
 void SlowPWM::onCycleStart(void)
 {
     offTimer.attach(callback(this,&SlowPWM::onTurnOff),_timeOn);
diff -r c90e2d2f52aa -r 3f7eb3ad23d4 SlowPWM.h
--- a/SlowPWM.h	Fri Jun 21 09:33:26 2019 +0000
+++ b/SlowPWM.h	Fri Jun 21 09:45:08 2019 +0000
@@ -17,13 +17,13 @@
 // constructor that also starts things running
     SlowPWM( const PinName pin, const float period, const float highTime);
  
-// set the period
+/// set the period
     void setPeriod(const float period);
  
-// set the on time per cycle
+/// set the on time per cycle. If 0 or the period or longer timers are stopped and output set perminently 
     void setHighTime(const float highTime);
 
-// set the on time per cycle as a fraction
+// set the on time per cycle as a fraction of the period.
     void setDutyCycle(const float cycle);
  
 // stop things.