Dependents: YMotor remoteContolledRobot Pokittris Pokittris ... more
Revision 0:f1e5739acc27, committed 2015-08-23
- Comitter:
- inst
- Date:
- Sun Aug 23 15:17:49 2015 +0000
- Commit message:
- first
Changed in this revision
PWMOut.cpp | Show annotated file Show diff for this revision Revisions of this file |
PWMOut.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r f1e5739acc27 PWMOut.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PWMOut.cpp Sun Aug 23 15:17:49 2015 +0000 @@ -0,0 +1,27 @@ +#include "mbed.h" +#include "PWMOut.h" + +PWMOut::PWMOut( PinName p ){ + mPWM = new PwmOut( p ); + mTimer = new Timer; + mTimer->start(); + mPeriod_us = 20 * 1000; + mNextDuty = 0.0f; +} + +void PWMOut::setDuty( float duty ){ + mNextDuty = duty; + + if ( mTimer->read_us() > mPeriod_us ){ + mPWM->write( mNextDuty ); + mTimer->reset(); + } +} + +void PWMOut::setPeriod_sec( float period ){ + setPeriod_us( static_cast< uint32_t >( period * 1000.0f * 1000.0f ) ); +} + +void PWMOut::setPeriod_us( uint32_t period ){ + mPWM->period_us( period ); +}
diff -r 000000000000 -r f1e5739acc27 PWMOut.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PWMOut.h Sun Aug 23 15:17:49 2015 +0000 @@ -0,0 +1,20 @@ +#ifndef INCLUDED_PWM_OUT_H +#define INCLUDED_PWM_OUT_H + +#include "mbed.h" + +class PWMOut{ +public: + PWMOut( PinName p ); + void setDuty( float duty ); + void setPeriod_sec( float period ); + void setPeriod_us( uint32_t period ); + +private: + PwmOut* mPWM; + uint32_t mPeriod_us; + Timer* mTimer; + float mNextDuty; +}; + +#endif