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 15:43:02 2014 +0000
Revision:
6:e1df0b4f36e4
Parent:
4:44dd41c989d0
Child:
7:2a58e3363242
Added mutex for protection of pulsewidth and period during 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 6:e1df0b4f36e4 24 #include "rtos.h"
pHysiX 0:1ae106ada18a 25
pHysiX 3:38bd7aa1d4d2 26 #ifndef MBED_PWMIN_H_
pHysiX 3:38bd7aa1d4d2 27 #define MBED_PWMIN_H_
pHysiX 0:1ae106ada18a 28
pHysiX 3:38bd7aa1d4d2 29 /** PwmIn class to read PWM inputs
pHysiX 3:38bd7aa1d4d2 30 *
pHysiX 3:38bd7aa1d4d2 31 * Uses InterruptIn to measure the changes on the input
pHysiX 3:38bd7aa1d4d2 32 * and record the time they occur
pHysiX 3:38bd7aa1d4d2 33 *
pHysiX 3:38bd7aa1d4d2 34 * @note uses InterruptIn, so not available on p19/p20
pHysiX 3:38bd7aa1d4d2 35 */
pHysiX 0:1ae106ada18a 36 class PwmIn
pHysiX 0:1ae106ada18a 37 {
pHysiX 0:1ae106ada18a 38 public:
pHysiX 3:38bd7aa1d4d2 39 /** Create a PwmIn
pHysiX 3:38bd7aa1d4d2 40 *
pHysiX 3:38bd7aa1d4d2 41 * @param p The pwm input pin (must support InterruptIn)
pHysiX 3:38bd7aa1d4d2 42 */
pHysiX 0:1ae106ada18a 43 PwmIn(PinName p);
pHysiX 0:1ae106ada18a 44
pHysiX 0:1ae106ada18a 45 void rise();
pHysiX 0:1ae106ada18a 46 void fall();
pHysiX 3:38bd7aa1d4d2 47
pHysiX 3:38bd7aa1d4d2 48 /** Read the current period
pHysiX 3:38bd7aa1d4d2 49 *
pHysiX 3:38bd7aa1d4d2 50 * @returns the period in seconds
pHysiX 3:38bd7aa1d4d2 51 */
pHysiX 0:1ae106ada18a 52 float period();
pHysiX 3:38bd7aa1d4d2 53
pHysiX 3:38bd7aa1d4d2 54 /** Read the current pulsewidth
pHysiX 3:38bd7aa1d4d2 55 *
pHysiX 3:38bd7aa1d4d2 56 * @returns the pulsewidth in seconds
pHysiX 3:38bd7aa1d4d2 57 */
pHysiX 0:1ae106ada18a 58 float pulsewidth();
pHysiX 3:38bd7aa1d4d2 59
pHysiX 3:38bd7aa1d4d2 60 /** Read the current dutycycle
pHysiX 3:38bd7aa1d4d2 61 *
pHysiX 3:38bd7aa1d4d2 62 * @returns the dutycycle as a percentage, represented between 0.0-1.0
pHysiX 3:38bd7aa1d4d2 63 */
pHysiX 0:1ae106ada18a 64 float dutycycle();
pHysiX 0:1ae106ada18a 65
pHysiX 3:38bd7aa1d4d2 66 /** Timer to detect when a pulse has not changed over time (Ian).
pHysiX 3:38bd7aa1d4d2 67 */
pHysiX 0:1ae106ada18a 68 Timer stallTimer;
pHysiX 0:1ae106ada18a 69 protected:
pHysiX 6:e1df0b4f36e4 70 Mutex mutex_var_update;
pHysiX 6:e1df0b4f36e4 71
pHysiX 0:1ae106ada18a 72 InterruptIn _p;
pHysiX 0:1ae106ada18a 73 Timer _t;
pHysiX 4:44dd41c989d0 74 volatile int _pulsewidth, _period;
pHysiX 0:1ae106ada18a 75 bool stallInit;
pHysiX 0:1ae106ada18a 76 };
pHysiX 0:1ae106ada18a 77
pHysiX 0:1ae106ada18a 78 #endif