Sagawa Electronics,inc.

Dependencies:   mbed

Fork of sagawa_lpc1114 by kazushiro tanimoto

Committer:
zero515
Date:
Sat Nov 26 19:25:42 2016 +0000
Revision:
2:de000dc1b277
Parent:
0:d90f3421aaef
Sagawa Electronics,inc.

Who changed what in which revision?

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