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
Revision:
6:e1df0b4f36e4
Parent:
4:44dd41c989d0
Child:
7:2a58e3363242
--- a/PwmIn.h	Fri May 16 23:19:34 2014 +0000
+++ b/PwmIn.h	Mon May 19 15:43:02 2014 +0000
@@ -21,6 +21,7 @@
  * THE SOFTWARE.
  */
 #include "mbed.h"
+#include "rtos.h"
 
 #ifndef MBED_PWMIN_H_
 #define MBED_PWMIN_H_
@@ -66,6 +67,8 @@
      */
     Timer stallTimer;
 protected:
+    Mutex mutex_var_update;
+
     InterruptIn _p;
     Timer _t;
     volatile int _pulsewidth, _period;