Dependents: YMotor remoteContolledRobot Pokittris Pokittris ... more
PWMOut.cpp@0:f1e5739acc27, 2015-08-23 (annotated)
- Committer:
- inst
- Date:
- Sun Aug 23 15:17:49 2015 +0000
- Revision:
- 0:f1e5739acc27
first
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
inst | 0:f1e5739acc27 | 1 | #include "mbed.h" |
inst | 0:f1e5739acc27 | 2 | #include "PWMOut.h" |
inst | 0:f1e5739acc27 | 3 | |
inst | 0:f1e5739acc27 | 4 | PWMOut::PWMOut( PinName p ){ |
inst | 0:f1e5739acc27 | 5 | mPWM = new PwmOut( p ); |
inst | 0:f1e5739acc27 | 6 | mTimer = new Timer; |
inst | 0:f1e5739acc27 | 7 | mTimer->start(); |
inst | 0:f1e5739acc27 | 8 | mPeriod_us = 20 * 1000; |
inst | 0:f1e5739acc27 | 9 | mNextDuty = 0.0f; |
inst | 0:f1e5739acc27 | 10 | } |
inst | 0:f1e5739acc27 | 11 | |
inst | 0:f1e5739acc27 | 12 | void PWMOut::setDuty( float duty ){ |
inst | 0:f1e5739acc27 | 13 | mNextDuty = duty; |
inst | 0:f1e5739acc27 | 14 | |
inst | 0:f1e5739acc27 | 15 | if ( mTimer->read_us() > mPeriod_us ){ |
inst | 0:f1e5739acc27 | 16 | mPWM->write( mNextDuty ); |
inst | 0:f1e5739acc27 | 17 | mTimer->reset(); |
inst | 0:f1e5739acc27 | 18 | } |
inst | 0:f1e5739acc27 | 19 | } |
inst | 0:f1e5739acc27 | 20 | |
inst | 0:f1e5739acc27 | 21 | void PWMOut::setPeriod_sec( float period ){ |
inst | 0:f1e5739acc27 | 22 | setPeriod_us( static_cast< uint32_t >( period * 1000.0f * 1000.0f ) ); |
inst | 0:f1e5739acc27 | 23 | } |
inst | 0:f1e5739acc27 | 24 | |
inst | 0:f1e5739acc27 | 25 | void PWMOut::setPeriod_us( uint32_t period ){ |
inst | 0:f1e5739acc27 | 26 | mPWM->period_us( period ); |
inst | 0:f1e5739acc27 | 27 | } |