Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SoftPWM by
SoftPWM.cpp@0:7918ce37626c, 2013-10-23 (annotated)
- Committer:
- komaida424
- Date:
- Wed Oct 23 19:31:14 2013 +0000
- Revision:
- 0:7918ce37626c
- Child:
- 1:9aba3dc9cd97
The PWM output by software. Can be output to any pin. It can be used to replace the Pwmout.
;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| komaida424 | 0:7918ce37626c | 1 | #include "mbed.h" |
| komaida424 | 0:7918ce37626c | 2 | #include "InterruptIn.h" |
| komaida424 | 0:7918ce37626c | 3 | #include "SoftPWM.h" |
| komaida424 | 0:7918ce37626c | 4 | |
| komaida424 | 0:7918ce37626c | 5 | SoftPWM::SoftPWM(PinName _outpin,bool _positive) : pulse(_outpin) //constructa |
| komaida424 | 0:7918ce37626c | 6 | { |
| komaida424 | 0:7918ce37626c | 7 | if ( _positive ) |
| komaida424 | 0:7918ce37626c | 8 | pulse = 0; |
| komaida424 | 0:7918ce37626c | 9 | else |
| komaida424 | 0:7918ce37626c | 10 | pulse = 1; |
| komaida424 | 0:7918ce37626c | 11 | positive = _positive; |
| komaida424 | 0:7918ce37626c | 12 | interval = 0.02; |
| komaida424 | 0:7918ce37626c | 13 | width = 0; |
| komaida424 | 0:7918ce37626c | 14 | start(); |
| komaida424 | 0:7918ce37626c | 15 | } |
| komaida424 | 0:7918ce37626c | 16 | |
| komaida424 | 0:7918ce37626c | 17 | float SoftPWM::read() |
| komaida424 | 0:7918ce37626c | 18 | { |
| komaida424 | 0:7918ce37626c | 19 | if ( width <= 0.0 ) return 0.0; |
| komaida424 | 0:7918ce37626c | 20 | if ( width > 1.0 ) return 1.0; |
| komaida424 | 0:7918ce37626c | 21 | return width / interval; |
| komaida424 | 0:7918ce37626c | 22 | } |
| komaida424 | 0:7918ce37626c | 23 | |
| komaida424 | 0:7918ce37626c | 24 | void SoftPWM::write(float duty) |
| komaida424 | 0:7918ce37626c | 25 | { |
| komaida424 | 0:7918ce37626c | 26 | width = interval * duty; |
| komaida424 | 0:7918ce37626c | 27 | if ( duty <= 0.0 ) width = 0.0; |
| komaida424 | 0:7918ce37626c | 28 | if ( duty > 1.0 ) width = interval; |
| komaida424 | 0:7918ce37626c | 29 | } |
| komaida424 | 0:7918ce37626c | 30 | |
| komaida424 | 0:7918ce37626c | 31 | void SoftPWM::start() |
| komaida424 | 0:7918ce37626c | 32 | { |
| komaida424 | 0:7918ce37626c | 33 | _ticker.attach(this,&SoftPWM::TickerInterrapt,interval); |
| komaida424 | 0:7918ce37626c | 34 | } |
| komaida424 | 0:7918ce37626c | 35 | |
| komaida424 | 0:7918ce37626c | 36 | void SoftPWM::stop() |
| komaida424 | 0:7918ce37626c | 37 | { |
| komaida424 | 0:7918ce37626c | 38 | _ticker.detach(); |
| komaida424 | 0:7918ce37626c | 39 | if ( positive ) |
| komaida424 | 0:7918ce37626c | 40 | pulse = 0; |
| komaida424 | 0:7918ce37626c | 41 | else |
| komaida424 | 0:7918ce37626c | 42 | pulse = 1; |
| komaida424 | 0:7918ce37626c | 43 | wait(width); |
| komaida424 | 0:7918ce37626c | 44 | } |
| komaida424 | 0:7918ce37626c | 45 | |
| komaida424 | 0:7918ce37626c | 46 | void SoftPWM::period(float _period) |
| komaida424 | 0:7918ce37626c | 47 | { |
| komaida424 | 0:7918ce37626c | 48 | interval = _period; |
| komaida424 | 0:7918ce37626c | 49 | start(); |
| komaida424 | 0:7918ce37626c | 50 | } |
| komaida424 | 0:7918ce37626c | 51 | |
| komaida424 | 0:7918ce37626c | 52 | void SoftPWM::period_ms(int _period) |
| komaida424 | 0:7918ce37626c | 53 | { |
| komaida424 | 0:7918ce37626c | 54 | period((float)_period / 1000); |
| komaida424 | 0:7918ce37626c | 55 | start(); |
| komaida424 | 0:7918ce37626c | 56 | } |
| komaida424 | 0:7918ce37626c | 57 | |
| komaida424 | 0:7918ce37626c | 58 | void SoftPWM::period_us(int _period) |
| komaida424 | 0:7918ce37626c | 59 | { |
| komaida424 | 0:7918ce37626c | 60 | period((float)_period / 1000000); |
| komaida424 | 0:7918ce37626c | 61 | start(); |
| komaida424 | 0:7918ce37626c | 62 | } |
| komaida424 | 0:7918ce37626c | 63 | |
| komaida424 | 0:7918ce37626c | 64 | void SoftPWM::pulsewidth(float _width) |
| komaida424 | 0:7918ce37626c | 65 | { |
| komaida424 | 0:7918ce37626c | 66 | width = _width; |
| komaida424 | 0:7918ce37626c | 67 | if ( width < 0.0 ) width = 0.0; |
| komaida424 | 0:7918ce37626c | 68 | } |
| komaida424 | 0:7918ce37626c | 69 | |
| komaida424 | 0:7918ce37626c | 70 | void SoftPWM::pulsewidth_ms(int _width) |
| komaida424 | 0:7918ce37626c | 71 | { |
| komaida424 | 0:7918ce37626c | 72 | pulsewidth((float)_width / 1000); |
| komaida424 | 0:7918ce37626c | 73 | } |
| komaida424 | 0:7918ce37626c | 74 | |
| komaida424 | 0:7918ce37626c | 75 | void SoftPWM::pulsewidth_us(int _width) |
| komaida424 | 0:7918ce37626c | 76 | { |
| komaida424 | 0:7918ce37626c | 77 | pulsewidth((float)_width / 1000000); |
| komaida424 | 0:7918ce37626c | 78 | } |
| komaida424 | 0:7918ce37626c | 79 | |
| komaida424 | 0:7918ce37626c | 80 | void SoftPWM::TickerInterrapt() |
| komaida424 | 0:7918ce37626c | 81 | { |
| komaida424 | 0:7918ce37626c | 82 | if ( width <= 0 ) return; |
| komaida424 | 0:7918ce37626c | 83 | _timeout.attach(this,&SoftPWM::end,width); |
| komaida424 | 0:7918ce37626c | 84 | if ( positive ) |
| komaida424 | 0:7918ce37626c | 85 | pulse = 1; |
| komaida424 | 0:7918ce37626c | 86 | else |
| komaida424 | 0:7918ce37626c | 87 | pulse = 0; |
| komaida424 | 0:7918ce37626c | 88 | } |
| komaida424 | 0:7918ce37626c | 89 | |
| komaida424 | 0:7918ce37626c | 90 | void SoftPWM::end() |
| komaida424 | 0:7918ce37626c | 91 | { |
| komaida424 | 0:7918ce37626c | 92 | if ( positive ) |
| komaida424 | 0:7918ce37626c | 93 | pulse = 0; |
| komaida424 | 0:7918ce37626c | 94 | else |
| komaida424 | 0:7918ce37626c | 95 | pulse = 1; |
| komaida424 | 0:7918ce37626c | 96 | // _timeout.detach(); |
| komaida424 | 0:7918ce37626c | 97 | } |
| komaida424 | 0:7918ce37626c | 98 | ; |
| komaida424 | 0:7918ce37626c | 99 |
