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);
    }
}
Committer:
AndyA
Date:
Fri Jun 21 09:33:26 2019 +0000
Revision:
2:c90e2d2f52aa
Parent:
1:386d04fe1e37
Child:
3:3f7eb3ad23d4
Re-order constructor arguments.; Add special case handling for duty cycles of 0 or 1.; Stop now also cancels any scheduled turn off.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndyA 1:386d04fe1e37 1 #include "SlowPWM.h"
AndyA 0:76861123625b 2
AndyA 1:386d04fe1e37 3 SlowPWM::SlowPWM(const PinName pin ):DigitalOut(pin)
AndyA 0:76861123625b 4 {
AndyA 0:76861123625b 5 _repeatTime=1;
AndyA 0:76861123625b 6 _timeOn=0.5f;
AndyA 0:76861123625b 7 }
AndyA 0:76861123625b 8
AndyA 2:c90e2d2f52aa 9 SlowPWM::SlowPWM( const PinName pin, const float period, const float highTime ):DigitalOut(pin)
AndyA 0:76861123625b 10 {
AndyA 0:76861123625b 11 setPeriod(period);
AndyA 0:76861123625b 12 setHighTime(highTime);
AndyA 0:76861123625b 13 start();
AndyA 0:76861123625b 14 }
AndyA 0:76861123625b 15
AndyA 2:c90e2d2f52aa 16 void SlowPWM::setPeriod(const float period)
AndyA 0:76861123625b 17 {
AndyA 0:76861123625b 18 _repeatTime = period;
AndyA 0:76861123625b 19 if (_repeatTime <= 0) // check it's not 0 or negative
AndyA 0:76861123625b 20 _repeatTime = 1;
AndyA 0:76861123625b 21 setHighTime(_timeOn); // perform sanity check on high time
AndyA 0:76861123625b 22 }
AndyA 0:76861123625b 23
AndyA 2:c90e2d2f52aa 24 void SlowPWM::setHighTime(const float highTime)
AndyA 0:76861123625b 25 {
AndyA 0:76861123625b 26 _timeOn = highTime;
AndyA 0:76861123625b 27 if (_timeOn >= _repeatTime) // check it's not more than the cycle time.
AndyA 0:76861123625b 28 _timeOn = _repeatTime/2; // set to 50% if invalid
AndyA 0:76861123625b 29 }
AndyA 0:76861123625b 30
AndyA 2:c90e2d2f52aa 31 void SlowPWM::setDutyCycle(const float cycle) {
AndyA 2:c90e2d2f52aa 32 if (cycle == 1) {
AndyA 2:c90e2d2f52aa 33 stop();
AndyA 2:c90e2d2f52aa 34 write(1);
AndyA 2:c90e2d2f52aa 35 } else if (cycle == 0) {
AndyA 2:c90e2d2f52aa 36 stop();
AndyA 2:c90e2d2f52aa 37 write(0);
AndyA 2:c90e2d2f52aa 38 } else if ((cycle >0) && (cycle <1)) {
AndyA 0:76861123625b 39 _timeOn = _repeatTime*cycle;
AndyA 2:c90e2d2f52aa 40 start();
AndyA 0:76861123625b 41 } else
AndyA 0:76861123625b 42 _timeOn = _repeatTime/2; // set to 50% if invalid
AndyA 0:76861123625b 43 }
AndyA 0:76861123625b 44
AndyA 1:386d04fe1e37 45 void SlowPWM::stop()
AndyA 0:76861123625b 46 {
AndyA 2:c90e2d2f52aa 47 offTimer.detach();
AndyA 0:76861123625b 48 cycleTimer.detach();
AndyA 0:76861123625b 49 }
AndyA 0:76861123625b 50
AndyA 0:76861123625b 51 // start things.
AndyA 1:386d04fe1e37 52 void SlowPWM::start()
AndyA 0:76861123625b 53 {
AndyA 1:386d04fe1e37 54 cycleTimer.attach(callback(this,&SlowPWM::onCycleStart),_repeatTime);
AndyA 0:76861123625b 55 onCycleStart();
AndyA 0:76861123625b 56 }
AndyA 0:76861123625b 57
AndyA 1:386d04fe1e37 58 void SlowPWM::onTurnOff(void)
AndyA 0:76861123625b 59 {
AndyA 0:76861123625b 60 write(0);
AndyA 0:76861123625b 61 }
AndyA 0:76861123625b 62
AndyA 1:386d04fe1e37 63 void SlowPWM::onCycleStart(void)
AndyA 0:76861123625b 64 {
AndyA 1:386d04fe1e37 65 offTimer.attach(callback(this,&SlowPWM::onTurnOff),_timeOn);
AndyA 0:76861123625b 66 write(1);
AndyA 0:76861123625b 67 }