This Library allows you to use any pin on the mbed micro as PWM out. It also allows the user to update the Period and Pulse width in realtime.
PwmOutAny.cpp@0:9eb94736e774, 2013-12-08 (annotated)
- Committer:
- mr63
- Date:
- Sun Dec 08 19:04:44 2013 +0000
- Revision:
- 0:9eb94736e774
This Library allows you to use any pin on the mbed micro as PWM out. It also allows the user to update the Period and Pulse width in realtime.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mr63 | 0:9eb94736e774 | 1 | #include "PwmOutAny.h" |
mr63 | 0:9eb94736e774 | 2 | |
mr63 | 0:9eb94736e774 | 3 | float _period; |
mr63 | 0:9eb94736e774 | 4 | float _PercentHigh; |
mr63 | 0:9eb94736e774 | 5 | |
mr63 | 0:9eb94736e774 | 6 | PwmOutAny::PwmOutAny(PinName pin1, float Period, float PercentHigh):_PwmOut(pin1) |
mr63 | 0:9eb94736e774 | 7 | { |
mr63 | 0:9eb94736e774 | 8 | _period=Period; |
mr63 | 0:9eb94736e774 | 9 | _PercentHigh=PercentHigh ; |
mr63 | 0:9eb94736e774 | 10 | _PwmTicker.attach(this,&PwmOutAny::TickerFlipper,_period/2); |
mr63 | 0:9eb94736e774 | 11 | } |
mr63 | 0:9eb94736e774 | 12 | |
mr63 | 0:9eb94736e774 | 13 | void PwmOutAny::TickerFlipper() |
mr63 | 0:9eb94736e774 | 14 | { |
mr63 | 0:9eb94736e774 | 15 | static float CurrentPeriod; |
mr63 | 0:9eb94736e774 | 16 | if(!_PwmOut) |
mr63 | 0:9eb94736e774 | 17 | { |
mr63 | 0:9eb94736e774 | 18 | _PwmTicker.detach(); |
mr63 | 0:9eb94736e774 | 19 | _PwmTicker.attach(this,&PwmOutAny::TickerFlipper,CurrentPeriod*(_PercentHigh)); |
mr63 | 0:9eb94736e774 | 20 | _PwmOut = true; |
mr63 | 0:9eb94736e774 | 21 | } |
mr63 | 0:9eb94736e774 | 22 | else |
mr63 | 0:9eb94736e774 | 23 | { |
mr63 | 0:9eb94736e774 | 24 | CurrentPeriod = _period; |
mr63 | 0:9eb94736e774 | 25 | _PwmTicker.detach(); |
mr63 | 0:9eb94736e774 | 26 | _PwmTicker.attach(this,&PwmOutAny::TickerFlipper,CurrentPeriod*(1.0-_PercentHigh)); |
mr63 | 0:9eb94736e774 | 27 | _PwmOut = false; |
mr63 | 0:9eb94736e774 | 28 | } |
mr63 | 0:9eb94736e774 | 29 | } |
mr63 | 0:9eb94736e774 | 30 | |
mr63 | 0:9eb94736e774 | 31 | |
mr63 | 0:9eb94736e774 | 32 | float PwmOutAny::Get_PulseWidth() |
mr63 | 0:9eb94736e774 | 33 | { |
mr63 | 0:9eb94736e774 | 34 | return (_PercentHigh); |
mr63 | 0:9eb94736e774 | 35 | |
mr63 | 0:9eb94736e774 | 36 | } |
mr63 | 0:9eb94736e774 | 37 | void PwmOutAny::Set_PulseWidth(float PW) |
mr63 | 0:9eb94736e774 | 38 | { |
mr63 | 0:9eb94736e774 | 39 | _PercentHigh = PW; |
mr63 | 0:9eb94736e774 | 40 | } |
mr63 | 0:9eb94736e774 | 41 | float PwmOutAny::Get_Period() |
mr63 | 0:9eb94736e774 | 42 | { |
mr63 | 0:9eb94736e774 | 43 | return _period; |
mr63 | 0:9eb94736e774 | 44 | } |
mr63 | 0:9eb94736e774 | 45 | void PwmOutAny::Set_Period(float ChangePeriod) |
mr63 | 0:9eb94736e774 | 46 | { |
mr63 | 0:9eb94736e774 | 47 | _period = ChangePeriod; |
mr63 | 0:9eb94736e774 | 48 | } |
mr63 | 0:9eb94736e774 | 49 |