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.
SoftPWM.cpp
00001 #include "mbed.h" 00002 #include "InterruptIn.h" 00003 #include "SoftPWM.h" 00004 void wait(float); 00005 00006 SoftPWM::SoftPWM(PinName _outpin,bool _positive) : pulse(_outpin) //constructa 00007 { 00008 if ( _positive ) 00009 pulse = 0; 00010 else 00011 pulse = 1; 00012 positive = _positive; 00013 interval = 0.02; 00014 width = 0; 00015 start(); 00016 } 00017 00018 float SoftPWM::read() 00019 { 00020 if ( width <= 0.0f ) return 0.0; 00021 if ( width > 1.0f ) return 1.0; 00022 return width / interval; 00023 } 00024 00025 void SoftPWM::write(float duty) 00026 { 00027 width = interval * duty; 00028 if ( duty <= 0.0f ) width = 0.0; 00029 if ( duty > 1.0f ) width = interval; 00030 } 00031 00032 void SoftPWM::start() 00033 { 00034 uint32_t itv = interval; 00035 _ticker.attach(callback(this,&SoftPWM::TickerInterrapt),itv); 00036 } 00037 00038 void SoftPWM::stop() 00039 { 00040 _ticker.detach(); 00041 if ( positive ) 00042 pulse = 0; 00043 else 00044 pulse = 1; 00045 wait(width); 00046 } 00047 00048 void SoftPWM::period(float _period) 00049 { 00050 interval = _period; 00051 start(); 00052 } 00053 00054 void SoftPWM::period_ms(int _period) 00055 { 00056 period((float)_period / 1000); 00057 start(); 00058 } 00059 00060 void SoftPWM::period_us(int _period) 00061 { 00062 period((float)_period / 1000000); 00063 start(); 00064 } 00065 00066 void SoftPWM::pulsewidth(float _width) 00067 { 00068 width = _width; 00069 if ( width < 0.0f ) width = 0.0; 00070 } 00071 00072 void SoftPWM::pulsewidth_ms(int _width) 00073 { 00074 pulsewidth((float)_width / 1000); 00075 } 00076 00077 void SoftPWM::pulsewidth_us(int _width) 00078 { 00079 pulsewidth((float)_width / 1000000); 00080 } 00081 00082 void SoftPWM::TickerInterrapt() 00083 { 00084 if ( width <= 0 ) return; 00085 _timeout.attach(callback(this,&SoftPWM::end),width); 00086 if ( positive ) 00087 pulse = 1; 00088 else 00089 pulse = 0; 00090 } 00091 00092 void SoftPWM::end() 00093 { 00094 if ( positive ) 00095 pulse = 0; 00096 else 00097 pulse = 1; 00098 // _timeout.detach(); 00099 } 00100 ; 00101
Generated on Wed Jul 13 2022 09:20:34 by
1.7.2