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); } }
SlowPWM.h@3:3f7eb3ad23d4, 2019-06-21 (annotated)
- Committer:
- AndyA
- Date:
- Fri Jun 21 09:45:08 2019 +0000
- Revision:
- 3:3f7eb3ad23d4
- Parent:
- 2:c90e2d2f52aa
Added auto start stop to setHighTime;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndyA | 1:386d04fe1e37 | 1 | #ifndef __SlowPWM_H__ |
AndyA | 1:386d04fe1e37 | 2 | #define __SlowPWM_H__ |
AndyA | 0:76861123625b | 3 | |
AndyA | 0:76861123625b | 4 | #include "mbed.h" |
AndyA | 2:c90e2d2f52aa | 5 | |
AndyA | 2:c90e2d2f52aa | 6 | |
AndyA | 2:c90e2d2f52aa | 7 | /// A basic class to create very slow PWM signals using Digital IO and timers. |
AndyA | 2:c90e2d2f52aa | 8 | /// If setting a period and high time then you must manually call start. |
AndyA | 2:c90e2d2f52aa | 9 | /// If setting a duty cycle then it will start automatically, make sure you've set the peiod first if you care about it. |
AndyA | 1:386d04fe1e37 | 10 | class SlowPWM : public DigitalOut |
AndyA | 0:76861123625b | 11 | { |
AndyA | 0:76861123625b | 12 | public: |
AndyA | 0:76861123625b | 13 | |
AndyA | 0:76861123625b | 14 | // constructor without setting anything going. |
AndyA | 1:386d04fe1e37 | 15 | SlowPWM( const PinName pin); |
AndyA | 0:76861123625b | 16 | |
AndyA | 0:76861123625b | 17 | // constructor that also starts things running |
AndyA | 2:c90e2d2f52aa | 18 | SlowPWM( const PinName pin, const float period, const float highTime); |
AndyA | 0:76861123625b | 19 | |
AndyA | 3:3f7eb3ad23d4 | 20 | /// set the period |
AndyA | 2:c90e2d2f52aa | 21 | void setPeriod(const float period); |
AndyA | 0:76861123625b | 22 | |
AndyA | 3:3f7eb3ad23d4 | 23 | /// set the on time per cycle. If 0 or the period or longer timers are stopped and output set perminently |
AndyA | 2:c90e2d2f52aa | 24 | void setHighTime(const float highTime); |
AndyA | 0:76861123625b | 25 | |
AndyA | 3:3f7eb3ad23d4 | 26 | // set the on time per cycle as a fraction of the period. |
AndyA | 2:c90e2d2f52aa | 27 | void setDutyCycle(const float cycle); |
AndyA | 0:76861123625b | 28 | |
AndyA | 2:c90e2d2f52aa | 29 | // stop things. |
AndyA | 0:76861123625b | 30 | void stop(); |
AndyA | 0:76861123625b | 31 | |
AndyA | 0:76861123625b | 32 | // start things |
AndyA | 0:76861123625b | 33 | void start(); |
AndyA | 0:76861123625b | 34 | |
AndyA | 0:76861123625b | 35 | private: |
AndyA | 0:76861123625b | 36 | // internal functions and variables not visible to the outside world |
AndyA | 0:76861123625b | 37 | |
AndyA | 0:76861123625b | 38 | void onTurnOff(void); |
AndyA | 0:76861123625b | 39 | void onCycleStart(void); |
AndyA | 0:76861123625b | 40 | |
AndyA | 0:76861123625b | 41 | Ticker cycleTimer; |
AndyA | 0:76861123625b | 42 | Timeout offTimer; |
AndyA | 0:76861123625b | 43 | |
AndyA | 0:76861123625b | 44 | float _timeOn; |
AndyA | 0:76861123625b | 45 | float _repeatTime; |
AndyA | 0:76861123625b | 46 | }; |
AndyA | 0:76861123625b | 47 | #endif |