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:
Tue Jun 18 10:52:51 2019 +0000
Revision:
0:76861123625b
Child:
1:386d04fe1e37
Initial commit.;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AndyA 0:76861123625b 1 #include "slowPWM.h"
AndyA 0:76861123625b 2
AndyA 0:76861123625b 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 0:76861123625b 9 slowPWM::slowPWM( float period, float highTime, const PinName pin ):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 0:76861123625b 16 void slowPWM::setPeriod(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 0:76861123625b 24 void slowPWM::setHighTime(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 0:76861123625b 31 void slowPWM::setDutyCycle(float cycle) {
AndyA 0:76861123625b 32 if ((cycle >=0) && (cycle <=1)) {
AndyA 0:76861123625b 33 _timeOn = _repeatTime*cycle;
AndyA 0:76861123625b 34 } else
AndyA 0:76861123625b 35 _timeOn = _repeatTime/2; // set to 50% if invalid
AndyA 0:76861123625b 36 }
AndyA 0:76861123625b 37
AndyA 0:76861123625b 38 void slowPWM::stop()
AndyA 0:76861123625b 39 {
AndyA 0:76861123625b 40 cycleTimer.detach();
AndyA 0:76861123625b 41 }
AndyA 0:76861123625b 42
AndyA 0:76861123625b 43 // start things.
AndyA 0:76861123625b 44 void slowPWM::start()
AndyA 0:76861123625b 45 {
AndyA 0:76861123625b 46 cycleTimer.attach(callback(this,&slowPWM::onCycleStart),_repeatTime);
AndyA 0:76861123625b 47 onCycleStart();
AndyA 0:76861123625b 48 }
AndyA 0:76861123625b 49
AndyA 0:76861123625b 50 void slowPWM::onTurnOff(void)
AndyA 0:76861123625b 51 {
AndyA 0:76861123625b 52 write(0);
AndyA 0:76861123625b 53 }
AndyA 0:76861123625b 54
AndyA 0:76861123625b 55 void slowPWM::onCycleStart(void)
AndyA 0:76861123625b 56 {
AndyA 0:76861123625b 57 offTimer.attach(callback(this,&slowPWM::onTurnOff),_timeOn);
AndyA 0:76861123625b 58 write(1);
AndyA 0:76861123625b 59 }