The PWM output by software. Can be output to any pin. It can be used to replace the Pwmout.
Fork of SoftPWM by
SoftPWM.h@1:9aba3dc9cd97, 2018-06-05 (annotated)
- Committer:
- haarkon
- Date:
- Tue Jun 05 12:21:14 2018 +0000
- Revision:
- 1:9aba3dc9cd97
- Parent:
- 0:7918ce37626c
Lightly modified to correct multiple dumb warning
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
komaida424 | 0:7918ce37626c | 1 | #ifndef SoftPWM_H |
komaida424 | 0:7918ce37626c | 2 | #define SoftPWM_H |
komaida424 | 0:7918ce37626c | 3 | #define POSITIVE true |
komaida424 | 0:7918ce37626c | 4 | #define NEGATIVE false |
komaida424 | 0:7918ce37626c | 5 | |
komaida424 | 0:7918ce37626c | 6 | #include "mbed.h" |
komaida424 | 0:7918ce37626c | 7 | |
komaida424 | 0:7918ce37626c | 8 | class SoftPWM |
komaida424 | 0:7918ce37626c | 9 | { |
komaida424 | 0:7918ce37626c | 10 | private: |
komaida424 | 0:7918ce37626c | 11 | Timeout _timeout; |
komaida424 | 0:7918ce37626c | 12 | Ticker _ticker; |
komaida424 | 0:7918ce37626c | 13 | void end(); |
komaida424 | 0:7918ce37626c | 14 | DigitalOut pulse; |
komaida424 | 0:7918ce37626c | 15 | bool positive; |
komaida424 | 0:7918ce37626c | 16 | void TickerInterrapt(); |
haarkon | 1:9aba3dc9cd97 | 17 | double width; |
haarkon | 1:9aba3dc9cd97 | 18 | double interval; |
komaida424 | 0:7918ce37626c | 19 | public: |
komaida424 | 0:7918ce37626c | 20 | SoftPWM(PinName,bool mode=true); |
komaida424 | 0:7918ce37626c | 21 | // void attach_us(int); |
komaida424 | 0:7918ce37626c | 22 | void start(); |
haarkon | 1:9aba3dc9cd97 | 23 | void write(double); |
haarkon | 1:9aba3dc9cd97 | 24 | double read(); |
haarkon | 1:9aba3dc9cd97 | 25 | void pulsewidth(double); |
komaida424 | 0:7918ce37626c | 26 | void pulsewidth_ms(int); |
komaida424 | 0:7918ce37626c | 27 | void pulsewidth_us(int); |
haarkon | 1:9aba3dc9cd97 | 28 | void period(double); |
komaida424 | 0:7918ce37626c | 29 | void period_ms(int); |
komaida424 | 0:7918ce37626c | 30 | void period_us(int); |
komaida424 | 0:7918ce37626c | 31 | void stop(); |
haarkon | 1:9aba3dc9cd97 | 32 | operator double() { |
komaida424 | 0:7918ce37626c | 33 | if ( width <= 0.0 ) return 0.0; |
komaida424 | 0:7918ce37626c | 34 | if ( width > 1.0 ) return 1.0; |
komaida424 | 0:7918ce37626c | 35 | return width / interval; |
komaida424 | 0:7918ce37626c | 36 | } |
haarkon | 1:9aba3dc9cd97 | 37 | SoftPWM& operator=(double duty) { |
komaida424 | 0:7918ce37626c | 38 | width = interval * duty; |
haarkon | 1:9aba3dc9cd97 | 39 | if ( duty <= 0.0 ) width = 0.0f; |
komaida424 | 0:7918ce37626c | 40 | if ( duty > 1.0 ) width = interval; |
komaida424 | 0:7918ce37626c | 41 | return *this; |
komaida424 | 0:7918ce37626c | 42 | } |
komaida424 | 0:7918ce37626c | 43 | |
komaida424 | 0:7918ce37626c | 44 | }; |
komaida424 | 0:7918ce37626c | 45 | #endif |