Code to read a PPM/PWM pulse, interrupt driven. Most useful for RC receivers. Code is non-blocking, and a poll of the stall timer can detect if a signal has been lost (signal changes will reset the timer). Code originally written by Simon Ford, modified by me to add in a lost-signal guard.

Dependents:   triforce-control usbjoystick_prg

Code is simple to use.

Will add a quick tutorial on use, and how to use the lost-signal/stall detection in due course.

Pulsewidth and period variables are:

  • Declared volatile to make sure the value read is not compiler optimised, since the values are modified through an ISR; and

Edit: WITHDRAWN:

  • Protected by mutex to make sure that values read are true (such as when an ISR is occurring, but a thread tries to read the value during the ISR). Reason:
  • Mutexes are not meant to be called in an ISR (asynchronous). They are costly and may block a thread
Committer:
pHysiX
Date:
Mon May 19 16:04:34 2014 +0000
Revision:
7:2a58e3363242
Parent:
6:e1df0b4f36e4
Removed mutex. Bad and incorrect of mutex for an ISR

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pHysiX 3:38bd7aa1d4d2 1 /* mbed PwmIn Library
pHysiX 3:38bd7aa1d4d2 2 * Copyright (c) 2008-2010, sford
pHysiX 3:38bd7aa1d4d2 3 * Subsequently modified by Trung Tin Ian HUA 2014 to add lost signal detection.
pHysiX 3:38bd7aa1d4d2 4 *
pHysiX 3:38bd7aa1d4d2 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
pHysiX 3:38bd7aa1d4d2 6 * of this software and associated documentation files (the "Software"), to deal
pHysiX 3:38bd7aa1d4d2 7 * in the Software without restriction, including without limitation the rights
pHysiX 3:38bd7aa1d4d2 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pHysiX 3:38bd7aa1d4d2 9 * copies of the Software, and to permit persons to whom the Software is
pHysiX 3:38bd7aa1d4d2 10 * furnished to do so, subject to the following conditions:
pHysiX 3:38bd7aa1d4d2 11 *
pHysiX 3:38bd7aa1d4d2 12 * The above copyright notice and this permission notice shall be included in
pHysiX 3:38bd7aa1d4d2 13 * all copies or substantial portions of the Software.
pHysiX 3:38bd7aa1d4d2 14 *
pHysiX 3:38bd7aa1d4d2 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pHysiX 3:38bd7aa1d4d2 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pHysiX 3:38bd7aa1d4d2 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pHysiX 3:38bd7aa1d4d2 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pHysiX 3:38bd7aa1d4d2 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pHysiX 3:38bd7aa1d4d2 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pHysiX 3:38bd7aa1d4d2 21 * THE SOFTWARE.
pHysiX 3:38bd7aa1d4d2 22 */
pHysiX 0:1ae106ada18a 23 #include "mbed.h"
pHysiX 0:1ae106ada18a 24
pHysiX 3:38bd7aa1d4d2 25 #ifndef MBED_PWMIN_H_
pHysiX 3:38bd7aa1d4d2 26 #define MBED_PWMIN_H_
pHysiX 0:1ae106ada18a 27
pHysiX 3:38bd7aa1d4d2 28 /** PwmIn class to read PWM inputs
pHysiX 3:38bd7aa1d4d2 29 *
pHysiX 3:38bd7aa1d4d2 30 * Uses InterruptIn to measure the changes on the input
pHysiX 3:38bd7aa1d4d2 31 * and record the time they occur
pHysiX 3:38bd7aa1d4d2 32 *
pHysiX 3:38bd7aa1d4d2 33 * @note uses InterruptIn, so not available on p19/p20
pHysiX 3:38bd7aa1d4d2 34 */
pHysiX 0:1ae106ada18a 35 class PwmIn
pHysiX 0:1ae106ada18a 36 {
pHysiX 0:1ae106ada18a 37 public:
pHysiX 3:38bd7aa1d4d2 38 /** Create a PwmIn
pHysiX 3:38bd7aa1d4d2 39 *
pHysiX 3:38bd7aa1d4d2 40 * @param p The pwm input pin (must support InterruptIn)
pHysiX 3:38bd7aa1d4d2 41 */
pHysiX 0:1ae106ada18a 42 PwmIn(PinName p);
pHysiX 0:1ae106ada18a 43
pHysiX 0:1ae106ada18a 44 void rise();
pHysiX 0:1ae106ada18a 45 void fall();
pHysiX 3:38bd7aa1d4d2 46
pHysiX 3:38bd7aa1d4d2 47 /** Read the current period
pHysiX 3:38bd7aa1d4d2 48 *
pHysiX 3:38bd7aa1d4d2 49 * @returns the period in seconds
pHysiX 3:38bd7aa1d4d2 50 */
pHysiX 0:1ae106ada18a 51 float period();
pHysiX 3:38bd7aa1d4d2 52
pHysiX 3:38bd7aa1d4d2 53 /** Read the current pulsewidth
pHysiX 3:38bd7aa1d4d2 54 *
pHysiX 3:38bd7aa1d4d2 55 * @returns the pulsewidth in seconds
pHysiX 3:38bd7aa1d4d2 56 */
pHysiX 0:1ae106ada18a 57 float pulsewidth();
pHysiX 3:38bd7aa1d4d2 58
pHysiX 3:38bd7aa1d4d2 59 /** Read the current dutycycle
pHysiX 3:38bd7aa1d4d2 60 *
pHysiX 3:38bd7aa1d4d2 61 * @returns the dutycycle as a percentage, represented between 0.0-1.0
pHysiX 3:38bd7aa1d4d2 62 */
pHysiX 0:1ae106ada18a 63 float dutycycle();
pHysiX 0:1ae106ada18a 64
pHysiX 3:38bd7aa1d4d2 65 /** Timer to detect when a pulse has not changed over time (Ian).
pHysiX 3:38bd7aa1d4d2 66 */
pHysiX 0:1ae106ada18a 67 Timer stallTimer;
pHysiX 0:1ae106ada18a 68 protected:
pHysiX 0:1ae106ada18a 69 InterruptIn _p;
pHysiX 0:1ae106ada18a 70 Timer _t;
pHysiX 4:44dd41c989d0 71 volatile int _pulsewidth, _period;
pHysiX 0:1ae106ada18a 72 bool stallInit;
pHysiX 0:1ae106ada18a 73 };
pHysiX 0:1ae106ada18a 74
pHysiX 0:1ae106ada18a 75 #endif