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:
Thu May 08 09:42:36 2014 +0000
Revision:
2:1f00548c2aa6
Parent:
0:1ae106ada18a
Child:
3:38bd7aa1d4d2
Comment added for clarity

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pHysiX 0:1ae106ada18a 1 /* PWM In by Simon Ford */
pHysiX 2:1f00548c2aa6 2 /* Includes a stall guard */
pHysiX 0:1ae106ada18a 3
pHysiX 0:1ae106ada18a 4 #include "mbed.h"
pHysiX 0:1ae106ada18a 5
pHysiX 0:1ae106ada18a 6 #ifndef _PWMIN_H_
pHysiX 0:1ae106ada18a 7 #define _PWMIN_H_
pHysiX 0:1ae106ada18a 8
pHysiX 0:1ae106ada18a 9 class PwmIn
pHysiX 0:1ae106ada18a 10 {
pHysiX 0:1ae106ada18a 11 public:
pHysiX 0:1ae106ada18a 12 PwmIn(PinName p);
pHysiX 0:1ae106ada18a 13
pHysiX 0:1ae106ada18a 14 void rise();
pHysiX 0:1ae106ada18a 15 void fall();
pHysiX 0:1ae106ada18a 16
pHysiX 0:1ae106ada18a 17 float period();
pHysiX 0:1ae106ada18a 18 float pulsewidth();
pHysiX 0:1ae106ada18a 19 float dutycycle();
pHysiX 0:1ae106ada18a 20
pHysiX 0:1ae106ada18a 21 Timer stallTimer;
pHysiX 0:1ae106ada18a 22
pHysiX 0:1ae106ada18a 23 protected:
pHysiX 0:1ae106ada18a 24 InterruptIn _p;
pHysiX 0:1ae106ada18a 25 Timer _t;
pHysiX 0:1ae106ada18a 26 int _pulsewidth, _period;
pHysiX 0:1ae106ada18a 27 bool stallInit;
pHysiX 0:1ae106ada18a 28 };
pHysiX 0:1ae106ada18a 29
pHysiX 0:1ae106ada18a 30 #endif