Output PWM on any pin. Partly based on SoftPWM with some improvements

Committer:
gagarcr
Date:
Thu Sep 03 08:15:34 2015 +0000
Revision:
0:813ee8141cdd
Intial version based on SoftPWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gagarcr 0:813ee8141cdd 1 #include "mbed.h"
gagarcr 0:813ee8141cdd 2 #include "InterruptIn.h"
gagarcr 0:813ee8141cdd 3 #include "SoftPwmOut.h"
gagarcr 0:813ee8141cdd 4
gagarcr 0:813ee8141cdd 5 SoftPwmOut::SoftPwmOut(PinName pin) : _outPin(pin) // Constructor definition. Assign _out
gagarcr 0:813ee8141cdd 6 {
gagarcr 0:813ee8141cdd 7 _interval = 0.1; // Default fequency
gagarcr 0:813ee8141cdd 8 _width = 0;
gagarcr 0:813ee8141cdd 9 start();
gagarcr 0:813ee8141cdd 10 }
gagarcr 0:813ee8141cdd 11
gagarcr 0:813ee8141cdd 12 float SoftPwmOut::read()
gagarcr 0:813ee8141cdd 13 {
gagarcr 0:813ee8141cdd 14 return _width / _interval;
gagarcr 0:813ee8141cdd 15 }
gagarcr 0:813ee8141cdd 16
gagarcr 0:813ee8141cdd 17 void SoftPwmOut::write(float duty)
gagarcr 0:813ee8141cdd 18 {
gagarcr 0:813ee8141cdd 19 _width = _interval * duty;
gagarcr 0:813ee8141cdd 20 if ( duty <= 0.0 ) _width = 0.0;
gagarcr 0:813ee8141cdd 21 if ( duty > 1.0 ) _width = _interval;
gagarcr 0:813ee8141cdd 22 }
gagarcr 0:813ee8141cdd 23
gagarcr 0:813ee8141cdd 24 void SoftPwmOut::start()
gagarcr 0:813ee8141cdd 25 {
gagarcr 0:813ee8141cdd 26 _ticker.attach(this,&SoftPwmOut::TickerInterrupt,_interval);
gagarcr 0:813ee8141cdd 27 }
gagarcr 0:813ee8141cdd 28
gagarcr 0:813ee8141cdd 29 void SoftPwmOut::stop()
gagarcr 0:813ee8141cdd 30 {
gagarcr 0:813ee8141cdd 31 _ticker.detach();
gagarcr 0:813ee8141cdd 32 //wait(width);
gagarcr 0:813ee8141cdd 33 }
gagarcr 0:813ee8141cdd 34
gagarcr 0:813ee8141cdd 35 void SoftPwmOut::period(float period)
gagarcr 0:813ee8141cdd 36 {
gagarcr 0:813ee8141cdd 37 _interval = period;
gagarcr 0:813ee8141cdd 38 start();
gagarcr 0:813ee8141cdd 39 }
gagarcr 0:813ee8141cdd 40
gagarcr 0:813ee8141cdd 41 void SoftPwmOut::period_ms(int period)
gagarcr 0:813ee8141cdd 42 {
gagarcr 0:813ee8141cdd 43 SoftPwmOut::period((float)period / 1000);
gagarcr 0:813ee8141cdd 44 start();
gagarcr 0:813ee8141cdd 45 }
gagarcr 0:813ee8141cdd 46
gagarcr 0:813ee8141cdd 47 void SoftPwmOut::period_us(int period)
gagarcr 0:813ee8141cdd 48 {
gagarcr 0:813ee8141cdd 49 SoftPwmOut::period((float)period / 1000000);
gagarcr 0:813ee8141cdd 50 start();
gagarcr 0:813ee8141cdd 51 }
gagarcr 0:813ee8141cdd 52
gagarcr 0:813ee8141cdd 53 void SoftPwmOut::pulsewidth(float width)
gagarcr 0:813ee8141cdd 54 {
gagarcr 0:813ee8141cdd 55 _width = width;
gagarcr 0:813ee8141cdd 56 if ( _width < 0.0 ) _width = 0.0;
gagarcr 0:813ee8141cdd 57 if ( _width > _interval) _width = _interval;
gagarcr 0:813ee8141cdd 58 }
gagarcr 0:813ee8141cdd 59
gagarcr 0:813ee8141cdd 60 void SoftPwmOut::pulsewidth_ms(int width)
gagarcr 0:813ee8141cdd 61 {
gagarcr 0:813ee8141cdd 62 SoftPwmOut::pulsewidth((float)width / 1000);
gagarcr 0:813ee8141cdd 63 }
gagarcr 0:813ee8141cdd 64
gagarcr 0:813ee8141cdd 65 void SoftPwmOut::pulsewidth_us(int width)
gagarcr 0:813ee8141cdd 66 {
gagarcr 0:813ee8141cdd 67 SoftPwmOut::pulsewidth((float)width / 1000000);
gagarcr 0:813ee8141cdd 68 }
gagarcr 0:813ee8141cdd 69
gagarcr 0:813ee8141cdd 70 void SoftPwmOut::TickerInterrupt()
gagarcr 0:813ee8141cdd 71 {
gagarcr 0:813ee8141cdd 72 _outPin = 0;
gagarcr 0:813ee8141cdd 73 if ( _width <= 0 ) return;
gagarcr 0:813ee8141cdd 74 _timeout.attach(this,&SoftPwmOut::end,_width);
gagarcr 0:813ee8141cdd 75 }
gagarcr 0:813ee8141cdd 76
gagarcr 0:813ee8141cdd 77 void SoftPwmOut::end()
gagarcr 0:813ee8141cdd 78 {
gagarcr 0:813ee8141cdd 79 _outPin = 1;
gagarcr 0:813ee8141cdd 80 // _timeout.detach();
gagarcr 0:813ee8141cdd 81 }
gagarcr 0:813ee8141cdd 82 ;
gagarcr 0:813ee8141cdd 83