An interface to read a PWM input signal, using InterruptIn

Dependents:   PwmIn_HelloWorld PwmIn_ModSerial CHR6dm_reading tugboat ... more

Committer:
simon
Date:
Thu Sep 02 18:17:35 2010 +0000
Revision:
1:6d68eb9b6bbb
Convert previous example in to a library, including doxygen docs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:6d68eb9b6bbb 1 /* mbed PwmIn Library
simon 1:6d68eb9b6bbb 2 * Copyright (c) 2008-2010, sford
simon 1:6d68eb9b6bbb 3 *
simon 1:6d68eb9b6bbb 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:6d68eb9b6bbb 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:6d68eb9b6bbb 6 * in the Software without restriction, including without limitation the rights
simon 1:6d68eb9b6bbb 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:6d68eb9b6bbb 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:6d68eb9b6bbb 9 * furnished to do so, subject to the following conditions:
simon 1:6d68eb9b6bbb 10 *
simon 1:6d68eb9b6bbb 11 * The above copyright notice and this permission notice shall be included in
simon 1:6d68eb9b6bbb 12 * all copies or substantial portions of the Software.
simon 1:6d68eb9b6bbb 13 *
simon 1:6d68eb9b6bbb 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:6d68eb9b6bbb 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:6d68eb9b6bbb 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:6d68eb9b6bbb 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:6d68eb9b6bbb 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:6d68eb9b6bbb 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:6d68eb9b6bbb 20 * THE SOFTWARE.
simon 1:6d68eb9b6bbb 21 */
simon 1:6d68eb9b6bbb 22
simon 1:6d68eb9b6bbb 23 #include "PwmIn.h"
simon 1:6d68eb9b6bbb 24
simon 1:6d68eb9b6bbb 25 PwmIn::PwmIn(PinName p) : _p(p) {
simon 1:6d68eb9b6bbb 26 _p.rise(this, &PwmIn::rise);
simon 1:6d68eb9b6bbb 27 _p.fall(this, &PwmIn::fall);
simon 1:6d68eb9b6bbb 28 _period = 0.0;
simon 1:6d68eb9b6bbb 29 _pulsewidth = 0.0;
simon 1:6d68eb9b6bbb 30 _t.start();
simon 1:6d68eb9b6bbb 31 }
simon 1:6d68eb9b6bbb 32
simon 1:6d68eb9b6bbb 33 float PwmIn::period() {
simon 1:6d68eb9b6bbb 34 return _period;
simon 1:6d68eb9b6bbb 35 }
simon 1:6d68eb9b6bbb 36
simon 1:6d68eb9b6bbb 37 float PwmIn::pulsewidth() {
simon 1:6d68eb9b6bbb 38 return _pulsewidth;
simon 1:6d68eb9b6bbb 39 }
simon 1:6d68eb9b6bbb 40
simon 1:6d68eb9b6bbb 41 float PwmIn::dutycycle() {
simon 1:6d68eb9b6bbb 42 return _pulsewidth / _period;
simon 1:6d68eb9b6bbb 43 }
simon 1:6d68eb9b6bbb 44
simon 1:6d68eb9b6bbb 45 void PwmIn::rise() {
simon 1:6d68eb9b6bbb 46 _period = _t.read();
simon 1:6d68eb9b6bbb 47 _t.reset();
simon 1:6d68eb9b6bbb 48 }
simon 1:6d68eb9b6bbb 49
simon 1:6d68eb9b6bbb 50 void PwmIn::fall() {
simon 1:6d68eb9b6bbb 51 _pulsewidth = _t.read();
simon 1:6d68eb9b6bbb 52 }