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 #ifndef __slowPWM.h__
AndyA 0:76861123625b 2 #define __slowPWM.h__
AndyA 0:76861123625b 3
AndyA 0:76861123625b 4 #include "mbed.h"
AndyA 0:76861123625b 5 class slowPWM : public DigitalOut
AndyA 0:76861123625b 6 {
AndyA 0:76861123625b 7 public:
AndyA 0:76861123625b 8
AndyA 0:76861123625b 9 // constructor without setting anything going.
AndyA 0:76861123625b 10 slowPWM( const PinName pin);
AndyA 0:76861123625b 11
AndyA 0:76861123625b 12 // constructor that also starts things running
AndyA 0:76861123625b 13 slowPWM( float period, float highTime, const PinName pin );
AndyA 0:76861123625b 14
AndyA 0:76861123625b 15 // set the period
AndyA 0:76861123625b 16 void setPeriod(float period);
AndyA 0:76861123625b 17
AndyA 0:76861123625b 18 // set the on time per cycle
AndyA 0:76861123625b 19 void setHighTime(float highTime);
AndyA 0:76861123625b 20
AndyA 0:76861123625b 21 // set the on time per cycle as a fraction
AndyA 0:76861123625b 22 void setDutyCycle(float cycle);
AndyA 0:76861123625b 23
AndyA 0:76861123625b 24
AndyA 0:76861123625b 25 // stop things. If output is high it will still turn low at the correct time
AndyA 0:76861123625b 26 void stop();
AndyA 0:76861123625b 27
AndyA 0:76861123625b 28 // start things
AndyA 0:76861123625b 29 void start();
AndyA 0:76861123625b 30
AndyA 0:76861123625b 31 private:
AndyA 0:76861123625b 32 // internal functions and variables not visible to the outside world
AndyA 0:76861123625b 33
AndyA 0:76861123625b 34 void onTurnOff(void);
AndyA 0:76861123625b 35 void onCycleStart(void);
AndyA 0:76861123625b 36
AndyA 0:76861123625b 37 Ticker cycleTimer;
AndyA 0:76861123625b 38 Timeout offTimer;
AndyA 0:76861123625b 39
AndyA 0:76861123625b 40 float _timeOn;
AndyA 0:76861123625b 41 float _repeatTime;
AndyA 0:76861123625b 42 };
AndyA 0:76861123625b 43 #endif