softPWM

Dependents:   2021_NHK_A

Committer:
THtakahiro702286
Date:
Wed Jan 29 09:20:03 2020 +0000
Revision:
0:9a7ca977803b
Child:
1:cb8028e779a1
Child:
2:594922580cdc
softPWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
THtakahiro702286 0:9a7ca977803b 1 #include "mbed.h"
THtakahiro702286 0:9a7ca977803b 2 #include "InterruptIn.h"
THtakahiro702286 0:9a7ca977803b 3 #include "SoftPWM.h"
THtakahiro702286 0:9a7ca977803b 4
THtakahiro702286 0:9a7ca977803b 5 SoftPWM::SoftPWM(PinName _outpin,bool _positive) : pulse(_outpin) //constructa
THtakahiro702286 0:9a7ca977803b 6 {
THtakahiro702286 0:9a7ca977803b 7 if ( _positive )
THtakahiro702286 0:9a7ca977803b 8 pulse = 0;
THtakahiro702286 0:9a7ca977803b 9 else
THtakahiro702286 0:9a7ca977803b 10 pulse = 1;
THtakahiro702286 0:9a7ca977803b 11 positive = _positive;
THtakahiro702286 0:9a7ca977803b 12 interval = 0.02;
THtakahiro702286 0:9a7ca977803b 13 width = 0;
THtakahiro702286 0:9a7ca977803b 14 start();
THtakahiro702286 0:9a7ca977803b 15 }
THtakahiro702286 0:9a7ca977803b 16
THtakahiro702286 0:9a7ca977803b 17 float SoftPWM::read()
THtakahiro702286 0:9a7ca977803b 18 {
THtakahiro702286 0:9a7ca977803b 19 if ( width <= 0.0 ) return 0.0;
THtakahiro702286 0:9a7ca977803b 20 if ( width > 1.0 ) return 1.0;
THtakahiro702286 0:9a7ca977803b 21 return width / interval;
THtakahiro702286 0:9a7ca977803b 22 }
THtakahiro702286 0:9a7ca977803b 23
THtakahiro702286 0:9a7ca977803b 24 void SoftPWM::write(float duty)
THtakahiro702286 0:9a7ca977803b 25 {
THtakahiro702286 0:9a7ca977803b 26 width = interval * duty;
THtakahiro702286 0:9a7ca977803b 27 if ( duty <= 0.0 ) width = 0.0;
THtakahiro702286 0:9a7ca977803b 28 if ( duty > 1.0 ) width = interval;
THtakahiro702286 0:9a7ca977803b 29 }
THtakahiro702286 0:9a7ca977803b 30
THtakahiro702286 0:9a7ca977803b 31 void SoftPWM::start()
THtakahiro702286 0:9a7ca977803b 32 {
THtakahiro702286 0:9a7ca977803b 33 _ticker.attach(this,&SoftPWM::TickerInterrapt,interval);
THtakahiro702286 0:9a7ca977803b 34 }
THtakahiro702286 0:9a7ca977803b 35
THtakahiro702286 0:9a7ca977803b 36 void SoftPWM::stop()
THtakahiro702286 0:9a7ca977803b 37 {
THtakahiro702286 0:9a7ca977803b 38 _ticker.detach();
THtakahiro702286 0:9a7ca977803b 39 if ( positive )
THtakahiro702286 0:9a7ca977803b 40 pulse = 0;
THtakahiro702286 0:9a7ca977803b 41 else
THtakahiro702286 0:9a7ca977803b 42 pulse = 1;
THtakahiro702286 0:9a7ca977803b 43 wait(width);
THtakahiro702286 0:9a7ca977803b 44 }
THtakahiro702286 0:9a7ca977803b 45
THtakahiro702286 0:9a7ca977803b 46 void SoftPWM::period(float _period)
THtakahiro702286 0:9a7ca977803b 47 {
THtakahiro702286 0:9a7ca977803b 48 interval = _period;
THtakahiro702286 0:9a7ca977803b 49 start();
THtakahiro702286 0:9a7ca977803b 50 }
THtakahiro702286 0:9a7ca977803b 51
THtakahiro702286 0:9a7ca977803b 52 void SoftPWM::period_ms(int _period)
THtakahiro702286 0:9a7ca977803b 53 {
THtakahiro702286 0:9a7ca977803b 54 period((float)_period / 1000);
THtakahiro702286 0:9a7ca977803b 55 start();
THtakahiro702286 0:9a7ca977803b 56 }
THtakahiro702286 0:9a7ca977803b 57
THtakahiro702286 0:9a7ca977803b 58 void SoftPWM::period_us(int _period)
THtakahiro702286 0:9a7ca977803b 59 {
THtakahiro702286 0:9a7ca977803b 60 period((float)_period / 1000000);
THtakahiro702286 0:9a7ca977803b 61 start();
THtakahiro702286 0:9a7ca977803b 62 }
THtakahiro702286 0:9a7ca977803b 63
THtakahiro702286 0:9a7ca977803b 64 void SoftPWM::pulsewidth(float _width)
THtakahiro702286 0:9a7ca977803b 65 {
THtakahiro702286 0:9a7ca977803b 66 width = _width;
THtakahiro702286 0:9a7ca977803b 67 if ( width < 0.0 ) width = 0.0;
THtakahiro702286 0:9a7ca977803b 68 }
THtakahiro702286 0:9a7ca977803b 69
THtakahiro702286 0:9a7ca977803b 70 void SoftPWM::pulsewidth_ms(int _width)
THtakahiro702286 0:9a7ca977803b 71 {
THtakahiro702286 0:9a7ca977803b 72 pulsewidth((float)_width / 1000);
THtakahiro702286 0:9a7ca977803b 73 }
THtakahiro702286 0:9a7ca977803b 74
THtakahiro702286 0:9a7ca977803b 75 void SoftPWM::pulsewidth_us(int _width)
THtakahiro702286 0:9a7ca977803b 76 {
THtakahiro702286 0:9a7ca977803b 77 pulsewidth((float)_width / 1000000);
THtakahiro702286 0:9a7ca977803b 78 }
THtakahiro702286 0:9a7ca977803b 79
THtakahiro702286 0:9a7ca977803b 80 void SoftPWM::TickerInterrapt()
THtakahiro702286 0:9a7ca977803b 81 {
THtakahiro702286 0:9a7ca977803b 82 if ( width <= 0 ) return;
THtakahiro702286 0:9a7ca977803b 83 _timeout.attach(this,&SoftPWM::end,width);
THtakahiro702286 0:9a7ca977803b 84 if ( positive )
THtakahiro702286 0:9a7ca977803b 85 pulse = 1;
THtakahiro702286 0:9a7ca977803b 86 else
THtakahiro702286 0:9a7ca977803b 87 pulse = 0;
THtakahiro702286 0:9a7ca977803b 88 }
THtakahiro702286 0:9a7ca977803b 89
THtakahiro702286 0:9a7ca977803b 90 void SoftPWM::end()
THtakahiro702286 0:9a7ca977803b 91 {
THtakahiro702286 0:9a7ca977803b 92 if ( positive )
THtakahiro702286 0:9a7ca977803b 93 pulse = 0;
THtakahiro702286 0:9a7ca977803b 94 else
THtakahiro702286 0:9a7ca977803b 95 pulse = 1;
THtakahiro702286 0:9a7ca977803b 96 // _timeout.detach();
THtakahiro702286 0:9a7ca977803b 97 }
THtakahiro702286 0:9a7ca977803b 98 ;
THtakahiro702286 0:9a7ca977803b 99