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 #ifndef MBED_PWMIN_H
simon 1:6d68eb9b6bbb 24 #define MBED_PWMIN_H
simon 1:6d68eb9b6bbb 25
simon 1:6d68eb9b6bbb 26 #include "mbed.h"
simon 1:6d68eb9b6bbb 27
simon 1:6d68eb9b6bbb 28 /** PwmIn class to read PWM inputs
simon 1:6d68eb9b6bbb 29 *
simon 1:6d68eb9b6bbb 30 * Uses InterruptIn to measure the changes on the input
simon 1:6d68eb9b6bbb 31 * and record the time they occur
simon 1:6d68eb9b6bbb 32 *
simon 1:6d68eb9b6bbb 33 * @note uses InterruptIn, so not available on p19/p20
simon 1:6d68eb9b6bbb 34 */
simon 1:6d68eb9b6bbb 35 class PwmIn {
simon 1:6d68eb9b6bbb 36 public:
simon 1:6d68eb9b6bbb 37 /** Create a PwmIn
simon 1:6d68eb9b6bbb 38 *
simon 1:6d68eb9b6bbb 39 * @param p The pwm input pin (must support InterruptIn)
simon 1:6d68eb9b6bbb 40 */
simon 1:6d68eb9b6bbb 41 PwmIn(PinName p);
simon 1:6d68eb9b6bbb 42
simon 1:6d68eb9b6bbb 43 /** Read the current period
simon 1:6d68eb9b6bbb 44 *
simon 1:6d68eb9b6bbb 45 * @returns the period in seconds
simon 1:6d68eb9b6bbb 46 */
simon 1:6d68eb9b6bbb 47 float period();
simon 1:6d68eb9b6bbb 48
simon 1:6d68eb9b6bbb 49 /** Read the current pulsewidth
simon 1:6d68eb9b6bbb 50 *
simon 1:6d68eb9b6bbb 51 * @returns the pulsewidth in seconds
simon 1:6d68eb9b6bbb 52 */
simon 1:6d68eb9b6bbb 53 float pulsewidth();
simon 1:6d68eb9b6bbb 54
simon 1:6d68eb9b6bbb 55 /** Read the current dutycycle
simon 1:6d68eb9b6bbb 56 *
simon 1:6d68eb9b6bbb 57 * @returns the dutycycle as a percentage, represented between 0.0-1.0
simon 1:6d68eb9b6bbb 58 */
simon 1:6d68eb9b6bbb 59 float dutycycle();
simon 1:6d68eb9b6bbb 60
simon 1:6d68eb9b6bbb 61 protected:
simon 1:6d68eb9b6bbb 62 void rise();
simon 1:6d68eb9b6bbb 63 void fall();
simon 1:6d68eb9b6bbb 64
simon 1:6d68eb9b6bbb 65 InterruptIn _p;
simon 1:6d68eb9b6bbb 66 Timer _t;
simon 1:6d68eb9b6bbb 67 float _pulsewidth, _period;
simon 1:6d68eb9b6bbb 68 };
simon 1:6d68eb9b6bbb 69
simon 1:6d68eb9b6bbb 70 #endif