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
Diff: PwmIn.h
- Revision:
- 0:1ae106ada18a
- Child:
- 2:1f00548c2aa6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PwmIn.h Wed Apr 30 06:37:19 2014 +0000 @@ -0,0 +1,29 @@ +/* PWM In by Simon Ford */ + +#include "mbed.h" + +#ifndef _PWMIN_H_ +#define _PWMIN_H_ + +class PwmIn +{ +public: + PwmIn(PinName p); + + void rise(); + void fall(); + + float period(); + float pulsewidth(); + float dutycycle(); + + Timer stallTimer; + +protected: + InterruptIn _p; + Timer _t; + int _pulsewidth, _period; + bool stallInit; +}; + +#endif